-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathuseNotificationList.ts
77 lines (63 loc) · 2.43 KB
/
useNotificationList.ts
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import { useEffect, useMemo, useState } from 'react';
import { useSubscribe } from '@deriv-com/api-hooks';
type TNotificationLinks = {
href: string;
rel: string;
};
type TNotification = {
category: string;
id: number;
links: TNotificationLinks[];
message_key: string;
payload: string;
read: boolean;
removed: boolean;
};
const handleData = (incomingMessages: TNotification[], prevMessages: TNotification[]) => {
if (!incomingMessages) return prevMessages;
let notifications = prevMessages;
for (let updateIdx = 0; updateIdx < incomingMessages.length; updateIdx++) {
const update = incomingMessages[updateIdx];
const existingMessageIndex = notifications.findIndex((message: TNotification) => message.id === update.id);
const existingMessage = notifications[existingMessageIndex];
if (existingMessage) {
if (update.removed)
notifications = notifications.filter((message: TNotification) => message.id !== update.id);
else notifications[existingMessageIndex] = { ...existingMessage, ...update };
} else notifications.unshift(update);
}
notifications.sort((a: TNotification, b: TNotification) => b.id - a.id);
return [...notifications];
};
/**
* Hook that returns the list of notifications.
*
* @example const { data: notifications } = useNotificationList();
*/
const useNotificationList = () => {
// @ts-expect-error Type undefined. This endpoint will be added to api-hooks.
const { data, ...rest } = useSubscribe('notifications_list');
const [messages, setMessages] = useState<TNotification[]>([]);
const modified_data = useMemo(() => {
if (!messages) return undefined;
// TODO: Remove this filter once all the notifications are implemented
const notifications = messages.filter((notification: { message_key: string }) =>
['p2p-limit-upgrade-available'].includes(notification.message_key)
);
return notifications;
}, [messages]);
useEffect(() => {
// @ts-expect-error Type undefined.
if (data?.notifications_list) {
setMessages(prevMessages => {
// @ts-expect-error Type undefined.
return handleData(data.notifications_list.messages, prevMessages);
});
}
}, [data]);
return {
data: modified_data || [],
...rest,
};
};
export default useNotificationList;