import { useEffect, useState } from "react"; import { AlertCircle } 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 { Textarea } from "../../../components/ui/textarea.tsx"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "../../../components/ui/select.tsx"; import { mayHaveBeenWritten, relayServerFailure } from "../../../utils/fetchFailureMessage"; import { formatDateTime } from "../../../utils/dateUtils"; import { UnconfirmedResult, type RefusedRequisition, type RequisitionContact } from "../../../hooks/useInboundRequisitions.ts"; export interface ContactDraft { method: "" | "phone" | "fax"; recipient: string; spokeWith: string; outcome: string; } export const BLANK_CONTACT: ContactDraft = { method: "", recipient: "", spokeWith: "", outcome: "" }; /** One id and time per attempt, kept across retries so the server recognises a resend. */ export interface AttemptStamp { id: string; occurredAt: string; } export const newStamp = (): AttemptStamp => ({ id: crypto.randomUUID(), occurredAt: new Date().toISOString() }); export const contactReady = (draft: ContactDraft) => Boolean(draft.method) && draft.outcome.trim().length > 0; export const contactFrom = (draft: ContactDraft, stamp: AttemptStamp): Omit => ({ id: stamp.id, method: draft.method as "phone" | "fax", outcome: draft.outcome.trim(), occurredAt: stamp.occurredAt, ...(draft.recipient.trim() ? { recipient: draft.recipient.trim() } : {}), ...(draft.spokeWith.trim() ? { spokeWith: draft.spokeWith.trim() } : {}), }); // A lost response is unknown, not a failure: the server may have kept it, and a resend is safe. export const failureWording = (failure: unknown, what: string) => (failure instanceof UnconfirmedResult || mayHaveBeenWritten(failure) ? `We could not confirm whether the ${what} was saved. Check the Refused list before trying again; trying again will not record it twice.` : relayServerFailure(failure, `The ${what} was not saved.`)); export const describeContact = (contact: RequisitionContact) => [ `${contact.method === "fax" ? "Fax" : "Phone"} ${formatDateTime(contact.occurredAt)}`, contact.recipient, contact.spokeWith ? `Spoke with ${contact.spokeWith}` : undefined, contact.outcome, ].filter(Boolean).join(" · "); export const ContactFields = ({ draft, onChange, disabled, idPrefix }: { draft: ContactDraft; onChange: (next: ContactDraft) => void; disabled?: boolean; idPrefix: string; }) => { const set = (key: keyof ContactDraft) => (value: string) => onChange({ ...draft, [key]: value }); return (
set("recipient")(event.target.value)} />
set("spokeWith")(event.target.value)} />