forked from StellarSplit/StellarSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotifications.ts
More file actions
70 lines (59 loc) · 1.81 KB
/
Copy pathnotifications.ts
File metadata and controls
70 lines (59 loc) · 1.81 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
import { create } from "zustand";
import type { Notification, NotificationType } from "../types/notifications";
interface NotificationsState {
notifications: Notification[];
typeFilter: NotificationType | "all";
markAsRead: (id: string) => void;
markAsUnread: (id: string) => void;
markAllAsRead: () => void;
setTypeFilter: (type: NotificationType | "all") => void;
clearAll: () => void;
addNotification: (notification: Omit<Notification, "id" | "read" | "createdAt">) => void;
removeNotification: (id: string) => void;
unreadCount: () => number;
}
function createNotification(
input: Omit<Notification, "id" | "read" | "createdAt">
): Notification {
return {
...input,
id: crypto.randomUUID(),
read: false,
createdAt: new Date().toISOString(),
};
}
export const useNotificationsStore = create<NotificationsState>((set, get) => ({
notifications: [],
typeFilter: "all",
markAsRead: (id) =>
set((state) => ({
notifications: state.notifications.map((n) =>
n.id === id ? { ...n, read: true } : n
),
})),
markAsUnread: (id) =>
set((state) => ({
notifications: state.notifications.map((n) =>
n.id === id ? { ...n, read: false } : n
),
})),
markAllAsRead: () =>
set((state) => ({
notifications: state.notifications.map((n) => ({ ...n, read: true })),
})),
setTypeFilter: (typeFilter) => set({ typeFilter }),
clearAll: () => set({ notifications: [] }),
addNotification: (input) =>
set((state) => ({
notifications: [
createNotification(input),
...state.notifications,
],
})),
removeNotification: (id) =>
set((state) => ({
notifications: state.notifications.filter((n) => n.id !== id),
})),
unreadCount: () =>
get().notifications.filter((n) => !n.read).length,
}));