-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathNotifications.context.tsx
More file actions
92 lines (74 loc) · 2.84 KB
/
Notifications.context.tsx
File metadata and controls
92 lines (74 loc) · 2.84 KB
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
86
87
88
89
90
91
92
import React, { createContext, ReactNode, useCallback, useContext, useMemo, useState } from 'react'
import { useProfileContext } from '~/libs/core'
import { dismiss, wasDismissed } from './localstorage.utils'
export type NotificationType = 'success' | 'error' | 'info' | 'warning' | 'banner';
export interface Notification {
id: string;
type: NotificationType;
icon?: ReactNode
message: string;
duration?: number; // in ms
}
type NotifyPayload = string | (Partial<Notification> & { message: string })
export interface NotificationContextType {
notifications: Notification[];
notify: (message: NotifyPayload, type?: NotificationType, duration?: number) => Notification | void;
showBannerNotification: (message: NotifyPayload) => Notification | void;
removeNotification: (id: string) => void;
}
const NotificationContext = createContext<NotificationContextType | undefined>(undefined)
export const useNotification = (): NotificationContextType => {
const context = useContext(NotificationContext)
if (!context) throw new Error('useNotification must be used within a NotificationProvider')
return context
}
export const NotificationProvider: React.FC<{
children: ReactNode,
}> = props => {
const profileCtx = useProfileContext()
const uuid = profileCtx.profile?.userId ?? 'annon'
const [notifications, setNotifications] = useState<Notification[]>([])
const removeNotification = useCallback((id: string, persist?: boolean) => {
setNotifications(prev => prev.filter(n => n.id !== id))
if (persist) {
dismiss(id)
}
}, [])
const notify = useCallback(
(message: NotifyPayload, type: NotificationType = 'info', duration = 3000) => {
const id = `${uuid}[${typeof message === 'string' ? message : message.id}]`
const newNotification: Notification
= typeof message === 'string'
? { duration, id, message, type }
: { duration, type, ...message, id }
if (wasDismissed(id)) {
return undefined
}
setNotifications(prev => [...prev, newNotification])
if (duration > 0) {
setTimeout(() => removeNotification(id), duration)
}
return newNotification
},
[uuid],
)
const showBannerNotification = useCallback((
message: NotifyPayload,
) => notify(message, 'banner', 0), [notify])
const ctxValue = useMemo(() => ({
notifications,
notify,
removeNotification,
showBannerNotification,
}), [
notifications,
notify,
removeNotification,
showBannerNotification,
])
return (
<NotificationContext.Provider value={ctxValue}>
{props.children}
</NotificationContext.Provider>
)
}