"use client"; import { useEffect, useState } from "react"; import { useTranslation } from "react-i18next"; import { Button } from "@/components/ui/button"; import { Dialog, DialogClose, DialogDescription, DialogFooter, DialogHeader, DialogPanel, DialogPopup, DialogTitle, } from "@/components/ui/dialog"; import { ROLE_LABELS } from "@/lib/access"; import { type Patient, transferPatient } from "@/lib/patients"; import { listProviders, type Provider } from "@/lib/staff"; import { notify } from "@/lib/toast"; const selectClass = "h-9 w-full rounded-3xl border border-transparent bg-input/50 px-3 text-sm outline-none focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/30"; type Props = { patient: Patient; open: boolean; onOpenChange: (open: boolean) => void; onTransferred: (patient: Patient) => void; }; // Reassign a patient to another clinician. The new provider becomes the // patient's primary provider (and PCP label), which moves the chart into their // panel under per-doctor visibility. export function TransferPatientDialog({ patient, open, onOpenChange, onTransferred, }: Props) { const { t } = useTranslation(); const [providers, setProviders] = useState([]); const [providerId, setProviderId] = useState(patient.primaryProviderId ?? ""); const [submitting, setSubmitting] = useState(false); const [error, setError] = useState(null); useEffect(() => { if (!open) return; setProviderId(patient.primaryProviderId ?? ""); setError(null); let active = true; listProviders() .then((list) => active && setProviders(list)) .catch(() => active && setProviders([])); return () => { active = false; }; }, [open, patient.primaryProviderId]); const submit = async () => { if (!providerId || submitting) return; setSubmitting(true); setError(null); try { const updated = await transferPatient(patient.fileNumber, providerId); onTransferred(updated); notify.success( t("patients.transfer.successTitle"), t("patients.transfer.successBody", { name: updated.name, provider: updated.pcp, }), ); onOpenChange(false); } catch (err) { const message = err instanceof Error ? err.message : t("patients.transfer.error"); setError(message); notify.error(t("patients.transfer.errorTitle"), message); } finally { setSubmitting(false); } }; return ( {t("patients.transfer.title")} {t("patients.transfer.description", { name: patient.name })} {error && (

{error}

)}
}> {t("patients.transfer.cancel")}
); }