import { useAuthServiceContext } from '../../../services/AuthService'; import { useFhirLocations } from '../../../hooks/useFhirLocations'; import { readPhysicalRooming, usesPhysicalRooming } from '../../../utils/practiceRooming'; /** * Whether this practice rooms patients: its own answer, or unset, whether it has any rooms. Undefined * while an unset practice's locations are still being read, so a label can wait rather than flip. */ export const usePracticeRoomingAnswer = (): boolean | undefined => usePracticeRoomingRead().answer; /** The answer, and whether reading the rooms failed, so a waiting label can stop waiting and offer a retry. */ export const usePracticeRoomingRead = (): { answer: boolean | undefined; failed: boolean; retry: () => void } => { const { wpOrganization } = useAuthServiceContext(); const { locationsBundle, locationsList, loading, error, loadLocations } = useFhirLocations(); const organization = wpOrganization?.current; const answer = readPhysicalRooming(organization) === undefined && !locationsBundle ? undefined : usesPhysicalRooming(organization, locationsList ?? []); return { answer, failed: answer === undefined && Boolean(error) && !loading, retry: () => void loadLocations(true), }; }; /** The same answer, keeping today's rooming screens until it is known rather than flashing them away. */ export const usePracticeRooming = (): boolean => usePracticeRoomingAnswer() ?? true;