From 98018ca590975c82ca202a313d61feea5d2b4819 Mon Sep 17 00:00:00 2001 From: Mo Tarbin Date: Sat, 10 Jan 2026 14:14:45 -0500 Subject: [PATCH] fix: update SSE message handler to reflect latest userProfile changes there was issue where userprofile was undefined cause the message being display for same user how make the action. The new useEffect hook updates the EventSource's handler whenever handleSSEMessage changes --- src/hooks/useSSE.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/hooks/useSSE.js b/src/hooks/useSSE.js index cb381b9..2729d06 100644 --- a/src/hooks/useSSE.js +++ b/src/hooks/useSSE.js @@ -18,6 +18,7 @@ const CIRCUIT_BREAKER_RESET_TIME = 600000 // 10 minutes export const useSSE = () => { const { data: userProfile } = useUserProfile() + const { isAuthenticated, token } = useAuth() const [connectionState, setConnectionState] = useState(SSE_STATES.CLOSED) const [lastEvent, setLastEvent] = useState(null) @@ -96,6 +97,8 @@ export const useSSE = () => { case 'chore.completed': case 'chore.status': case 'chore.skipped': { + console.log('userProfile: ', userProfile, eventData.data.user) + if (eventData?.data?.user?.id !== userProfile?.id) { showNotification({ type: 'info', @@ -253,7 +256,7 @@ export const useSSE = () => { return // Stop processing if JSON parsing fails } }, - [queryClient, showNotification, showError], + [queryClient, showNotification, showError, userProfile], ) const stopHeartbeatMonitor = useCallback(() => { @@ -570,6 +573,14 @@ export const useSSE = () => { } }, [stopHeartbeatMonitor]) + // Update EventSource message handler when handleSSEMessage changes (e.g., when userProfile loads) + useEffect(() => { + if (eventSourceRef.current && eventSourceRef.current.readyState === SSE_STATES.OPEN) { + console.log('SSE: Updating message handler with latest userProfile') + eventSourceRef.current.onmessage = handleSSEMessage + } + }, [handleSSEMessage]) + // Handle visibility changes for better performance useEffect(() => { const handleVisibilityChange = () => {