Refactor NotificationContext to Streamline Notification Fetching Logic
All checks were successful
farmcontrol/farmcontrol-ui/pipeline/head This commit looks good

- Removed the useEffectEvent for fetching notifications and integrated the fetching logic directly into the useEffect hooks for improved clarity and performance.
- Updated dependency arrays to ensure notifications are fetched correctly based on authentication and connection status, enhancing responsiveness to state changes.
This commit is contained in:
Tom Butcher 2026-08-21 11:09:49 +01:00
parent 0225e229d8
commit 867a9c4e61

View File

@ -3,8 +3,7 @@ import {
useState,
useContext,
useCallback,
useEffect,
useEffectEvent
useEffect
} from 'react'
import { useLocation } from 'react-router-dom'
import { notification, Drawer } from 'antd'
@ -60,11 +59,6 @@ const NotificationProvider = ({ children }) => {
}
}, [authenticated, fetchNotificationsApi, showError])
const onFetchNotifications = useEffectEvent(() => {
if (!authenticated || !connected) return
fetchNotifications()
})
const markNotificationAsRead = useCallback(
async (notificationId) => {
try {
@ -121,15 +115,15 @@ const NotificationProvider = ({ children }) => {
// Initial load / when we become authenticated and connected
useEffect(() => {
onFetchNotifications()
}, [authenticated, connected])
if (!authenticated || !connected) return
fetchNotifications()
}, [authenticated, connected, fetchNotifications])
// Refresh when the notification center opens
useEffect(() => {
if (notificationCenterVisible) {
onFetchNotifications()
}
}, [notificationCenterVisible])
if (!notificationCenterVisible || !authenticated || !connected) return
fetchNotifications()
}, [notificationCenterVisible, authenticated, connected, fetchNotifications])
useEffect(() => {
setNotificationCenterVisible(false)