import { createContext, useContext } from "react"; import type { PatientReferral } from "../../../utils/patientReferrals.ts"; export type PatientHomeReferrals = { /** The patient's referrals once read; `undefined` while loading or after the read failed. */ referrals: PatientReferral[] | undefined; failed: boolean; markBooked: (referralId: string) => Promise; declineInvite: (referralId: string) => Promise; }; // Unprovided means no referral read on this surface, so it adds nothing rather than withholding counts. const NONE: PatientHomeReferrals = { referrals: [], failed: false, markBooked: async () => {}, declineInvite: async () => {} }; export const PatientHomeReferralsContext = createContext(NONE); export const usePatientHomeReferrals = (): PatientHomeReferrals => useContext(PatientHomeReferralsContext); /** A referral still waits on the patient until it is booked. */ export const openReferrals = (referrals: PatientReferral[]): PatientReferral[] => referrals.filter((referral) => referral.stage !== "booked");