"use client"; import { useRecordCapabilities } from "@/hooks/use-record-capabilities"; import { useId, useMemo, useState } from "react"; import { createPortal } from "react-dom"; import { useInfiniteQuery, useMutation, useQueryClient, } from "@tanstack/react-query"; import { showErrorToast } from "@/lib/errors/error-toast"; import { Loader2, Plus, Trash2 } from "lucide-react"; import { Button } from "@/components/ui/button"; import { ResponsiveSheet } from "@/components/ui/responsive-sheet"; import { QueryErrorCard } from "@/components/ui/query-error-card"; import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, } from "@/components/ui/alert-dialog"; import { Label } from "@/components/ui/label"; import { Badge } from "@/components/ui/badge"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { Textarea } from "@/components/ui/textarea"; import { useFormatters, useTranslations } from "@/lib/i18n/context"; import { useRovingRadioGroup } from "@/hooks/use-roving-radio-group"; import type { MedicationSideEffectCategory, MedicationSideEffectEntry, } from "@/generated/prisma/client"; import { SIDE_EFFECT_CATEGORY_ORDER, SIDE_EFFECT_SEVERITY_LADDER, categoryForEntry, entriesByCategory, severityLikertLabel, } from "@/lib/medications/side-effects/taxonomy"; import { MedicationDetailSection } from "@/components/medications/medication-detail-section"; import { apiDelete, apiFetchEnvelope, apiPost } from "@/lib/api/api-fetch"; import { queryKeys } from "@/lib/query-keys"; /** * v1.4.25 W19d — GLP-1 side-effect section for the medication detail * page. Sits below the drug-level chart and above the intake history * timeline. Mounted only for `treatmentClass === "GLP1"` rows. * * UX shape: * - Section header + "Log side effect" CTA opens a modal dialog. * - Inside the dialog: category picker → entry chip-picker (filtered * by category) → 5-Likert severity ladder → optional notes. * - Below the header: timeline of the last 30 days, newest first, * each row showing category badge + entry label + severity chip + * timestamp + delete button + collapsed notes. * * v1.4.25 W21 Fix-N — wraps the shared `` so * the three sibling sections share one chrome contract, and swaps the * native `` so the picker * matches the rest of the form library (focus rings, keyboard nav, * theme tokens). * * Component-tests cover: empty state, category → entry filtering, * submit-and-refetch flow, severity-label translation lookup. */ interface SideEffectRow { id: string; category: MedicationSideEffectCategory; entry: MedicationSideEffectEntry; severity: number; occurredAt: string; notes: string | null; createdAt: string; } interface SideEffectsSectionProps { medicationId: string; } const NOTES_MAX = 280; function categoryI18nKey(category: MedicationSideEffectCategory): string { // GI → gi, METABOLIC → metabolic, INJECTION_SITE → injectionSite, // COGNITIVE → cognitive, GLP1_SPECIFIC → glp1Specific. switch (category) { case "GI": return "gi"; case "METABOLIC": return "metabolic"; case "INJECTION_SITE": return "injectionSite"; case "COGNITIVE": return "cognitive"; case "GLP1_SPECIFIC": return "glp1Specific"; } } function entryI18nKey(entry: MedicationSideEffectEntry): string { // SCREAMING_SNAKE → camelCase. return entry .toLowerCase() .replace(/_([a-z])/g, (_, c: string) => c.toUpperCase()); } /** Entries per page of the side-effect list. */ const SIDE_EFFECTS_PAGE_SIZE = 50; interface SideEffectPageParam { /** The window start, fixed when the first page loaded. */ from: string; /** Exclusive upper bound: just after the oldest entry already shown. */ to: string; } export function SideEffectsSection({ medicationId }: SideEffectsSectionProps) { const { t } = useTranslations(); const { canWriteDomain, canManageDomain } = useRecordCapabilities(); const canAddSideEffect = canWriteDomain("medications"); const canManageMedications = canManageDomain("medications"); const fmt = useFormatters(); const queryClient = useQueryClient(); const [open, setOpen] = useState(false); const [category, setCategory] = useState("GI"); const [entry, setEntry] = useState(null); const [severity, setSeverity] = useState<1 | 2 | 3 | 4 | 5 | null>(null); const [notes, setNotes] = useState(""); const [formError, setFormError] = useState(null); // v1.4.27 R4 RC2 — sticky-pin the Save / Cancel row by portalling it // into the `` footer slot. The submit button keeps // its `
` association via the HTML `form` attribute. const [footerEl, setFooterEl] = useState(null); const [pendingDeleteId, setPendingDeleteId] = useState(null); const formId = useId(); const listKey = queryKeys.medicationSideEffects(medicationId); // The last 30 days, newest first, one page at a time. The window is fixed // when the first page loads; each further page asks for the entries before // the oldest one shown (`to` is exclusive, so the boundary instant is // re-asked one millisecond later and the overlap is dropped by id). const { data, isLoading, isError, refetch, fetchNextPage, hasNextPage, isFetchingNextPage, } = useInfiniteQuery({ queryKey: listKey, initialPageParam: null as SideEffectPageParam | null, queryFn: async ({ pageParam }) => { const to = pageParam ? new Date(pageParam.to) : new Date(); const from = pageParam ? new Date(pageParam.from) : new Date(to.getTime() - 30 * 24 * 60 * 60 * 1000); const url = `/api/medications/${medicationId}/side-effects?from=${encodeURIComponent(from.toISOString())}&to=${encodeURIComponent(to.toISOString())}&limit=${SIDE_EFFECTS_PAGE_SIZE}`; const { data: payload } = await apiFetchEnvelope<{ items: SideEffectRow[]; meta: { total: number }; }>(url); return { ...payload, from: from.toISOString() }; }, getNextPageParam: (lastPage): SideEffectPageParam | undefined => { if (lastPage.items.length < SIDE_EFFECTS_PAGE_SIZE) return undefined; const oldest = lastPage.items[lastPage.items.length - 1]; return { from: lastPage.from, to: new Date(Date.parse(oldest.occurredAt) + 1).toISOString(), }; }, staleTime: 30 * 1000, }); const createMutation = useMutation({ mutationFn: async (payload: { entry: MedicationSideEffectEntry; severity: number; notes: string | null; }) => { // v1.4.25 W21 Fix-N (code-M6) — drop client-side `category` // from the wire payload. The server derives it from the entry // via `categoryForEntry`. Backwards-compatible with older // clients during the cut window (route accepts both shapes). return apiPost( `/api/medications/${medicationId}/side-effects`, payload, ); }, onSuccess: () => { void queryClient.invalidateQueries({ queryKey: listKey }); }, }); const deleteMutation = useMutation({ mutationFn: async (logId: string) => { await apiDelete(`/api/medications/${medicationId}/side-effects/${logId}`); return true; }, onSuccess: () => { void queryClient.invalidateQueries({ queryKey: listKey }); }, // v1.16.4 — a failed delete used to fail silently, leaving the row // in place with no signal that the request was rejected. onError: (failure) => { showErrorToast(t("medications.sideEffects.deleteError"), { cause: failure, }); }, }); const items = useMemo(() => { const seen = new Set(); const out: SideEffectRow[] = []; for (const page of data?.pages ?? []) { for (const row of page.items) { if (seen.has(row.id)) continue; seen.add(row.id); out.push(row); } } return out; }, [data]); const filteredEntries = useMemo( () => entriesByCategory(category), [category], ); const { getRadioProps: getEntryRadioProps } = useRovingRadioGroup({ count: filteredEntries.length, selectedIndex: entry ? filteredEntries.indexOf(entry) : -1, onSelect: (index) => setEntry(filteredEntries[index]!), }); const { getRadioProps: getSeverityRadioProps } = useRovingRadioGroup({ count: SIDE_EFFECT_SEVERITY_LADDER.length, selectedIndex: severity !== null ? severity - 1 : -1, onSelect: (index) => setSeverity((index + 1) as 1 | 2 | 3 | 4 | 5), }); function resetForm() { setCategory("GI"); setEntry(null); setSeverity(null); setNotes(""); setFormError(null); } function handleSubmit(e: React.FormEvent) { e.preventDefault(); setFormError(null); if (!entry) { setFormError(t("medications.sideEffects.errorEntry")); return; } if (!severity) { setFormError(t("medications.sideEffects.errorSeverity")); return; } // Sanity-check the entry still belongs to the picked category; // the server derives the category authoritatively from `entry`. void categoryForEntry(entry); createMutation.mutate( { entry, severity, notes: notes.trim() ? notes.trim().slice(0, NOTES_MAX) : null, }, { onSuccess: () => { resetForm(); setOpen(false); }, onError: (err) => { setFormError(err instanceof Error ? err.message : String(err)); }, }, ); } const headerExtras = ( <> {items.length > 0 && ( ({items.length}) )} {/* v1.36.x — noting a side effect is an admitted delegated write. Removing one that is already noted is not. */} {canAddSideEffect && ( )} ); return ( {isLoading && (
{t("medications.sideEffects.loading")}
)} {!isLoading && isError && ( void refetch()} /> )} {!isLoading && !isError && items.length === 0 && (

{t("medications.sideEffects.emptyState")}

)} {!isLoading && !isError && items.length > 0 && (

{t("medications.sideEffects.recentTitle")}

    {items.map((row) => (
  • {/* v1.4.28 FB-F2 — fixed-width date slot pinned to the right of the row meta so the date stamp keeps its column across rows. The category badge + entry label + severity chip share the left-most slot; the date claims a 4ch-wide right column. */}
    {t( `medications.sideEffects.categories.${categoryI18nKey(row.category)}`, )} {t( `medications.sideEffects.entries.${entryI18nKey(row.entry)}`, )} {t( `medications.sideEffects.severity.${severityLikertLabel(row.severity as 1 | 2 | 3 | 4 | 5)}`, )}

    {fmt.dateShortSmart(new Date(row.occurredAt))}

    {row.notes && (

    {row.notes}

    )}
    {canManageMedications && ( )}
  • ))}
{hasNextPage ? (
) : null}
)} {/* Themed confirm — replaces the native window.confirm so the delete gate matches every other destructive action in the app. */} { if (!o) setPendingDeleteId(null); }} > {t("medications.sideEffects.deleteConfirmTitle")} {t("medications.sideEffects.deleteConfirmBody")} {t("common.cancel")} { e.preventDefault(); if (pendingDeleteId) deleteMutation.mutate(pendingDeleteId); setPendingDeleteId(null); }} disabled={deleteMutation.isPending} aria-busy={deleteMutation.isPending || undefined} className="bg-destructive dark:bg-destructive/60 hover:bg-destructive/90 text-white" > {t("medications.sideEffects.deleteCta")} } >
{filteredEntries.map((candidate, index) => { const selected = candidate === entry; return ( ); })}
{SIDE_EFFECT_SEVERITY_LADDER.map((label, idx) => { const value = (idx + 1) as 1 | 2 | 3 | 4 | 5; const selected = severity === value; return ( ); })}