-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
Copy pathNotificationService.tsx
85 lines (73 loc) · 2.75 KB
/
NotificationService.tsx
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
78
79
80
81
82
83
84
85
import React, { useContext, useEffect, useMemo } from "react";
import { Toast } from "components/ui/Toast";
import useTypesafeReducer from "hooks/useTypesafeReducer";
import { actions, initialState, notificationServiceReducer } from "./reducer";
import { Notification, NotificationServiceApi, NotificationServiceState } from "./types";
const notificationServiceContext = React.createContext<NotificationServiceApi | null>(null);
const NotificationService = ({ children }: { children: React.ReactNode }) => {
const [state, { addNotification, clearAll, deleteNotificationById }] = useTypesafeReducer<
NotificationServiceState,
typeof actions
>(notificationServiceReducer, initialState, actions);
const notificationService: NotificationServiceApi = useMemo(
() => ({
addNotification,
deleteNotificationById,
clearAll,
}),
// eslint-disable-next-line react-hooks/exhaustive-deps
[]
);
const firstNotification = state.notifications && state.notifications.length ? state.notifications[0] : null;
return (
<>
<notificationServiceContext.Provider value={notificationService}>{children}</notificationServiceContext.Provider>
{firstNotification ? (
// Show only first notification
<Toast
title={firstNotification.title}
text={firstNotification.text}
hasError={firstNotification.isError}
onClose={
firstNotification.nonClosable
? undefined
: () => {
deleteNotificationById(firstNotification.id);
firstNotification.onClose?.();
}
}
/>
) : null}
</>
);
};
export const useNotificationService: (
notification?: Notification,
dependencies?: []
) => {
registerNotification: (notification: Notification) => void;
unregisterAllNotifications: () => void;
unregisterNotificationById: (notificationId: string | number) => void;
} = (notification, dependencies) => {
const notificationService = useContext(notificationServiceContext);
if (!notificationService) {
throw new Error("useNotificationService must be used within a NotificationService.");
}
useEffect(() => {
if (notification) {
notificationService.addNotification(notification);
}
return () => {
if (notification) {
notificationService.deleteNotificationById(notification.id);
}
};
// eslint-disable-next-line
}, [notification, notificationService, ...(dependencies || [])]);
return {
registerNotification: notificationService.addNotification,
unregisterNotificationById: notificationService.deleteNotificationById,
unregisterAllNotifications: notificationService.clearAll,
};
};
export default React.memo(NotificationService);