forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits

notifications.ts81 lines, 2.0 KiBHistoryRawDownload
1export type NotificationType = "message" | "event" | "exam" | "homework";
2
3export interface NotificationItem {
4 id: string;
5 type: NotificationType;
6 title: string;
7 description: string;
8 preview?: string;
9 targetUrl: string;
10 timestamp: number;
11}
12
13const STORAGE_KEY = "hezarfen.dismissedNotifications";
14const READ_STORAGE_KEY = "hezarfen.readNotifications";
15
16function canUseStorage(): boolean {
17 return typeof window !== "undefined" && typeof localStorage !== "undefined";
18}
19
20export function getDismissedNotificationIds(): Set<string> {
21 if (!canUseStorage()) return new Set();
22 try {
23 const raw = localStorage.getItem(STORAGE_KEY);
24 if (!raw) return new Set();
25 const arr = JSON.parse(raw);
26 return Array.isArray(arr) ? new Set(arr) : new Set();
27 } catch {
28 return new Set();
29 }
30}
31
32export function dismissNotificationId(id: string): Set<string> {
33 const current = getDismissedNotificationIds();
34 current.add(id);
35 if (canUseStorage()) {
36 try {
37 localStorage.setItem(STORAGE_KEY, JSON.stringify(Array.from(current)));
38 } catch {
39 // ignore
40 }
41 }
42 return current;
43}
44
45export function dismissAllNotificationIds(ids: string[]): Set<string> {
46 const current = getDismissedNotificationIds();
47 ids.forEach((id) => current.add(id));
48 if (canUseStorage()) {
49 try {
50 localStorage.setItem(STORAGE_KEY, JSON.stringify(Array.from(current)));
51 } catch {
52 // ignore
53 }
54 }
55 return current;
56}
57
58export function getReadNotificationIds(): Set<string> {
59 if (!canUseStorage()) return new Set();
60 try {
61 const raw = localStorage.getItem(READ_STORAGE_KEY);
62 if (!raw) return new Set();
63 const arr = JSON.parse(raw);
64 return Array.isArray(arr) ? new Set(arr) : new Set();
65 } catch {
66 return new Set();
67 }
68}
69
70export function markNotificationRead(id: string): Set<string> {
71 const current = getReadNotificationIds();
72 current.add(id);
73 if (canUseStorage()) {
74 try {
75 localStorage.setItem(READ_STORAGE_KEY, JSON.stringify(Array.from(current)));
76 } catch {
77 // ignore
78 }
79 }
80 return current;
81}