import { useEffect } from 'react'; import { Patient } from 'fhir/r4'; import { usePatientServiceContext, useWebSocketServiceContext } from '../services'; export const useFhirPatientBroadcast = () => { const { subscribe, unsubscribe } = useWebSocketServiceContext(); const { patientState, setPatientState } = usePatientServiceContext(); useEffect(() => { if (!patientState) { return; } const handlePatientEvent = (cloudEvent: any) => { const patient: Patient = cloudEvent.data; const eventType = cloudEvent.type; if (patientState.id === patient.id) { switch (eventType) { case 'com.akashichealthrecords.fhir.patient.updated': setPatientState(patient); break; } } }; subscribe('com.akashichealthrecords.fhir.patient.updated', handlePatientEvent); return () => { unsubscribe('com.akashichealthrecords.fhir.patient.updated', handlePatientEvent); }; }, [patientState, subscribe, unsubscribe, setPatientState]); };