@@ -18,22 +18,11 @@ const NotificationContext = createContext<NotificationContextValue | undefined>(
1818 undefined
1919) ;
2020
21- export const useNotificationContext = ( ) => {
22- const ctx = useContext ( NotificationContext ) ;
23- if ( ! ctx )
24- throw new Error (
25- 'useNotificationContext must be used within NotificationProvider'
26- ) ;
27- return ctx ;
28- } ;
29-
3021export const NotificationProvider = ( { children } : { children : ReactNode } ) => {
3122 const [ permStatus , setPermStatus ] =
3223 useState < PermissionStatus > ( 'undetermined' ) ;
33- const [ token , setToken ] = useState < string | undefined > ( undefined ) ;
34- const [ lastReceived , setLastReceived ] = useState < string | undefined > (
35- undefined
36- ) ;
24+ const [ token , setToken ] = useState < string > ( ) ;
25+ const [ lastReceived , setLastReceived ] = useState < string > ( ) ;
3726
3827 useEffect ( ( ) => {
3928 let active = true ;
@@ -67,12 +56,11 @@ export const NotificationProvider = ({ children }: { children: ReactNode }) => {
6756 } , [ permStatus ] ) ;
6857
6958 useEffect ( ( ) => {
70- if ( permStatus !== 'granted' && permStatus !== 'provisional' ) return ;
7159 const sub = Notifications . addOnNotificationReceived ( ( n ) => {
72- setLastReceived ( `${ n . title ?? '(no title) ' } — ${ n . body ?? '(no body) ' } ` ) ;
60+ setLastReceived ( `Received: ${ n . title ?? '' } — ${ n . body ?? '' } ` ) ;
7361 } ) ;
7462 return ( ) => sub . remove ( ) ;
75- } , [ permStatus ] ) ;
63+ } , [ ] ) ;
7664
7765 useEffect ( ( ) => {
7866 if ( permStatus !== 'granted' && permStatus !== 'provisional' ) return ;
@@ -108,3 +96,12 @@ export const NotificationProvider = ({ children }: { children: ReactNode }) => {
10896 </ NotificationContext . Provider >
10997 ) ;
11098} ;
99+
100+ export const useNotification = ( ) => {
101+ const ctx = useContext ( NotificationContext ) ;
102+ if ( ! ctx )
103+ throw new Error (
104+ 'useNotificationContext must be used within NotificationProvider'
105+ ) ;
106+ return ctx ;
107+ } ;
0 commit comments