"use client"; import { CircleCheck, Clock, Pill, Plus } from "lucide-react"; import { type ReactNode, useEffect, useMemo, useState } from "react"; import { AddPrescriptionDialog, type NewPrescription, } from "@/components/prescriptions/add-prescription-dialog"; import { PrescriptionDetailSheet } from "@/components/prescriptions/prescription-detail-sheet"; import { Avatar, AvatarFallback } from "@/components/ui/avatar"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { Card } from "@/components/ui/card"; import { type Prescription, type RxStatus, createPrescription, formatPrescribedAt, listPrescriptions, } from "@/lib/prescriptions"; import { notify } from "@/lib/toast"; export type { Prescription, RxStatus } from "@/lib/prescriptions"; const statusVariant: Record< RxStatus, "default" | "secondary" | "destructive" | "outline" > = { active: "default", completed: "outline", expired: "destructive", }; const statusLabel: Record = { active: "Active", completed: "Completed", expired: "Expired", }; function Kpi({ label, value, icon: Icon, }: { label: string; value: string; icon: typeof Pill; }) { return (
{label} {value}
); } function RxRow({ rx, onOpen }: { rx: Prescription; onOpen: () => void }) { return (
{ if (event.key === "Enter" || event.key === " ") { event.preventDefault(); onOpen(); } }} role="button" tabIndex={0} > {rx.initials}
{rx.medication} {rx.dose && ( · {rx.dose} )} {rx.name} · #{rx.fileNumber} · {rx.frequency}
{rx.prescriber} {formatPrescribedAt(rx.prescribedAt)}
{statusLabel[rx.status]}
); } function Section({ title, description, children, }: { title: string; description?: string; children: ReactNode; }) { return (

{title}

{description && (

{description}

)}
{children}
); } export function PrescriptionsView() { const [addOpen, setAddOpen] = useState(false); const [list, setList] = useState([]); const [selected, setSelected] = useState(null); const [sheetOpen, setSheetOpen] = useState(false); useEffect(() => { let active = true; listPrescriptions() .then((data) => { if (active) setList(data); }) .catch(() => { /* api-client redirects on 401; otherwise leave the list empty */ }); return () => { active = false; }; }, []); const openRx = (rx: Prescription) => { setSelected(rx); setSheetOpen(true); }; // Persist a new prescription, then add the saved record to the top of the list. const addPrescription = async (rx: NewPrescription) => { try { const created = await createPrescription({ fileNumber: rx.fileNumber, name: rx.name, initials: rx.initials, medication: rx.medication, dose: rx.dose, frequency: rx.frequency, duration: rx.duration || null, notes: rx.notes || null, }); setList((prev) => [created, ...prev]); } catch { notify.error("Couldn't add prescription", "Please try again."); } }; const kpis = useMemo( () => [ { label: "Active", value: String(list.filter((r) => r.status === "active").length), icon: Pill, }, { label: "Completed", value: String(list.filter((r) => r.status === "completed").length), icon: CircleCheck, }, { label: "Expired", value: String(list.filter((r) => r.status === "expired").length), icon: Clock, }, ], [list], ); return (

Prescriptions

Medications prescribed across the clinic.

{kpis.map((k) => ( ))}
{list.map((rx) => ( openRx(rx)} rx={rx} /> ))}
); }