import { useCallback, useEffect, useState } from "react"; import { useHttpService } from "../services/HttpService.tsx"; import { environment } from "../environments/environment.ts"; import { relayServerFailure } from "../utils/fetchFailureMessage"; /** * Every result in this practice that nobody has acknowledged, whoever ordered it. * * WHY IT IS NOT THE CLINICIAN'S OWN LIST. `useResultsAwaitingReview` answers "what is waiting on * ME", scoped on the server to the caller's own requester reference, and an order whose requester * is not a practitioner here cannot appear on it. MEASURED ON STAGING: five of the ten * unacknowledged results were of that kind, including one that arrived as a requisition from an * outside practice -- where the requester names that practice, correctly, because who sent the work * is a fact the order keeps. Walked the same day, the clinician's own list answered zero for a * practice holding two. * * THE BENCH QUEUE DID LIST THEM, so this is not about invisibility. What nothing did was count * them, say how long they had sat, or say which ones no clinician will ever be prompted about. * * `undefined` UNTIL THE FIRST ANSWER ARRIVES, NEVER `[]`, the same call the clinician's list makes. * An empty array would let a screen render "nothing is waiting" over a list that has not loaded, and * on this subject a false all-clear is the entire failure being guarded against. * * THE COUNT COMES FROM THE SERVER rather than being derived here. A screen filtering on a field it * misread would under-report, and under-reporting is the one direction this must not fail in. */ export interface UnreviewedResult { orderId?: string; kind?: "laboratory" | "imaging"; test?: string; priority?: string; authoredOn?: string; patientId?: string; patientDisplay?: string; /** A practitioner here, or the outside practice that sent the requisition in. */ requesterDisplay?: string; /** False means this row is seen here or it is seen nowhere. */ onSomebodysList: boolean; rejection?: { reason?: string; disposition?: string; recorded?: string; note?: string }; } export const useUnreviewedResults = () => { const { get } = useHttpService(); const [results, setResults] = useState(); const [unassigned, setUnassigned] = useState(0); // The server stopped before its last page, so every count here is a floor and none is an all-clear. const [truncated, setTruncated] = useState(false); const [error, setError] = useState(); const load = useCallback(async () => { setError(undefined); try { const found = await get<{ orders?: UnreviewedResult[]; unassigned?: number; truncated?: boolean }>( `${environment.fhirBaseApi}/diagnostic-order/unreviewed`, ); setResults(found?.orders ?? []); setUnassigned(found?.unassigned ?? 0); setTruncated(found?.truncated === true); } catch (failure) { console.error("Error loading unacknowledged results:", failure); setError(relayServerFailure( failure, "Could not check for unacknowledged results. Please try again.", )); } }, [get]); useEffect(() => { void load(); }, [load]); return { results, unassigned, truncated, error, reload: load }; };