import { useContext, useMemo } from "react"; import { useFhirRelatedPersons } from "../../../hooks/useFhirRelatedPersons"; import { useMyProxies } from "../../../hooks/useMyProxies"; import { usePatientServiceContext, useRelatedPersonServiceContext } from "../../../services"; import { PatientInboxContext } from "../../../components/PatientInbox/patientInboxContext"; import { usePracticeSwitcher } from "../../../components/PracticeSwitcher/PracticeSwitcherProvider"; import { familyCount, ownTeamCount, type PatientHomeDoorCounts } from "./patientHomeDoorCounts"; // Practices and teams are already loaded by the frame's providers; related persons share the // Family & caregivers page's cached search. export const usePatientHomeDoorCounts = (): PatientHomeDoorCounts => { const { practiceCount } = usePracticeSwitcher(); const inbox = useContext(PatientInboxContext); const { patientState } = usePatientServiceContext(); const { cachedForPatientId } = useRelatedPersonServiceContext(); const { loading, error, relatedPersonsList } = useFhirRelatedPersons(); const { proxies, isLoading: helpingLoading, error: helpingError } = useMyProxies(); const patientId = patientState?.id; return useMemo( () => ({ practices: practiceCount, family: familyCount({ patientId, answeredFor: cachedForPatientId, loading, error, relatedPersons: relatedPersonsList, helping: { loading: helpingLoading, error: helpingError, subjects: proxies.map((proxy) => proxy.subjectPatientId) }, }), collaboration: inbox ? ownTeamCount({ patientId, openTeams: inbox.openTeams, loading: inbox.teamsLoading, error: inbox.teamsError, }) : undefined, }), [practiceCount, patientId, cachedForPatientId, loading, error, relatedPersonsList, helpingLoading, helpingError, proxies, inbox], ); };