-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathNotificationContext.tsx
More file actions
36 lines (31 loc) · 1.32 KB
/
Copy pathNotificationContext.tsx
File metadata and controls
36 lines (31 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
'use client';
import { createContext, useContext, type ReactNode } from 'react';
import { useNotifications, type UseNotificationsReturn } from '@/hooks/useNotifications';
import { useBatchNotificationWatcher } from '@/hooks/useBatchNotificationWatcher';
const NotificationContext = createContext<UseNotificationsReturn | null>(null);
/**
* Cross-page background watcher. Mounted once here (inside the provider, so it
* runs on every route) it is the single dispatcher of batch.completed and
* blocker.created — making those notifications fire even when the execution
* page is unmounted. See issue #652.
*/
function BackgroundBatchWatcher({ addNotification }: { addNotification: UseNotificationsReturn['addNotification'] }) {
useBatchNotificationWatcher(addNotification);
return null;
}
export function NotificationProvider({ children }: { children: ReactNode }) {
const value = useNotifications();
return (
<NotificationContext.Provider value={value}>
<BackgroundBatchWatcher addNotification={value.addNotification} />
{children}
</NotificationContext.Provider>
);
}
export function useNotificationContext(): UseNotificationsReturn {
const ctx = useContext(NotificationContext);
if (!ctx) {
throw new Error('useNotificationContext must be used inside <NotificationProvider>');
}
return ctx;
}