import { useAuthServiceContext } from '../services'; import { useViewerScope } from './useViewerScope'; import { isPatientExperience } from '../utils/mountContext'; // The whole scope a clinical care team read answers: who is reading, at which practice or at none, about // which patient. Read state, the published list and every writer to it carry this, so nothing read for one // scope is shown in, or written into, another. export const careTeamReadScope = (viewer: string, organizationId: string | undefined, patientId: string | undefined): string => `${viewer}#${organizationId ?? 'neutral'}#${patientId ?? ''}`; const UNRESOLVED = '#unresolved#'; /** A scope no read may be sent for yet: the practice is still resolving, or a staff door holds none. */ export const careTeamScopeAdmitted = (scope: string): boolean => !scope.includes(UNRESOLVED); // A practice not yet resolved is not the neutral host, and a staff door never reads neutrally: only a patient or // proxy session whose practice check answered none reads with no practice. export const useCareTeamReadScope = (patientId: string | undefined): string => { const viewer = useViewerScope(); const { currentOrganization, organizationCheckComplete } = useAuthServiceContext(); if (!currentOrganization?.id && (!organizationCheckComplete || !isPatientExperience())) { return viewer + UNRESOLVED + (patientId ?? ''); } return careTeamReadScope(viewer, currentOrganization?.id, patientId); };