Refactor NotificationContext to improve notification fetching logic

- Introduced useEffectEvent for handling notification fetching based on authentication and connection status.
- Simplified the useEffect dependencies for fetching notifications when the notification center is opened.
- Removed unused token from context to streamline the NotificationProvider component.
This commit is contained in:
Tom Butcher 2026-08-10 23:29:49 +01:00
parent 3ed92a141b
commit 5de4c23a71

View File

@ -3,7 +3,8 @@ 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'
@ -19,7 +20,7 @@ const NotificationContext = createContext()
const NotificationProvider = ({ children }) => { const NotificationProvider = ({ children }) => {
const [api, contextHolder] = notification.useNotification() const [api, contextHolder] = notification.useNotification()
const location = useLocation() const location = useLocation()
const { authenticated, token } = useContext(AuthContext) const { authenticated } = useContext(AuthContext)
const { const {
showError, showError,
fetchNotificationsApi, fetchNotificationsApi,
@ -27,7 +28,8 @@ const NotificationProvider = ({ children }) => {
markAllNotificationsAsReadApi, markAllNotificationsAsReadApi,
deleteNotificationApi, deleteNotificationApi,
deleteAllNotificationsApi, deleteAllNotificationsApi,
registerNotificationListener registerNotificationListener,
connected
} = useContext(ApiServerContext) } = useContext(ApiServerContext)
const [notificationCenterVisible, setNotificationCenterVisible] = const [notificationCenterVisible, setNotificationCenterVisible] =
@ -58,6 +60,11 @@ 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 {
@ -112,17 +119,17 @@ const NotificationProvider = ({ children }) => {
const unreadCount = notifications.filter((n) => !n.read).length const unreadCount = notifications.filter((n) => !n.read).length
// Initial load / when we become authenticated and connected
useEffect(() => { useEffect(() => {
if (authenticated == true && notificationCenterVisible == true) { onFetchNotifications()
fetchNotifications() }, [authenticated, connected])
}
}, [authenticated, notificationCenterVisible, fetchNotifications])
// Refresh when the notification center opens
useEffect(() => { useEffect(() => {
if (authenticated && token) { if (notificationCenterVisible) {
fetchNotifications() onFetchNotifications()
} }
}, [authenticated, token, fetchNotifications]) }, [notificationCenterVisible])
useEffect(() => { useEffect(() => {
setNotificationCenterVisible(false) setNotificationCenterVisible(false)