mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-17 07:42:23 +00:00
feat(medications): GLP-1 cadence visualisation + compliance chips on detail page
This commit is contained in:
1 parent
3ef49679ed
commit
71cc05acd6
9 files changed
+770
No files matched your search
@@ -8,6 +8,7 @@ import { Button } from "@/components/ui/button";
|
||||
import { IntakeHistoryList } from "@/components/medications/intake-history-list";
|
||||
import { DrugLevelChart } from "@/components/medications/DrugLevelChart";
|
||||
import { SideEffectsSection } from "@/components/medications/SideEffectsSection";
|
||||
import { SchedulingSection } from "@/components/medications/SchedulingSection";
|
||||
import { ArrowLeft, Loader2, Plus } from "lucide-react";
|
||||
import Link from "next/link";
|
||||
import { useTranslations } from "@/lib/i18n/context";
|
||||
@@ -40,6 +41,7 @@ export default function IntakeHistoryPage({
|
||||
name: string;
|
||||
dose: string;
|
||||
treatmentClass?: string;
|
||||
notificationsEnabled?: boolean;
|
||||
};
|
||||
},
|
||||
enabled: isAuthenticated,
|
||||
@@ -109,6 +111,18 @@ export default function IntakeHistoryPage({
|
||||
<SideEffectsSection medicationId={id} />
|
||||
)}
|
||||
|
||||
{/* v1.4.25 W19e — GLP-1 cadence visualisation + compliance chips.
|
||||
Sits between the side-effect logbook and the intake history so
|
||||
the user lands on cycle context (drug-level), then symptom
|
||||
record, then schedule cadence + adherence, then the
|
||||
dose-by-dose timeline. */}
|
||||
{medication?.treatmentClass === "GLP1" && (
|
||||
<SchedulingSection
|
||||
medicationId={id}
|
||||
reminderEnabled={medication.notificationsEnabled ?? true}
|
||||
/>
|
||||
)}
|
||||
|
||||
<IntakeHistoryList
|
||||
medicationId={id}
|
||||
createOpen={createOpen}
|
||||
|
||||
@@ -0,0 +1,309 @@
|
||||
"use client";
|
||||
|
||||
import { useMemo } from "react";
|
||||
import Link from "next/link";
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
import { Loader2, BellRing, BellOff, Pencil } from "lucide-react";
|
||||
|
||||
import { Badge } from "@/components/ui/badge";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { useFormatters, useTranslations } from "@/lib/i18n/context";
|
||||
|
||||
/**
|
||||
* v1.4.25 W19e — GLP-1 cadence + compliance section.
|
||||
*
|
||||
* Sits between `<SideEffectsSection>` (W19d) and `<IntakeHistoryList>`
|
||||
* on the medication detail page. Same chrome as the W19d section so
|
||||
* the three Wave-4b panels read as one visual group.
|
||||
*
|
||||
* Three sub-sections, top-to-bottom:
|
||||
*
|
||||
* 1. Header strip — reminder on/off badge + next-due chip + "Edit
|
||||
* schedule" link back to the medication list (where the existing
|
||||
* `medication-form` is the canonical schedule editor; we
|
||||
* deliberately do not re-implement the schedule editor here so
|
||||
* there's no second source of truth).
|
||||
*
|
||||
* 2. Cadence visualisation — a 30-day track showing one cell per
|
||||
* expected dose, status-coloured (taken / skipped / missed /
|
||||
* upcoming). The legend below the track names each status.
|
||||
*
|
||||
* 3. Compliance chips — four monochrome chips: adherence rate %,
|
||||
* current streak, longest streak, missed last 30. Marc-memory:
|
||||
* no gamified badges, no WebP art.
|
||||
*
|
||||
* Data comes from /api/medications/[id]/cadence; the route delegates
|
||||
* to the pure scheduling helpers so the chart, the chips, and the
|
||||
* route agree dose-for-dose.
|
||||
*/
|
||||
|
||||
interface CadenceTimelineEntry {
|
||||
day: string;
|
||||
windowStart: string;
|
||||
windowEnd: string;
|
||||
scheduleIndex: number;
|
||||
status: "taken" | "skipped" | "missed" | "upcoming";
|
||||
}
|
||||
|
||||
interface CadenceChips {
|
||||
adherenceRate: number | null;
|
||||
currentStreak: number;
|
||||
longestStreak: number;
|
||||
missedLast30: number;
|
||||
windowDays: number;
|
||||
}
|
||||
|
||||
interface CadenceResponse {
|
||||
windowDays: number;
|
||||
anchorIso: string;
|
||||
next: {
|
||||
windowStart: string;
|
||||
windowEnd: string;
|
||||
scheduleIndex: number;
|
||||
} | null;
|
||||
chips: CadenceChips;
|
||||
timeline: CadenceTimelineEntry[];
|
||||
}
|
||||
|
||||
interface SchedulingSectionProps {
|
||||
medicationId: string;
|
||||
reminderEnabled: boolean;
|
||||
}
|
||||
|
||||
const STATUS_STYLES: Record<CadenceTimelineEntry["status"], string> = {
|
||||
// Monochrome scale; chart visually identical contract — no new colour
|
||||
// tokens, all classes already exist in the design system.
|
||||
taken: "bg-primary/85",
|
||||
skipped: "bg-muted",
|
||||
missed: "bg-destructive/65",
|
||||
upcoming: "border-border/70 border bg-background",
|
||||
};
|
||||
|
||||
export function SchedulingSection({
|
||||
medicationId,
|
||||
reminderEnabled,
|
||||
}: SchedulingSectionProps) {
|
||||
const { t } = useTranslations();
|
||||
const fmt = useFormatters();
|
||||
|
||||
const { data, isLoading, error } = useQuery({
|
||||
queryKey: ["medications", medicationId, "cadence"],
|
||||
queryFn: async (): Promise<CadenceResponse> => {
|
||||
const res = await fetch(
|
||||
`/api/medications/${medicationId}/cadence?days=30`,
|
||||
);
|
||||
if (!res.ok) {
|
||||
throw new Error(`Failed to load cadence: ${res.status}`);
|
||||
}
|
||||
const json = await res.json();
|
||||
return json.data as CadenceResponse;
|
||||
},
|
||||
});
|
||||
|
||||
const adherenceLabel = useMemo(() => {
|
||||
if (!data) return null;
|
||||
if (data.chips.adherenceRate === null) {
|
||||
return t("medications.scheduling.compliance.noData");
|
||||
}
|
||||
return `${data.chips.adherenceRate}%`;
|
||||
}, [data, t]);
|
||||
|
||||
return (
|
||||
<section
|
||||
className="border-border/60 rounded-md border"
|
||||
aria-labelledby="scheduling-heading"
|
||||
>
|
||||
<header className="flex items-center justify-between px-3 py-2.5">
|
||||
<h2
|
||||
id="scheduling-heading"
|
||||
className="text-foreground/85 text-sm font-medium"
|
||||
>
|
||||
{t("medications.scheduling.section")}
|
||||
</h2>
|
||||
<div className="flex items-center gap-2">
|
||||
<Badge
|
||||
variant={reminderEnabled ? "secondary" : "outline"}
|
||||
className="gap-1 text-[10px]"
|
||||
>
|
||||
{reminderEnabled ? (
|
||||
<BellRing className="h-3 w-3" />
|
||||
) : (
|
||||
<BellOff className="h-3 w-3" />
|
||||
)}
|
||||
{t(
|
||||
reminderEnabled
|
||||
? "medications.scheduling.reminders.on"
|
||||
: "medications.scheduling.reminders.off",
|
||||
)}
|
||||
</Badge>
|
||||
<Button
|
||||
size="sm"
|
||||
variant="outline"
|
||||
asChild
|
||||
aria-label={t("medications.scheduling.editCta")}
|
||||
>
|
||||
<Link href="/medications">
|
||||
<Pencil className="mr-1.5 h-3.5 w-3.5" />
|
||||
{t("medications.scheduling.editCta")}
|
||||
</Link>
|
||||
</Button>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<div className="border-border/60 border-t px-3 py-3 text-xs">
|
||||
{isLoading && (
|
||||
<div className="text-muted-foreground flex items-center gap-2">
|
||||
<Loader2 className="h-3.5 w-3.5 animate-spin" />
|
||||
<span>{t("medications.scheduling.loading")}</span>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{!!error && !isLoading && (
|
||||
<p className="text-destructive">
|
||||
{t("medications.scheduling.loadFailed")}
|
||||
</p>
|
||||
)}
|
||||
|
||||
{data && !isLoading && (
|
||||
<div className="space-y-4">
|
||||
{/* Next-dose chip */}
|
||||
<div className="flex items-center justify-between gap-2">
|
||||
<p className="text-muted-foreground">
|
||||
{data.next ? (
|
||||
<>
|
||||
{t("medications.scheduling.cadenceViz.nextDoseLabel")}{" "}
|
||||
<span className="text-foreground font-medium">
|
||||
{fmt.dateShort(new Date(data.next.windowStart))}
|
||||
</span>
|
||||
</>
|
||||
) : (
|
||||
t("medications.scheduling.cadenceViz.noNextDose")
|
||||
)}
|
||||
</p>
|
||||
</div>
|
||||
|
||||
{/* 30-day timeline */}
|
||||
<div className="space-y-1.5">
|
||||
<p className="text-muted-foreground text-[11px] uppercase tracking-wide">
|
||||
{t("medications.scheduling.cadenceViz.title")}
|
||||
</p>
|
||||
{data.timeline.length === 0 ? (
|
||||
<p className="text-muted-foreground py-1">
|
||||
{t("medications.scheduling.cadenceViz.emptyState")}
|
||||
</p>
|
||||
) : (
|
||||
<>
|
||||
<div
|
||||
className="flex flex-wrap gap-1"
|
||||
role="img"
|
||||
aria-label={t(
|
||||
"medications.scheduling.cadenceViz.ariaTimeline",
|
||||
)}
|
||||
>
|
||||
{data.timeline.map((slot, i) => (
|
||||
<span
|
||||
key={`${slot.windowStart}-${i}`}
|
||||
className={`h-3 w-3 rounded-sm ${STATUS_STYLES[slot.status]}`}
|
||||
title={`${fmt.dateShort(new Date(slot.windowStart))} — ${t(
|
||||
`medications.scheduling.cadenceViz.status.${slot.status}`,
|
||||
)}`}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
<div className="text-muted-foreground flex flex-wrap gap-x-3 gap-y-1 pt-1 text-[10px]">
|
||||
{(["taken", "skipped", "missed", "upcoming"] as const).map(
|
||||
(s) => (
|
||||
<span key={s} className="inline-flex items-center gap-1">
|
||||
<span
|
||||
className={`h-2.5 w-2.5 rounded-sm ${STATUS_STYLES[s]}`}
|
||||
/>
|
||||
<span>
|
||||
{t(
|
||||
`medications.scheduling.cadenceViz.status.${s}`,
|
||||
)}
|
||||
</span>
|
||||
</span>
|
||||
),
|
||||
)}
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Compliance chips */}
|
||||
<div className="space-y-1.5">
|
||||
<p className="text-muted-foreground text-[11px] uppercase tracking-wide">
|
||||
{t("medications.scheduling.compliance.title")}
|
||||
</p>
|
||||
<div className="grid grid-cols-2 gap-2 sm:grid-cols-4">
|
||||
<ComplianceChip
|
||||
label={t(
|
||||
"medications.scheduling.compliance.adherenceRate",
|
||||
)}
|
||||
value={adherenceLabel ?? "—"}
|
||||
tooltip={t(
|
||||
"medications.scheduling.compliance.chips.adherenceTooltip",
|
||||
)}
|
||||
/>
|
||||
<ComplianceChip
|
||||
label={t(
|
||||
"medications.scheduling.compliance.currentStreak",
|
||||
)}
|
||||
value={`${data.chips.currentStreak} ${t(
|
||||
"medications.scheduling.compliance.unit.days",
|
||||
)}`}
|
||||
tooltip={t(
|
||||
"medications.scheduling.compliance.chips.streakTooltip",
|
||||
)}
|
||||
/>
|
||||
<ComplianceChip
|
||||
label={t(
|
||||
"medications.scheduling.compliance.longestStreak",
|
||||
)}
|
||||
value={`${data.chips.longestStreak} ${t(
|
||||
"medications.scheduling.compliance.unit.days",
|
||||
)}`}
|
||||
tooltip={t(
|
||||
"medications.scheduling.compliance.chips.longestTooltip",
|
||||
)}
|
||||
/>
|
||||
<ComplianceChip
|
||||
label={t(
|
||||
"medications.scheduling.compliance.missedLast30",
|
||||
)}
|
||||
value={`${data.chips.missedLast30} ${t(
|
||||
"medications.scheduling.compliance.unit.doses",
|
||||
)}`}
|
||||
tooltip={t(
|
||||
"medications.scheduling.compliance.chips.missedTooltip",
|
||||
)}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</section>
|
||||
);
|
||||
}
|
||||
|
||||
interface ComplianceChipProps {
|
||||
label: string;
|
||||
value: string;
|
||||
tooltip: string;
|
||||
}
|
||||
|
||||
function ComplianceChip({ label, value, tooltip }: ComplianceChipProps) {
|
||||
return (
|
||||
<div
|
||||
className="bg-muted/30 rounded-md px-2.5 py-1.5"
|
||||
title={tooltip}
|
||||
aria-label={`${label}: ${value}. ${tooltip}`}
|
||||
>
|
||||
<p className="text-muted-foreground text-[10px] uppercase tracking-wide">
|
||||
{label}
|
||||
</p>
|
||||
<p className="text-foreground text-sm font-medium">{value}</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,195 @@
|
||||
import { describe, expect, it, vi, beforeEach } from "vitest";
|
||||
import { renderToStaticMarkup } from "react-dom/server";
|
||||
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
||||
|
||||
import { I18nProvider } from "@/lib/i18n/context";
|
||||
import { SchedulingSection } from "@/components/medications/SchedulingSection";
|
||||
|
||||
/**
|
||||
* v1.4.25 W19e — SchedulingSection SSR smoke tests.
|
||||
*
|
||||
* Same testing convention as `SideEffectsSection.test.tsx`:
|
||||
* `renderToStaticMarkup` + seeded react-query cache. The
|
||||
* fetch-driven interactive branches (loading spinner, error state)
|
||||
* are covered by the API-route tests + the pure cadence + compliance
|
||||
* helper tests; the surface tests here pin the static-render contract:
|
||||
*
|
||||
* 1. Section heading + reminder badge render in EN and DE.
|
||||
* 2. Compliance chips display the four values (adherence, current,
|
||||
* longest, missed) when data is seeded.
|
||||
* 3. Empty timeline yields the empty-state copy.
|
||||
* 4. Reminder badge switches text between on / off.
|
||||
*/
|
||||
|
||||
function makeClient() {
|
||||
return new QueryClient({
|
||||
defaultOptions: {
|
||||
queries: {
|
||||
retry: false,
|
||||
staleTime: Number.POSITIVE_INFINITY,
|
||||
refetchOnMount: false,
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
interface SeedShape {
|
||||
windowDays: number;
|
||||
next: { windowStart: string; windowEnd: string; scheduleIndex: number } | null;
|
||||
chips: {
|
||||
adherenceRate: number | null;
|
||||
currentStreak: number;
|
||||
longestStreak: number;
|
||||
missedLast30: number;
|
||||
windowDays: number;
|
||||
};
|
||||
timeline: Array<{
|
||||
day: string;
|
||||
windowStart: string;
|
||||
windowEnd: string;
|
||||
scheduleIndex: number;
|
||||
status: "taken" | "skipped" | "missed" | "upcoming";
|
||||
}>;
|
||||
}
|
||||
|
||||
function seed(client: QueryClient, medId: string, payload: SeedShape) {
|
||||
client.setQueryData(["medications", medId, "cadence"], payload);
|
||||
}
|
||||
|
||||
function render(
|
||||
node: React.ReactNode,
|
||||
client: QueryClient,
|
||||
locale: "en" | "de" = "en",
|
||||
) {
|
||||
return renderToStaticMarkup(
|
||||
<I18nProvider initialLocale={locale}>
|
||||
<QueryClientProvider client={client}>{node}</QueryClientProvider>
|
||||
</I18nProvider>,
|
||||
);
|
||||
}
|
||||
|
||||
const SEEDED: SeedShape = {
|
||||
windowDays: 30,
|
||||
next: {
|
||||
windowStart: "2026-05-15T08:00:00.000Z",
|
||||
windowEnd: "2026-05-15T09:00:00.000Z",
|
||||
scheduleIndex: 0,
|
||||
},
|
||||
chips: {
|
||||
adherenceRate: 92,
|
||||
currentStreak: 12,
|
||||
longestStreak: 21,
|
||||
missedLast30: 2,
|
||||
windowDays: 30,
|
||||
},
|
||||
timeline: [
|
||||
{
|
||||
day: "2026-05-13T00:00:00.000Z",
|
||||
windowStart: "2026-05-13T08:00:00.000Z",
|
||||
windowEnd: "2026-05-13T09:00:00.000Z",
|
||||
scheduleIndex: 0,
|
||||
status: "taken",
|
||||
},
|
||||
{
|
||||
day: "2026-05-14T00:00:00.000Z",
|
||||
windowStart: "2026-05-14T08:00:00.000Z",
|
||||
windowEnd: "2026-05-14T09:00:00.000Z",
|
||||
scheduleIndex: 0,
|
||||
status: "missed",
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
describe("<SchedulingSection> — surface render", () => {
|
||||
it("renders the section heading in English", () => {
|
||||
const client = makeClient();
|
||||
seed(client, "med-1", SEEDED);
|
||||
const html = render(
|
||||
<SchedulingSection medicationId="med-1" reminderEnabled={true} />,
|
||||
client,
|
||||
);
|
||||
expect(html).toContain("Schedule");
|
||||
expect(html).toContain("Reminders on");
|
||||
});
|
||||
|
||||
it("renders the section heading in German", () => {
|
||||
const client = makeClient();
|
||||
seed(client, "med-1", SEEDED);
|
||||
const html = render(
|
||||
<SchedulingSection medicationId="med-1" reminderEnabled={true} />,
|
||||
client,
|
||||
"de",
|
||||
);
|
||||
expect(html).toContain("Zeitplan");
|
||||
expect(html).toContain("Erinnerungen an");
|
||||
});
|
||||
|
||||
it("switches the reminder badge when reminderEnabled is false", () => {
|
||||
const client = makeClient();
|
||||
seed(client, "med-1", SEEDED);
|
||||
const html = render(
|
||||
<SchedulingSection medicationId="med-1" reminderEnabled={false} />,
|
||||
client,
|
||||
);
|
||||
expect(html).toContain("Reminders off");
|
||||
});
|
||||
});
|
||||
|
||||
describe("<SchedulingSection> — chips", () => {
|
||||
it("renders the four compliance chip values", () => {
|
||||
const client = makeClient();
|
||||
seed(client, "med-1", SEEDED);
|
||||
const html = render(
|
||||
<SchedulingSection medicationId="med-1" reminderEnabled={true} />,
|
||||
client,
|
||||
);
|
||||
expect(html).toContain("92%");
|
||||
expect(html).toContain("12");
|
||||
expect(html).toContain("21");
|
||||
expect(html).toContain("Current streak");
|
||||
expect(html).toContain("Longest streak");
|
||||
expect(html).toContain("Adherence");
|
||||
});
|
||||
|
||||
it("renders `No data` when adherence is null", () => {
|
||||
const client = makeClient();
|
||||
seed(client, "med-1", {
|
||||
...SEEDED,
|
||||
chips: { ...SEEDED.chips, adherenceRate: null },
|
||||
});
|
||||
const html = render(
|
||||
<SchedulingSection medicationId="med-1" reminderEnabled={true} />,
|
||||
client,
|
||||
);
|
||||
expect(html).toContain("No data");
|
||||
});
|
||||
});
|
||||
|
||||
describe("<SchedulingSection> — timeline", () => {
|
||||
it("renders the timeline empty-state when no slots are seeded", () => {
|
||||
const client = makeClient();
|
||||
seed(client, "med-1", { ...SEEDED, timeline: [] });
|
||||
const html = render(
|
||||
<SchedulingSection medicationId="med-1" reminderEnabled={true} />,
|
||||
client,
|
||||
);
|
||||
expect(html).toContain("No doses scheduled in this window");
|
||||
});
|
||||
|
||||
it("renders the legend with all four statuses", () => {
|
||||
const client = makeClient();
|
||||
seed(client, "med-1", SEEDED);
|
||||
const html = render(
|
||||
<SchedulingSection medicationId="med-1" reminderEnabled={true} />,
|
||||
client,
|
||||
);
|
||||
expect(html).toContain("Taken");
|
||||
expect(html).toContain("Skipped");
|
||||
expect(html).toContain("Missed");
|
||||
expect(html).toContain("Upcoming");
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user