forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiBc35a83aBurak Boduroğlu4 months ago440 commits

notifications.ts54 lines, 1.3 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 targetUrl: string;
9 timestamp: number;
10}
11
12const STORAGE_KEY = "hezarfen.dismissedNotifications";
13
14function canUseStorage(): boolean {
15 return typeof window !== "undefined" && typeof localStorage !== "undefined";
16}
17
18export function getDismissedNotificationIds(): Set<string> {
19 if (!canUseStorage()) return new Set();
20 try {
21 const raw = localStorage.getItem(STORAGE_KEY);
22 if (!raw) return new Set();
23 const arr = JSON.parse(raw);
24 return Array.isArray(arr) ? new Set(arr) : new Set();
25 } catch {
26 return new Set();
27 }
28}
29
30export function dismissNotificationId(id: string): Set<string> {
31 const current = getDismissedNotificationIds();
32 current.add(id);
33 if (canUseStorage()) {
34 try {
35 localStorage.setItem(STORAGE_KEY, JSON.stringify(Array.from(current)));
36 } catch {
37 // ignore
38 }
39 }
40 return current;
41}
42
43export function dismissAllNotificationIds(ids: string[]): Set<string> {
44 const current = getDismissedNotificationIds();
45 ids.forEach((id) => current.add(id));
46 if (canUseStorage()) {
47 try {
48 localStorage.setItem(STORAGE_KEY, JSON.stringify(Array.from(current)));
49 } catch {
50 // ignore
51 }
52 }
53 return current;
54}