()
| 36 | // Track current timeout to clear it when immediate notifications arrive |
| 37 | let currentTimeoutId: NodeJS.Timeout | null = null; |
| 38 | export function useNotifications(): { |
| 39 | addNotification: AddNotificationFn; |
| 40 | removeNotification: RemoveNotificationFn; |
| 41 | } { |
| 42 | const store = useAppStateStore(); |
| 43 | const setAppState = useSetAppState(); |
| 44 | |
| 45 | // Process queue when current notification finishes or queue changes |
| 46 | const processQueue = useCallback(() => { |
| 47 | setAppState(prev => { |
| 48 | const next = getNext(prev.notifications.queue); |
| 49 | if (prev.notifications.current !== null || !next) { |
| 50 | return prev; |
| 51 | } |
| 52 | currentTimeoutId = setTimeout((setAppState, nextKey, processQueue) => { |
| 53 | currentTimeoutId = null; |
| 54 | setAppState(prev => { |
| 55 | // Compare by key instead of reference to handle re-created notifications |
| 56 | if (prev.notifications.current?.key !== nextKey) { |
| 57 | return prev; |
| 58 | } |
| 59 | return { |
| 60 | ...prev, |
| 61 | notifications: { |
| 62 | queue: prev.notifications.queue, |
| 63 | current: null |
| 64 | } |
| 65 | }; |
| 66 | }); |
| 67 | processQueue(); |
| 68 | }, next.timeoutMs ?? DEFAULT_TIMEOUT_MS, setAppState, next.key, processQueue); |
| 69 | return { |
| 70 | ...prev, |
| 71 | notifications: { |
| 72 | queue: prev.notifications.queue.filter(_ => _ !== next), |
| 73 | current: next |
| 74 | } |
| 75 | }; |
| 76 | }); |
| 77 | }, [setAppState]); |
| 78 | const addNotification = useCallback<AddNotificationFn>((notif: Notification) => { |
| 79 | // Handle immediate priority notifications |
| 80 | if (notif.priority === 'immediate') { |
| 81 | // Clear any existing timeout since we're showing a new immediate notification |
| 82 | if (currentTimeoutId) { |
| 83 | clearTimeout(currentTimeoutId); |
| 84 | currentTimeoutId = null; |
| 85 | } |
| 86 | |
| 87 | // Set up timeout for the immediate notification |
| 88 | currentTimeoutId = setTimeout((setAppState, notif, processQueue) => { |
| 89 | currentTimeoutId = null; |
| 90 | setAppState(prev => { |
| 91 | // Compare by key instead of reference to handle re-created notifications |
| 92 | if (prev.notifications.current?.key !== notif.key) { |
| 93 | return prev; |
| 94 | } |
| 95 | return { |
no test coverage detected