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
|
import { create } from "zustand"
import { persist } from "zustand/middleware"
const MAXIMUM_NOTIFICATIONS = 50
export interface StoredNotification {
identifier: string
message: string
timestamp: string
type: "info" | "success" | "error"
actionUrl?: string
}
interface NotificationState {
notifications: StoredNotification[]
lastViewedAt: string | null
addNotification: (
message: string,
type?: "info" | "success" | "error",
actionUrl?: string
) => void
dismissNotification: (identifier: string) => void
clearAllNotifications: () => void
markAllAsViewed: () => void
}
export const useNotificationStore = create<NotificationState>()(
persist(
(set) => ({
notifications: [],
lastViewedAt: null,
addNotification: (message, type = "info", actionUrl) =>
set((state) => {
const newNotification: StoredNotification = {
identifier: crypto.randomUUID(),
message,
timestamp: new Date().toISOString(),
type,
...(actionUrl ? { actionUrl } : {}),
}
const updated = [newNotification, ...state.notifications].slice(
0,
MAXIMUM_NOTIFICATIONS
)
return { notifications: updated }
}),
dismissNotification: (identifier) =>
set((state) => ({
notifications: state.notifications.filter(
(notification) => notification.identifier !== identifier
),
})),
clearAllNotifications: () => set({ notifications: [] }),
markAllAsViewed: () =>
set({ lastViewedAt: new Date().toISOString() }),
}),
{
name: "asa-news-notifications",
}
)
)
|