import { createContext, useContext } from "react"; import type { CollaborationTeam, TeamMembership } from "../../services/collaborationTeam"; import type { TeamMessageSummary } from "../../utils/collabTeamSummaries"; // `messagingRefusal`: why this viewer may read the thread but not write in it. export type PatientInboxTeam = CollaborationTeam & { members: TeamMembership[]; messagingRefusal?: string }; export interface PatientInboxValue { teams: PatientInboxTeam[]; openTeams: PatientInboxTeam[]; teamsLoading: boolean; teamsError: string | null; teamSummaries: Map; inboxUnread: number; reloadTeams: () => Promise; setOpenTeamId: (teamId: string | null) => void; /** A team as a lifecycle action just answered it: shown at once, ahead of any list read started before it. */ noteTeam: (team: CollaborationTeam) => void; } // Kept apart from the provider so the header reads the number without importing the services. export const PatientInboxContext = createContext(undefined); const NO_INBOX: PatientInboxValue = { teams: [], openTeams: [], teamsLoading: false, teamsError: null, teamSummaries: new Map(), inboxUnread: 0, reloadTeams: async () => undefined, setOpenTeamId: () => undefined, noteTeam: () => undefined, }; export const usePatientInbox = (): PatientInboxValue => useContext(PatientInboxContext) ?? NO_INBOX; export const usePatientInboxUnread = (): number => usePatientInbox().inboxUnread;