forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits

notifications.ts94 lines, 2.6 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 /** Where a click goes; a row without one opens the in-list preview instead. */
10 targetUrl?: string;
11 timestamp: number;
12}
13
14/**
15 * An unread count as every shell surface shows it — the bell and messages
16 * badges, the phone action button and the notification list header share one
17 * cap, so a badge never reads "9+" over a header that says "20". The exact
18 * number goes in the accessible label.
19 */
20export const UNREAD_DISPLAY_CAP = 9;
21
22export function formatUnreadCount(count: number): string {
23 return count > UNREAD_DISPLAY_CAP ? `${UNREAD_DISPLAY_CAP}+` : String(Math.max(0, count));
24}
25
26const STORAGE_KEY = "hezarfen.dismissedNotifications";
27const READ_STORAGE_KEY = "hezarfen.readNotifications";
28
29function canUseStorage(): boolean {
30 return typeof window !== "undefined" && typeof localStorage !== "undefined";
31}
32
33export function getDismissedNotificationIds(): Set<string> {
34 if (!canUseStorage()) return new Set();
35 try {
36 const raw = localStorage.getItem(STORAGE_KEY);
37 if (!raw) return new Set();
38 const arr = JSON.parse(raw);
39 return Array.isArray(arr) ? new Set(arr) : new Set();
40 } catch {
41 return new Set();
42 }
43}
44
45export function dismissNotificationId(id: string): Set<string> {
46 const current = getDismissedNotificationIds();
47 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 dismissAllNotificationIds(ids: string[]): Set<string> {
59 const current = getDismissedNotificationIds();
60 ids.forEach((id) => current.add(id));
61 if (canUseStorage()) {
62 try {
63 localStorage.setItem(STORAGE_KEY, JSON.stringify(Array.from(current)));
64 } catch {
65 // ignore
66 }
67 }
68 return current;
69}
70
71export function getReadNotificationIds(): Set<string> {
72 if (!canUseStorage()) return new Set();
73 try {
74 const raw = localStorage.getItem(READ_STORAGE_KEY);
75 if (!raw) return new Set();
76 const arr = JSON.parse(raw);
77 return Array.isArray(arr) ? new Set(arr) : new Set();
78 } catch {
79 return new Set();
80 }
81}
82
83export function markNotificationRead(id: string): Set<string> {
84 const current = getReadNotificationIds();
85 current.add(id);
86 if (canUseStorage()) {
87 try {
88 localStorage.setItem(READ_STORAGE_KEY, JSON.stringify(Array.from(current)));
89 } catch {
90 // ignore
91 }
92 }
93 return current;
94}