import React, { useMemo, useState } from 'react'; import type { Encounter } from 'fhir/r4'; import { availableActions, type ActionDescriptor, type QueueItem } from './encounterLifecycle'; import { useEncounterActions } from './useEncounterActions'; import { fmtTime, groupQueueByBucket, queueBucketsFor, QueueTaskLine } from './EncounterQueue'; import { BTN_CLASS } from './queueItemDisplay'; import { RoomPickerDialog } from './RoomPickerDialog'; import type { PatientTaskCount } from '../../../utils/patientTaskCount'; import { resolveAppointmentPatientLabel, type PatientNameResolution } from '../../../utils/appointmentParticipants'; import { EmptyState } from '../../../components/ui/empty-state'; interface Props { items: QueueItem[]; loading: boolean; taskCountByRowKey?: Record; patientNames?: PatientNameResolution; onSelect: (item: QueueItem) => void; onChanged?: () => void; onEnter: (encounter: Encounter, item: QueueItem) => void; /** Whether this practice rooms patients. */ rooming?: boolean; } // The rooming day beside its count cards: who is waiting to be roomed and who is in a room, each row // carrying its own next step, so nothing is opened for the person until they choose a patient. export const RoomingDayPanel: React.FC = ({ items, loading, taskCountByRowKey = {}, patientNames, onSelect, onChanged, onEnter, rooming: roomsInUse = true, }) => { const actions = useEncounterActions({ onChanged, onEnter }); const [rooming, setRooming] = useState(); const buckets = useMemo(() => queueBucketsFor('support', roomsInUse).filter((b) => b.key === 'to-room' || b.key === 'roomed'), [roomsInUse]); const byBucket = useMemo(() => groupQueueByBucket(items, queueBucketsFor('support'), 'support'), [items]); if (loading && items.length === 0) return null; const nameOf = (item: QueueItem): string => (item.appointment && patientNames ? resolveAppointmentPatientLabel(item.appointment, patientNames) : undefined) ?? item.patientName ?? 'Unknown patient'; const run = (item: QueueItem, a: ActionDescriptor) => { switch (a.kind) { case 'room': setRooming(item); return; case 'mark-ready': actions.markReady(item); return; case 'open-chart': case 'preview': actions.openChart(item); return; } }; return (
{items.length === 0 ? ( ) : buckets.map((bucket) => (

{bucket.title}

{byBucket[bucket.key].length === 0 ? : (
    {byBucket[bucket.key].map((item) => { const busy = actions.busyKey === item.key; return (
  • {fmtTime(item.atISO)} {item.reason ?? 'No reason given'}{item.room ? ` ยท Room ${item.room}` : ''} {availableActions(item.status, 'support', Boolean(item.room), roomsInUse).map((a) => ( ))}
  • ); })}
)}
))} { if (!open) setRooming(undefined); }} patientName={rooming ? nameOf(rooming) : 'patient'} onPick={(room) => { if (rooming) actions.roomPatient(rooming, room); setRooming(undefined); }} />
); }; export default RoomingDayPanel;