1
- import { useEffect , useState } from 'react' ;
1
+ import { useEffect , useMemo , useState } from 'react' ;
2
+ import { useSubscribe } from '@deriv-com/api-hooks' ;
2
3
3
4
type TNotificationLinks = {
4
5
href : string ;
@@ -7,54 +8,69 @@ type TNotificationLinks = {
7
8
type TNotification = {
8
9
category : string ;
9
10
id : number ;
10
- links ? : TNotificationLinks [ ] ;
11
+ links : TNotificationLinks [ ] ;
11
12
message_key : string ;
12
13
payload : string ;
13
14
read : boolean ;
14
15
removed : boolean ;
15
16
} ;
16
17
18
+ const handleData = ( incomingMessages : TNotification [ ] , prevMessages : TNotification [ ] ) => {
19
+ if ( ! incomingMessages ) return prevMessages ;
20
+
21
+ let notifications = prevMessages ;
22
+ for ( let updateIdx = 0 ; updateIdx < incomingMessages . length ; updateIdx ++ ) {
23
+ const update = incomingMessages [ updateIdx ] ;
24
+
25
+ const existingMessageIndex = notifications . findIndex ( ( message : TNotification ) => message . id === update . id ) ;
26
+ const existingMessage = notifications [ existingMessageIndex ] ;
27
+
28
+ if ( existingMessage ) {
29
+ if ( update . removed )
30
+ notifications = notifications . filter ( ( message : TNotification ) => message . id !== update . id ) ;
31
+ else notifications [ existingMessageIndex ] = { ...existingMessage , ...update } ;
32
+ } else notifications . unshift ( update ) ;
33
+ }
34
+
35
+ notifications . sort ( ( a : TNotification , b : TNotification ) => b . id - a . id ) ;
36
+
37
+ return [ ...notifications ] ;
38
+ } ;
39
+
17
40
/**
18
41
* Hook that returns the list of notifications.
19
42
*
43
+ * @example const { data: notifications } = useNotificationList();
20
44
*/
21
45
const useNotificationList = ( ) => {
22
- const [ notifications , setNotifications ] = useState < TNotification [ ] > ( [ ] ) ;
46
+ // @ts -expect-error Type undefined. This endpoint will be added to api-hooks.
47
+ const { data, ...rest } = useSubscribe ( 'notifications_list' ) ;
48
+ const [ messages , setMessages ] = useState < TNotification [ ] > ( [ ] ) ;
49
+
50
+ const modified_data = useMemo ( ( ) => {
51
+ if ( ! messages ) return undefined ;
52
+
53
+ // TODO: Remove this filter once all the notifications are implemented
54
+ const notifications = messages . filter ( ( notification : { message_key : string } ) =>
55
+ [ 'p2p-limit-upgrade-available' ] . includes ( notification . message_key )
56
+ ) ;
57
+
58
+ return notifications ;
59
+ } , [ messages ] ) ;
23
60
24
61
useEffect ( ( ) => {
25
- const fetchNotifications = async ( ) => {
26
- try {
27
- const response = await fetch ( 'https://fs191x.buildship.run/v4/notification/list' , {
28
- headers : {
29
- 'Content-Type' : 'application/json' ,
30
- token : localStorage . getItem ( 'authToken' ) || '' ,
31
- } ,
32
- method : 'GET' ,
33
- } ) ;
34
-
35
- if ( response . ok ) {
36
- const data = await response . json ( ) ;
37
-
38
- // TODO: Remove this filter once all the notifications are implemented
39
- const messages = data . filter ( ( notification : { message_key : string } ) =>
40
- [ 'p2p-limit-upgrade-available' ] . includes ( notification . message_key )
41
- ) ;
42
- setNotifications ( messages ) ;
43
- } else {
44
- // eslint-disable-next-line no-console
45
- console . error ( 'Failed to fetch notifications' ) ;
46
- }
47
- } catch ( error ) {
48
- // eslint-disable-next-line no-console
49
- console . error ( error ) ;
50
- }
51
- } ;
52
-
53
- fetchNotifications ( ) ;
54
- } , [ ] ) ;
62
+ // @ts -expect-error Type undefined.
63
+ if ( data ?. notifications_list ) {
64
+ setMessages ( prevMessages => {
65
+ // @ts -expect-error Type undefined.
66
+ return handleData ( data . notifications_list . messages , prevMessages ) ;
67
+ } ) ;
68
+ }
69
+ } , [ data ] ) ;
55
70
56
71
return {
57
- data : notifications || [ ] ,
72
+ data : modified_data || [ ] ,
73
+ ...rest ,
58
74
} ;
59
75
} ;
60
76
0 commit comments