diff --git a/src/components/Dashboard/context/NotificationContext.jsx b/src/components/Dashboard/context/NotificationContext.jsx index 28982397..a65380ee 100644 --- a/src/components/Dashboard/context/NotificationContext.jsx +++ b/src/components/Dashboard/context/NotificationContext.jsx @@ -3,7 +3,8 @@ import { useState, useContext, useCallback, - useEffect + useEffect, + useRef } from 'react' import { useLocation } from 'react-router-dom' import { notification, Drawer } from 'antd' @@ -59,6 +60,13 @@ const NotificationProvider = ({ children }) => { } }, [authenticated, fetchNotificationsApi, showError]) + const fetchNotificationsRef = useRef(fetchNotifications) + const authenticatedRef = useRef(authenticated) + const connectedRef = useRef(connected) + fetchNotificationsRef.current = fetchNotifications + authenticatedRef.current = authenticated + connectedRef.current = connected + const markNotificationAsRead = useCallback( async (notificationId) => { try { @@ -116,14 +124,15 @@ const NotificationProvider = ({ children }) => { // Initial load / when we become authenticated and connected useEffect(() => { if (!authenticated || !connected) return - fetchNotifications() - }, [authenticated, connected, fetchNotifications]) + fetchNotificationsRef.current() + }, [authenticated, connected]) // Refresh when the notification center opens useEffect(() => { - if (!notificationCenterVisible || !authenticated || !connected) return - fetchNotifications() - }, [notificationCenterVisible, authenticated, connected, fetchNotifications]) + if (!notificationCenterVisible) return + if (!authenticatedRef.current || !connectedRef.current) return + fetchNotificationsRef.current() + }, [notificationCenterVisible]) useEffect(() => { setNotificationCenterVisible(false)