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