import { useMemo, useState } from 'react'; import { usePatientServiceContext } from '../../services'; import { usePatientSnapshot } from '../../hooks/usePatientSnapshot'; import { SnapshotChart, Spinner } from './SnapshotChart'; /** Format a Date as a `YYYY-MM-DDTHH:mm` string in the user's LOCAL wall-clock time. */ function toLocalInputValue(d: Date): string { const local = new Date(d.getTime() - d.getTimezoneOffset() * 60000); return local.toISOString().slice(0, 16); } /** * Route-level, READ-ONLY "as of {date}" chart reconstruction. * * Reads the patient from context, holds an `asOfLocal` picker value, and renders whatever * `usePatientSnapshot` reconstructs for that instant via the presentational `SnapshotChart`. The * hook is self-driving (re-runs on patient/asOf change) - this view never calls a `run()` and * never puts the hook result in an effect. There are ZERO create/edit/archive/delete affordances * anywhere in this component. */ export function PatientSnapshot() { const { patientState } = usePatientServiceContext(); const nowLocal = useMemo(() => toLocalInputValue(new Date()), []); const [asOfLocal, setAsOfLocal] = useState(() => toLocalInputValue(new Date())); const asOfIso = useMemo(() => { if (!asOfLocal) return undefined; const d = new Date(asOfLocal); return Number.isNaN(d.getTime()) ? undefined : d.toISOString(); }, [asOfLocal]); const formattedAsOf = useMemo(() => { if (!asOfLocal) return ''; const d = new Date(asOfLocal); return Number.isNaN(d.getTime()) ? '' : d.toLocaleString(); }, [asOfLocal]); const { results, running } = usePatientSnapshot(patientState, asOfIso); if (!patientState) { return (
No patient is loaded. Open a patient to view their historical record.
); } return (
{/* Persistent, non-dismissible read-only banner. */}

Historical view — showing this record as of {formattedAsOf || 'an invalid date'}. Read-only.

Very recent times may lag 1–2 minutes behind the live record.

{/* Native date/time picker (no @wordpress DateTimePicker). */}
setAsOfLocal(e.target.value)} className="rounded-md border px-2 py-1 text-sm" /> {running && ( Reconstructing… )}
{/* Sections + rows (+ read-only detail modals) via the shared presentational chart. */} {!asOfIso ? (

Enter a valid date and time to reconstruct the record.

) : results.length === 0 ? (
) : ( )}
); } export default PatientSnapshot;