hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits
| 1 | export type NotificationType = "message" | "event" | "exam" | "homework"; |
| 2 | |
| 3 | export 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 | */ |
| 20 | export const UNREAD_DISPLAY_CAP = 9; |
| 21 | |
| 22 | export function formatUnreadCount(count: number): string { |
| 23 | return count > UNREAD_DISPLAY_CAP ? `${UNREAD_DISPLAY_CAP}+` : String(Math.max(0, count)); |
| 24 | } |
| 25 | |
| 26 | const STORAGE_KEY = "hezarfen.dismissedNotifications"; |
| 27 | const READ_STORAGE_KEY = "hezarfen.readNotifications"; |
| 28 | |
| 29 | function canUseStorage(): boolean { |
| 30 | return typeof window !== "undefined" && typeof localStorage !== "undefined"; |
| 31 | } |
| 32 | |
| 33 | export 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 | |
| 45 | export 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 | |
| 58 | export 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 | |
| 71 | export 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 | |
| 83 | export 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 | } |