import { useCallback, useMemo, useState } from 'react'; import type { ReceptionistAppointmentStatus } from '../ReceptionistHomePage/receptionistAppointmentStatus'; export type AppointmentStatusHold = { kind: 'unknown' | 'stale'; requested: ReceptionistAppointmentStatus; basedOn: string; }; /** * A status change nobody could confirm, or one refused because the appointment changed: nothing is chosen for * that appointment again until a fresh read settles it. Kept per appointment, so looking at another one in * between never lets go of it. */ export const useAppointmentStatusHolds = (selectedId: string | undefined) => { const [holds, setHolds] = useState>({}); const selectedHold = selectedId ? holds[selectedId] : undefined; const held = useMemo( () => (selectedId && selectedHold ? { id: selectedId, ...selectedHold } : undefined), [selectedHold, selectedId], ); const hold = useCallback((id: string, value: AppointmentStatusHold) => setHolds((current) => ({ ...current, [id]: value })), []); const release = useCallback((id: string) => setHolds(({ [id]: _released, ...rest }) => rest), []); return { held, hold, release }; };