import { isAsOfIncomplete } from '../utils/searchTruncation'; import { useEffect, useState, useCallback } from 'react'; import { usePatientServiceContext, useDeviceUseStatementServiceContext } from '../services'; import { describeFetchFailure } from '../utils/fetchFailureMessage'; import { patientCacheGuard } from '../utils/cacheOwner'; interface UseFhirDeviceUseStatementsOptions { autoLoad?: boolean; errorMessage?: string; forceReload?: boolean; } interface UseFhirDeviceUseStatementsReturn { /** Served as of a cutoff and not in full: what it left out is unknown, never none. */ incomplete: boolean; loading: boolean; error: string | undefined; loadDeviceUseStatements: () => Promise; clearError: () => void; deviceUseStatementsBundle: ReturnType['deviceUseStatementsBundle']; deviceUseStatementsList: ReturnType['deviceUseStatementsList']; } export const useFhirDeviceUseStatements = (options: UseFhirDeviceUseStatementsOptions = {}): UseFhirDeviceUseStatementsReturn => { const { autoLoad = true, errorMessage = "Failed to load device use statements", forceReload = false } = options; const { patientState } = usePatientServiceContext(); const { cachedForPatientId, fetchDeviceUseStatementsWithPolling, deviceUseStatementsBundle, deviceUseStatementsList } = useDeviceUseStatementServiceContext(); const [loading, setLoading] = useState(false); const [error, setError] = useState(); const loadDeviceUseStatements = useCallback(async () => { if (!patientState) { return; } setLoading(true); setError(undefined); try { await fetchDeviceUseStatementsWithPolling(patientState); } catch (err) { console.error('Failed to load device use statements:', err); setError(describeFetchFailure(err, errorMessage)); } finally { setLoading(false); } }, [patientState, fetchDeviceUseStatementsWithPolling, 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 (!deviceUseStatementsBundle || cachedForPatientId !== patientState.id || forceReload) { loadDeviceUseStatements(); } }, [autoLoad, patientState, deviceUseStatementsBundle, cachedForPatientId, forceReload, loadDeviceUseStatements]); // 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, loadDeviceUseStatements, clearError, // Served as of a cutoff and not in full: what it left out is unknown, never none. incomplete: isAsOfIncomplete(guard.bundle(deviceUseStatementsBundle, null)), deviceUseStatementsBundle: guard.bundle(deviceUseStatementsBundle, null), deviceUseStatementsList: guard.list(deviceUseStatementsList) }; };