Raw
1 "use client"
2
3 import React, { createContext, useContext, useMemo, useReducer } from "react"
4 import { NotificationContainer } from "@/components/Notification"
5 import uuid from "react-uuid"
6 import { NotificationItemProps } from "@/components/Notification/Item"
7
8 export type NotificationItemType = {
9 id?: string
10 } & NotificationItemProps
11
12 export type NotificationContextType = {
13 notifications: NotificationItemType[]
14 addNotification: (value: NotificationItemType) => void
15 generateId: () => string
16 removeNotification: (id: string) => void
17 updateNotification: (
18 id: string,
19 updatedData: Partial<Omit<NotificationItemType, "id">>
20 ) => void
21 }
22
23 export enum NotificationReducerActionTypes {
24 ADD = "add",
25 REMOVE = "remove",
26 UPDATE = "update",
27 }
28
29 export type NotificationReducerAction =
30 | {
31 type: NotificationReducerActionTypes.ADD
32 notification: NotificationItemType
33 }
34 | {
35 type: NotificationReducerActionTypes.REMOVE
36 id: string
37 }
38 | {
39 type: NotificationReducerActionTypes.UPDATE
40 id: string
41 updatedData: Partial<Omit<NotificationItemType, "id">>
42 }
43
44 const notificationReducer = (
45 state: NotificationItemType[],
46 action: NotificationReducerAction
47 ) => {
48 switch (action.type) {
49 case NotificationReducerActionTypes.ADD:
50 return [...state, action.notification]
51 case NotificationReducerActionTypes.REMOVE:
52 return state.filter((notification) => notification.id !== action.id)
53 case NotificationReducerActionTypes.UPDATE:
54 return state.map((notification) => {
55 if (notification.id === action.id) {
56 return {
57 ...notification,
58 ...action.updatedData,
59 }
60 }
61
62 return notification
63 })
64 default:
65 return state
66 }
67 }
68
69 const NotificationContext = createContext<NotificationContextType | null>(null)
70
71 export type NotificationProviderProps = {
72 children?: React.ReactNode
73 initial?: NotificationItemType[]
74 }
75
76 export const NotificationProvider = ({
77 children,
78 initial = [],
79 }: NotificationProviderProps) => {
80 const generateId = () => uuid()
81
82 const normalizedInitial = useMemo(() => {
83 return initial.map((notif) => ({
84 id: generateId(),
85 ...notif,
86 }))
87 }, [initial])
88
89 const [notifications, dispatch] = useReducer(
90 notificationReducer,
91 normalizedInitial
92 )
93
94 const addNotification = (notification: NotificationItemType) => {
95 if (!notification.id) {
96 notification.id = generateId()
97 }
98 dispatch({
99 type: NotificationReducerActionTypes.ADD,
100 notification,
101 })
102 }
103
104 const updateNotification = (
105 id: string,
106 updatedData: Partial<Omit<NotificationItemType, "id">>
107 ) => {
108 dispatch({
109 type: NotificationReducerActionTypes.UPDATE,
110 id,
111 updatedData,
112 })
113 }
114
115 const removeNotification = (id: string) => {
116 dispatch({
117 type: NotificationReducerActionTypes.REMOVE,
118 id,
119 })
120 }
121
122 return (
123 <NotificationContext.Provider
124 value={{
125 notifications,
126 addNotification,
127 generateId,
128 removeNotification,
129 updateNotification,
130 }}
131 >
132 {children}
133 <NotificationContainer />
134 </NotificationContext.Provider>
135 )
136 }
137
138 export const useNotifications = (
139 suppressError?: boolean
140 ): NotificationContextType | null => {
141 const context = useContext(NotificationContext)
142
143 if (!context && !suppressError) {
144 throw new Error(
145 "useNotifications must be used within a NotificationProvider"
146 )
147 }
148
149 return context
150 }