import { useEffect } from 'react'; import { Communication, CommunicationRequest } from 'fhir/r4'; import { useWebSocketServiceContext } from '../services'; interface UseCommunicationTargetedBroadcastOptions { communication?: Communication; communicationId?: string; isOpen?: boolean; onUpdate: (communication: Communication) => void; onDelete: () => void; } export const useFhirCommunicationTargetedBroadcast = ({ communication, communicationId, isOpen = true, onUpdate, onDelete }: UseCommunicationTargetedBroadcastOptions) => { const { subscribe, unsubscribe } = useWebSocketServiceContext(); useEffect(() => { if (!communication && !communicationId) { return; } if (!isOpen) { return; } const targetId = communicationId || communication?.id; const handleCommunicationEvent = (cloudEvent: any) => { const communicationPayload: Communication = cloudEvent.data; const eventType = cloudEvent.type; if (communicationPayload.id === targetId) { switch (eventType) { case 'com.akashichealthrecords.fhir.communication.updated': onUpdate(communicationPayload); break; case 'com.akashichealthrecords.fhir.communication.deleted': onDelete(); break; } } }; subscribe('com.akashichealthrecords.fhir.communication.updated', handleCommunicationEvent); subscribe('com.akashichealthrecords.fhir.communication.deleted', handleCommunicationEvent); return () => { unsubscribe('com.akashichealthrecords.fhir.communication.updated', handleCommunicationEvent); unsubscribe('com.akashichealthrecords.fhir.communication.deleted', handleCommunicationEvent); }; }, [communication, communicationId, isOpen, onUpdate, onDelete, subscribe, unsubscribe]); }; interface UseCommunicationRequestTargetedBroadcastOptions { communicationRequest?: CommunicationRequest; communicationRequestId?: string; isOpen?: boolean; onUpdate: (communicationRequest: CommunicationRequest) => void; onDelete: () => void; } export const useFhirCommunicationRequestTargetedBroadcast = ({ communicationRequest, communicationRequestId, isOpen = true, onUpdate, onDelete }: UseCommunicationRequestTargetedBroadcastOptions) => { const { subscribe, unsubscribe } = useWebSocketServiceContext(); useEffect(() => { if (!communicationRequest && !communicationRequestId) { return; } if (!isOpen) { return; } const targetId = communicationRequestId || communicationRequest?.id; const handleCommunicationRequestEvent = (cloudEvent: any) => { const communicationRequestPayload: CommunicationRequest = cloudEvent.data; const eventType = cloudEvent.type; if (communicationRequestPayload.id === targetId) { switch (eventType) { case 'com.akashichealthrecords.fhir.communicationrequest.updated': onUpdate(communicationRequestPayload); break; case 'com.akashichealthrecords.fhir.communicationrequest.deleted': onDelete(); break; } } }; subscribe('com.akashichealthrecords.fhir.communicationrequest.updated', handleCommunicationRequestEvent); subscribe('com.akashichealthrecords.fhir.communicationrequest.deleted', handleCommunicationRequestEvent); return () => { unsubscribe('com.akashichealthrecords.fhir.communicationrequest.updated', handleCommunicationRequestEvent); unsubscribe('com.akashichealthrecords.fhir.communicationrequest.deleted', handleCommunicationRequestEvent); }; }, [communicationRequest, communicationRequestId, isOpen, onUpdate, onDelete, subscribe, unsubscribe]); };