fix(visits): start the visit preparation and its report at the matching visit

Both surfaces read the start of their window from `preparation` and say
why it starts there: "Since your last visit with <practice> on <date>",
"Since your last <specialty> visit on <date>", "Since your last visit
of the same kind (<kind>) on <date>", or "The last 90 days" with the
reason that no earlier visit matched. The visits tab no longer picks
the last visit of any kind from the past list.

The report panel's appointment offer now sets both dates, the start
from the server and the end on the appointment's day, instead of the
end alone beside whatever start the panel held. Both stay editable.
This commit is contained in:
Marc-André Bombeck committed 2026-10-21 12:32:26 +02:00
1 parent 402cf1a35a
commit 99fec79177
15 files changed
+377 -86

No files matched your search

@@ -32,7 +32,7 @@ vi.mock("@/lib/api/api-fetch", async () => {
import { I18nProvider } from "@/lib/i18n/context";
import { queryKeys } from "@/lib/query-keys";
import type { EncounterDTO } from "@/lib/encounters/dto";
import { ReportVisitOffer } from "../report-visit-offer";
import { ReportVisitOffer, visitReportWindow } from "../report-visit-offer";
const DAY_MS = 24 * 60 * 60 * 1000;
@@ -67,7 +67,7 @@ function render(upcoming: EncounterDTO[], onUse = () => {}) {
return renderToStaticMarkup(
<QueryClientProvider client={queryClient}>
<I18nProvider initialLocale="en">
<ReportVisitOffer onUseVisitDate={onUse} />
<ReportVisitOffer onUseVisitWindow={onUse} />
</I18nProvider>
</QueryClientProvider>,
);
@@ -108,4 +108,57 @@ describe("<ReportVisitOffer>", () => {
expect(html).toContain('data-slot="report-visit-offer-apply"');
expect(onUse).not.toHaveBeenCalled();
});
// The issue behind this: an internist a week ago, a dermatologist next. The
// start used to be whatever the panel held, typically the last visit of any
// kind; it is now the server's window before this visit, and says why.
it("names where the report would start, and why there", () => {
const html = render([
visit({
id: "derm",
preparation: {
from: "2026-03-02",
basis: "specialty",
since: {
id: "old-derm",
occurredAt: "2026-03-02T09:00:00.000Z",
kind: "SPECIALIST",
practitionerName: "Former skin practice",
specialty: "Dermatology",
},
},
}),
]);
expect(html).toContain('data-slot="report-visit-offer-window"');
expect(html).toContain("Since your last Dermatology visit on");
expect(html).toContain("Use this window");
});
it("says why on the default window", () => {
const html = render([
visit({
id: "first",
preparation: { from: "2026-07-23", basis: "default", since: null },
}),
]);
expect(html).toContain("The last 90 days");
expect(html).toContain(
"No earlier visit with the same practice, specialty or kind.",
);
});
it("applies the server's start and the appointment's day on a press", () => {
const at = new Date(Date.now() + 5 * DAY_MS);
const window = visitReportWindow(
visit({
id: "derm",
occurredAt: at.toISOString(),
preparation: { from: "2026-03-02", basis: "default", since: null },
}),
);
expect(window.from).toBe("2026-03-02");
expect(window.to).toMatch(/^\d{4}-\d{2}-\d{2}$/);
// Without a window from the server, only the end moves.
expect(visitReportWindow(visit({ id: "plain" })).from).toBeNull();
});
});
@@ -14,15 +14,20 @@
* Termin ansteht") and does nothing until pressed. A control that moves on its
* own is the thing that makes an export panel untrustworthy.
*
* Nothing about the stored selection changes: this sets the custom-range end
* date and nothing else.
* Pressing it sets both ends of the custom range: the end on the appointment's
* day, the start where the server put the window before that visit
* (`EncounterDTO.preparation`: the last visit with the same practitioner, else
* the same specialty, else the same kind, else the last 90 days). The offer
* says which, so the start date explains itself, and both dates stay editable.
* Nothing about the stored selection changes.
*/
import { CalendarClock, X } from "lucide-react";
import { useState } from "react";
import { encounterKindText } from "@/components/encounters/encounter-labels";
import { preparationText } from "@/components/encounters/preparation-text";
import { Button } from "@/components/ui/button";
import { useEncounters } from "@/hooks/use-encounters";
import { useEncounters, type Encounter } from "@/hooks/use-encounters";
import { useFormatters, useTranslations } from "@/lib/i18n/context";
import type { EncounterKind } from "@/generated/prisma/client";
import { detectBrowserTimezone, userDayKey } from "@/lib/tz/format";
@@ -40,11 +45,27 @@ function localDay(iso: string): string {
return userDayKey(new Date(iso), detectBrowserTimezone());
}
/** The report window an appointment offers, as `yyyy-MM-dd` days. */
export interface VisitReportWindow {
/** The server's start of the window before the visit; null if it has none. */
from: string | null;
/** The appointment's own day. */
to: string;
}
/** What pressing the offer for `visit` applies. */
export function visitReportWindow(visit: Encounter): VisitReportWindow {
return {
from: visit.preparation?.from ?? null,
to: localDay(visit.occurredAt),
};
}
export function ReportVisitOffer({
onUseVisitDate,
onUseVisitWindow,
}: {
/** Sets the custom window's END date. Called only on an explicit press. */
onUseVisitDate: (endDay: string) => void;
/** Sets the custom window. Called only on an explicit press. */
onUseVisitWindow: (window: VisitReportWindow) => void;
}) {
const { t } = useTranslations();
const format = useFormatters();
@@ -68,6 +89,9 @@ export function ReportVisitOffer({
const what =
next.practitioner?.name ?? encounterKindText(t, next.kind as EncounterKind);
const windowText = next.preparation
? preparationText(t, format.dateShortSmartCalendar, next.preparation)
: null;
return (
<div
@@ -78,24 +102,38 @@ export function ReportVisitOffer({
a phone the sentence takes the full row and the two buttons wrap
below it; with a zero basis it was squeezed into a one-word column
beside them. */}
<p className="text-foreground flex min-w-0 flex-1 basis-full items-center gap-2 text-sm sm:basis-0">
<CalendarClock className="size-4 shrink-0" aria-hidden />
<span>
{t("settings.healthRecord.visitOffer", {
what,
when: format.date(next.occurredAt),
})}
</span>
</p>
<div className="text-foreground flex min-w-0 flex-1 basis-full items-start gap-2 text-sm sm:basis-0">
<CalendarClock className="mt-0.5 size-4 shrink-0" aria-hidden />
<div className="min-w-0 space-y-0.5">
<p>
{t("settings.healthRecord.visitOffer", {
what,
when: format.date(next.occurredAt),
})}
</p>
{/* Where the report would start, and why there: meta, so muted. */}
{windowText ? (
<p
className="text-muted-foreground text-xs"
data-slot="report-visit-offer-window"
>
{windowText.title}
</p>
) : null}
{windowText?.reason ? (
<p className="text-muted-foreground text-xs">{windowText.reason}</p>
) : null}
</div>
</div>
<Button
type="button"
variant="outline"
size="sm"
className="ml-auto min-h-11 sm:ml-0 sm:min-h-9"
data-slot="report-visit-offer-apply"
onClick={() => onUseVisitDate(localDay(next.occurredAt))}
onClick={() => onUseVisitWindow(visitReportWindow(next))}
>
{t("settings.healthRecord.visitOfferApply")}
{t("settings.healthRecord.visitOfferApplyWindow")}
</Button>
<Button
type="button"
@@ -322,11 +322,12 @@ export function HealthRecordExportPanel() {
</div>
{/* Never auto-applies: pressing it is what opens the custom window and
sets its end date. */}
sets both its dates, which stay editable. */}
<ReportVisitOffer
onUseVisitDate={(endDay) => {
onUseVisitWindow={({ from, to }) => {
setCustomRange(true);
setEndDate(endDay);
if (from) setStartDate(from);
setEndDate(to);
}}
/>