import { useEffect, useState } from "react"; import type { Patient } from "fhir/r4"; import { usePatientServiceContext, useSurfaceExpectedPatient } from "../../../services"; import ReceptionistPatientWorkspaceDialog from "./ReceptionistPatientWorkspaceDialog.tsx"; interface FrontDeskPatientQuickViewProps { /** The patient this quick view is for. The parent keys the component by it. */ patientId: string; /** A record the caller already holds for this id, such as one just registered. */ known?: Patient; onClose: () => void; onBack?: () => void; backLabel?: string; } type Loaded = { id: string; patient?: Patient; error?: string }; // Answers are stamped with the id asked for; one for any other patient is never shown or put in context. export default function FrontDeskPatientQuickView({ patientId, known, onClose, onBack, backLabel }: FrontDeskPatientQuickViewProps) { const { fetchPatient, setPatientState } = usePatientServiceContext(); const declareExpectedPatient = useSurfaceExpectedPatient(); const [loaded, setLoaded] = useState( known?.id === patientId ? { id: patientId, patient: known } : undefined, ); useEffect(() => { if (known?.id === patientId) { declareExpectedPatient(patientId); setPatientState(known); return; } let current = true; fetchPatient(patientId, false, undefined, true) .then((patient) => { if (!current || patient?.id !== patientId) { if (current) setLoaded({ id: patientId, error: "Failed to load the patient profile." }); return; } setLoaded({ id: patientId, patient }); declareExpectedPatient(patientId); setPatientState(patient); }) .catch((error) => { if (!current) return; console.error("Failed to load the patient quick view:", error); setLoaded({ id: patientId, error: "Failed to load the patient profile." }); }); return () => { current = false; }; // Keyed by patient: the id never changes over this component's life. // eslint-disable-next-line react-hooks/exhaustive-deps }, [patientId]); const mine = loaded?.id === patientId ? loaded : undefined; return ( { if (!open) onClose(); }} onBack={onBack} backLabel={backLabel} onPatientUpdated={(updated) => { if (updated.id !== patientId) return; setLoaded({ id: patientId, patient: updated }); setPatientState(updated); }} /> ); }