import type { RelatedPerson } from "fhir/r4"; import { splitTeamsByOwner, type TeamWithMembers } from "../../../utils/collaborationTeamSides"; /** * The quiet quantity beside three doors. Each number comes from the list its destination page reads, * and stays undefined until that list has answered: no number is better than a wrong zero. */ export interface PatientHomeDoorCounts { practices?: number; family?: number; collaboration?: number; } export const doorQuantity = (doorId: string, counts: PatientHomeDoorCounts): string | undefined => { const count = counts[doorId as keyof PatientHomeDoorCounts]; // A practice is listed only once the patient has booked and authorized it, so a new patient's true zero sat // under the header of the practice whose portal they had just joined. My practices says why; the door waits. if (doorId === "practices" && count === 0) return undefined; return count === undefined ? undefined : String(count); }; // Both tabs of Family & caregivers, each deduplicated the way the page lists it: who can access me, and the // people I help. Unknown until each has answered, so a half-read count never shows. export const familyCount = (source: { patientId: string | undefined; answeredFor: string | undefined; loading: boolean; error: string | undefined; relatedPersons: RelatedPerson[]; helping?: { loading: boolean; error?: unknown; subjects: string[] }; }): number | undefined => { const { patientId, answeredFor, loading, error, relatedPersons, helping } = source; if (!patientId || answeredFor !== patientId || loading || error) return undefined; if (helping && (helping.loading || helping.error)) return undefined; return new Set(relatedPersons.map((person) => person.id)).size + new Set(helping?.subjects ?? []).size; }; // The patient's own teams that are still open; a dissolved team is not counted anywhere. export const ownTeamCount = (source: { patientId: string | undefined; openTeams: TeamWithMembers[]; loading: boolean; error: string | null; }): number | undefined => { const { patientId, openTeams, loading, error } = source; if (!patientId || loading || error) return undefined; return splitTeamsByOwner(openTeams, patientId, []).mine.length; };