4
4
useLocalStorage ,
5
5
} from '@chia-network/api-react' ;
6
6
import { orderBy } from 'lodash' ;
7
- import React , { useMemo , useEffect , useCallback , createContext , type ReactNode } from 'react' ;
7
+ import React , { useMemo , useCallback , createContext , type ReactNode } from 'react' ;
8
8
9
9
import type Notification from '../../@types/Notification' ;
10
10
import useBlockchainNotifications from '../../hooks/useBlockchainNotifications' ;
@@ -21,7 +21,7 @@ export const NotificationsContext = createContext<
21
21
isLoading : boolean ;
22
22
error ?: Error ;
23
23
24
- unseenCount : number ;
24
+ seenAt : number | undefined ;
25
25
setAsSeen : ( ) => void ;
26
26
27
27
areNotificationsEnabled : boolean ;
@@ -31,6 +31,8 @@ export const NotificationsContext = createContext<
31
31
32
32
showNotification : ( notification : Notification ) => void ;
33
33
deleteNotification : ( notificationId : string ) => void ;
34
+
35
+ showPushNotifications : ( targetNotifications : Notification [ ] ) => void ;
34
36
}
35
37
| undefined
36
38
> ( undefined ) ;
@@ -114,44 +116,40 @@ export default function NotificationsProvider(props: NotificationsProviderProps)
114
116
return orderBy ( list , [ 'timestamp' ] , [ 'desc' ] ) ;
115
117
} , [ triggeredNotificationsByCurrentFingerprint , blockchainNotifications ] ) ;
116
118
117
- const showPushNotifications = useCallback ( ( ) => {
118
- // if fingerprint is not set then we can't show push notifications (user is not logged in)
119
- if ( ! globalNotifications || ! pushNotifications || isLoadingServices || ! fingerprint ) {
120
- return ;
121
- }
119
+ const showPushNotifications = useCallback (
120
+ ( targetNotifications : Notification [ ] ) => {
121
+ // if fingerprint is not set then we can't show push notifications (user is not logged in)
122
+ if ( ! globalNotifications || ! pushNotifications || isLoadingServices || ! fingerprint ) {
123
+ return ;
124
+ }
122
125
123
- setLastPushNotificationTimestamp ( ( prevLastPushNotificationTimestamp = 0 ) => {
124
- const firstUnseenNotification = notifications . find (
125
- ( notification ) => notification . timestamp > prevLastPushNotificationTimestamp ,
126
- ) ;
126
+ setLastPushNotificationTimestamp ( ( prevLastPushNotificationTimestamp = 0 ) => {
127
+ const firstUnseenNotification = targetNotifications . find (
128
+ ( notification ) => notification . timestamp > prevLastPushNotificationTimestamp ,
129
+ ) ;
127
130
128
- if ( ! firstUnseenNotification ) {
129
- return prevLastPushNotificationTimestamp ;
130
- }
131
+ if ( ! firstUnseenNotification ) {
132
+ return prevLastPushNotificationTimestamp ;
133
+ }
131
134
132
- const { title, body } = pushNotificationStringsForNotificationType ( firstUnseenNotification ) ;
135
+ const { title, body } = pushNotificationStringsForNotificationType ( firstUnseenNotification ) ;
133
136
134
- showPushNotification ( {
135
- title,
136
- body,
137
- } ) ;
137
+ showPushNotification ( {
138
+ title,
139
+ body,
140
+ } ) ;
138
141
139
- return firstUnseenNotification . timestamp ;
140
- } ) ;
141
- } , [
142
- globalNotifications ,
143
- pushNotifications ,
144
- isLoadingServices ,
145
- setLastPushNotificationTimestamp ,
146
- notifications ,
147
- showPushNotification ,
148
- fingerprint ,
149
- ] ) ;
150
-
151
- const unseenCount = useMemo (
152
- ( ) =>
153
- seenAt ? notifications . filter ( ( notification ) => notification . timestamp > seenAt ) . length : notifications . length ,
154
- [ seenAt , notifications ] ,
142
+ return firstUnseenNotification . timestamp ;
143
+ } ) ;
144
+ } ,
145
+ [
146
+ globalNotifications ,
147
+ pushNotifications ,
148
+ isLoadingServices ,
149
+ setLastPushNotificationTimestamp ,
150
+ showPushNotification ,
151
+ fingerprint ,
152
+ ] ,
155
153
) ;
156
154
157
155
const setAsSeen = useCallback ( ( ) => {
@@ -183,19 +181,15 @@ export default function NotificationsProvider(props: NotificationsProviderProps)
183
181
[ setTriggeredNotifications , deleteNotification ] ,
184
182
) ;
185
183
186
- useEffect ( ( ) => {
187
- showPushNotifications ( ) ;
188
- } , [ showPushNotifications ] ) ;
189
-
190
184
const contextValue = useMemo (
191
185
( ) => ( {
192
186
// base state
193
187
notifications,
194
188
isLoading,
195
189
error,
196
190
// seen
197
- unseenCount,
198
191
setAsSeen,
192
+ seenAt,
199
193
// settings
200
194
areNotificationsEnabled : globalNotifications ,
201
195
setNotificationsEnabled : setGlobalNotifications ,
@@ -204,14 +198,16 @@ export default function NotificationsProvider(props: NotificationsProviderProps)
204
198
205
199
showNotification,
206
200
deleteNotification : handleDeleteNotification ,
201
+
202
+ showPushNotifications,
207
203
} ) ,
208
204
[
209
205
notifications ,
210
206
isLoading ,
211
207
error ,
212
208
213
- unseenCount ,
214
209
setAsSeen ,
210
+ seenAt ,
215
211
216
212
globalNotifications ,
217
213
setGlobalNotifications ,
@@ -220,6 +216,8 @@ export default function NotificationsProvider(props: NotificationsProviderProps)
220
216
221
217
showNotification ,
222
218
handleDeleteNotification ,
219
+
220
+ showPushNotifications ,
223
221
] ,
224
222
) ;
225
223
0 commit comments