import { useEffect, useState } from "react"; import { AlertCircle, Loader2 } from "lucide-react"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "../../../components/ui/dialog.tsx"; import { Alert, AlertDescription } from "../../../components/ui/alert.tsx"; import { Button } from "../../../components/ui/button.tsx"; import { Input } from "../../../components/ui/input.tsx"; import { Label } from "../../../components/ui/label.tsx"; import { priorityLabel } from "../../../utils/fhirStatusVocabulary.ts"; import { ToggleGroupControl, ToggleGroupControlOption } from "../../../components/ui/toggle-group-control.tsx"; import { relayServerFailure } from "../../../utils/fetchFailureMessage"; import type { DiagnosticOrderQueueRow, OutsideRead, ReadElsewhere } from "../../../hooks/useDiagnosticOrderQueue.ts"; import { useImagingReadHandoff, type ImagingReader } from "../../../hooks/useImagingReadHandoff.ts"; import { SEP } from "./ImagingWorklist.tsx"; import "./RecordRefusalDialog.css"; // SENDING A STUDY TO ANOTHER PRACTICE TO BE READ. On Akashic, the images stay where they are, the reader // is let in to this one study, and its report comes back to this order. Off Akashic, nothing is sent by // Akashic at all: staff record where it is going and how, get it there themselves, and record later what // the reader said. const METHODS: { value: OutsideRead["method"]; label: string }[] = [ { value: "disc", label: "A disc" }, { value: "courier", label: "Courier" }, { value: "their-portal", label: "Their own upload portal" }, { value: "other", label: "Another way (say in the note)" }, ]; const SendForReadingDialog = ({ row, readers, onClose, onSent, onSentOutside, }: { row?: DiagnosticOrderQueueRow; readers: ImagingReader[]; onClose: () => void; onSent: (orderId: string, route: ReadElsewhere) => void; onSentOutside?: (orderId: string, outside: OutsideRead) => void; }) => { const { sendForReading, sendForOutsideRead } = useImagingReadHandoff(); const [where, setWhere] = useState<"akashic" | "outside">(readers.length ? "akashic" : "outside"); const [readerId, setReaderId] = useState(""); const [outside, setOutside] = useState({ reader: "", contact: "", method: "" as OutsideRead["method"] | "", note: "" }); const [busy, setBusy] = useState(false); const [error, setError] = useState(); useEffect(() => { setReaderId(readers[0]?.readerId ?? ""); setWhere(readers.length ? "akashic" : "outside"); setError(undefined); }, [row, readers]); if (!row?.orderId) return null; const chosen = readers.find((each) => each.readerId === readerId); const name = chosen?.reader ?? "the reader"; const instances = row.images?.instances ?? 0; const outsideName = outside.reader.trim(); const outsideReady = Boolean(outsideName && outside.method); const submit = async () => { setBusy(true); setError(undefined); try { if (where === "outside") { const response = await sendForOutsideRead(row.orderId as string, { reader: outsideName, method: outside.method as OutsideRead["method"], ...(outside.contact.trim() ? { contact: outside.contact.trim() } : {}), ...(outside.note.trim() ? { note: outside.note.trim() } : {}), }); if (response?.outsideRead?.sentAt) onSentOutside?.(row.orderId as string, response.outsideRead); } else { const response = await sendForReading(row.orderId as string, readerId); // The row changes only on what the server recorded. if (response?.route?.sentAt) onSent(row.orderId as string, response.route); } onClose(); } catch (failure) { console.error("Error sending the study to be read:", failure); setError(relayServerFailure(failure, "The study could not be sent. Please try again.")); } finally { setBusy(false); } }; return ( { if (!open && !busy) onClose(); }}> Send out to be read {[row.accession, row.test ?? "This study", row.patientDisplay].filter(Boolean).join(SEP)} {error && ( {error} )}
{readers.length > 0 && ( setWhere(value === "outside" ? "outside" : "akashic")}> )} {where === "akashic" ? ( <>

{instances === 1 ? "1 image" : instances + " images"}, and the reason it was ordered. The images stay here; {name} is let in to this study only.

) : ( <>

Akashic does not send the images to a practice that is not on Akashic. Get the study to them the way you choose below, with the requisition; then record here what they say once it arrives. Their report comes back to this order.

setOutside({ ...outside, reader: e.target.value })} />
setOutside({ ...outside, contact: e.target.value })} />
setOutside({ ...outside, note: e.target.value })} />
)} {/* The reader works from the order, so what it asks travels with the study either way. */}

{priorityLabel(row.priority) ?? "Not stated on the order"}

{row.question?.trim() || "Not recorded on the order"}

{where === "akashic" ? ( ) : ( )}
); }; export default SendForReadingDialog;