// Whose agreement answers these are. A tick, a recorded answer or a held answer belongs to the viewer signed in, the // practice and the person answered for; it is withheld the moment any of them changes, and never comes back. import { useRef } from 'react'; import { viewerEpoch } from '../../utils/writeThroughHold'; /** The account a screen is rendered for, as that render sees it; the global viewer epoch moves an effect later. */ export const renderedViewer = (currentUser: { fhir?: { resourceType?: string; id?: string } } | null | undefined): string => currentUser?.fhir?.id ? `${currentUser.fhir.resourceType ?? ''}/${currentUser.fhir.id}` : ''; export const agreementOwner = ( viewer: string, organizationId: string | undefined, actingAs: { relatedPersonId?: string; subjectPatientId?: string } | null | undefined, subjectPatientId?: string, ): string => [viewerEpoch(), viewer, organizationId ?? '', actingAs?.relatedPersonId ?? '', actingAs?.subjectPatientId ?? '', subjectPatientId ?? ''].join('|'); /** A generation that moves on whenever the owner changes, so returning to an earlier owner starts empty. */ export const useOwnerGeneration = (owner: string): { generation: number; current: () => number } => { const last = useRef(owner); const generation = useRef(0); if (last.current !== owner) { last.current = owner; generation.current += 1; } const current = useRef(() => generation.current); return { generation: generation.current, current: current.current }; }; /** Which practice the agreements are about: none yet resolved, the neutral patients host, or a resolved practice. */ export type AgreementPracticeScope = 'unresolved' | 'neutral' | 'practice'; // Only a patient or helper session whose practice check answered none is neutral; until then the practice is unknown. export const agreementPracticeScope = ( organizationId: string | undefined, organizationCheckComplete: boolean, patientExperience: boolean, ): AgreementPracticeScope => { if (organizationId) return 'practice'; return organizationCheckComplete && patientExperience ? 'neutral' : 'unresolved'; };