import { isAsOfIncomplete } from '../utils/searchTruncation'; import { useEffect, useState, useCallback } from 'react'; import { usePatientServiceContext, useVisionPrescriptionServiceContext } from '../services'; import { describeFetchFailure } from '../utils/fetchFailureMessage'; import { patientCacheGuard } from '../utils/cacheOwner'; interface UseFhirVisionPrescriptionsOptions { autoLoad?: boolean; errorMessage?: string; forceReload?: boolean; } interface UseFhirVisionPrescriptionsReturn { /** Served as of a cutoff and not in full: what it left out is unknown, never none. */ incomplete: boolean; loading: boolean; error: string | undefined; loadVisionPrescriptions: () => Promise; clearError: () => void; visionPrescriptionsBundle: ReturnType['visionPrescriptionsBundle']; visionPrescriptionsList: ReturnType['visionPrescriptionsList']; } export const useFhirVisionPrescriptions = (options: UseFhirVisionPrescriptionsOptions = {}): UseFhirVisionPrescriptionsReturn => { const { autoLoad = true, errorMessage = "Failed to load vision prescriptions", forceReload = false } = options; const { patientState } = usePatientServiceContext(); const { cachedForPatientId, fetchVisionPrescriptionsWithPolling, visionPrescriptionsBundle, visionPrescriptionsList } = useVisionPrescriptionServiceContext(); const [loading, setLoading] = useState(false); const [error, setError] = useState(); const loadVisionPrescriptions = useCallback(async () => { if (!patientState) { return; } setLoading(true); setError(undefined); try { await fetchVisionPrescriptionsWithPolling(patientState); } catch (err) { console.error('Failed to load vision prescriptions:', err); setError(describeFetchFailure(err, errorMessage)); } finally { setLoading(false); } }, [patientState, fetchVisionPrescriptionsWithPolling, errorMessage]); const clearError = useCallback(() => { setError(undefined); }, []); useEffect(() => { if (!autoLoad) return; if (!patientState) return; // A bundle belonging to SOMEBODY ELSE is not a reason not to ask. This used to fire only when // there was no bundle at all, so moving between two charts left the previous patient's list in // place, with no request and nothing on screen to say it was not theirs. // NOT a refetch on every navigation: same patient, bundle loaded, no request. if (!visionPrescriptionsBundle || cachedForPatientId !== patientState.id || forceReload) { loadVisionPrescriptions(); } }, [autoLoad, patientState, visionPrescriptionsBundle, cachedForPatientId, forceReload, loadVisionPrescriptions]); // The cache outlives a change of patient: hand out only the patient asked about. const guard = patientCacheGuard(cachedForPatientId, patientState?.id); return { loading: guard.loading(loading, Boolean(error)), error, loadVisionPrescriptions, clearError, // Served as of a cutoff and not in full: what it left out is unknown, never none. incomplete: isAsOfIncomplete(guard.bundle(visionPrescriptionsBundle, null)), visionPrescriptionsBundle: guard.bundle(visionPrescriptionsBundle, null), visionPrescriptionsList: guard.list(visionPrescriptionsList) }; };