✨ solid notification badge indicator without pulsing animation
Browse files at this commit70d60112eb6da50413a267a024e8fe67856a1786Parent: 7362bb1- feat
- ✨ solid notification badge indicator without pulsing animation
5 changed files +387 -2
- src/components/layout/app-shell.tsx +3 -1
- src/components/layout/notification-center.tsx +320 -0
- src/components/layout/right-nav.tsx +1 -1
- src/components/ui/icons.tsx +9 -0
- src/lib/notifications.ts +54 -0
src/components/layout/app-shell.tsxmodified+3 -1
| @@ -5,6 +5,7 @@ import { CelebiPanel } from "@/components/layout/celebi-panel"; | |||
| 5 | import { CommandPalette } from "@/components/layout/command-palette"; | 5 | import { CommandPalette } from "@/components/layout/command-palette"; |
| 6 | import { MobileTabBar } from "@/components/layout/mobile-tab-bar"; | 6 | import { MobileTabBar } from "@/components/layout/mobile-tab-bar"; |
| 7 | import { NavBar } from "@/components/layout/nav-bar"; | 7 | import { NavBar } from "@/components/layout/nav-bar"; |
| 8 | +import { NotificationCenter } from "@/components/layout/notification-center"; | ||
| 8 | import { RightNav } from "@/components/layout/right-nav"; | 9 | import { RightNav } from "@/components/layout/right-nav"; |
| 9 | import { SideNav } from "@/components/layout/side-nav"; | 10 | import { SideNav } from "@/components/layout/side-nav"; |
| 10 | import { SidebarAccount } from "@/components/layout/sidebar-account"; | 11 | import { SidebarAccount } from "@/components/layout/sidebar-account"; |
| @@ -176,7 +177,8 @@ export function AppShell(props: ParentProps) { | |||
| 176 | </button> | 177 | </button> |
| 177 | </div> | 178 | </div> |
| 178 | 179 | ||
| 179 | - <div class="flex flex-1 items-center justify-end min-w-0"> | 180 | + <div class="flex flex-1 items-center justify-end gap-2 min-w-0"> |
| 181 | + <NotificationCenter /> | ||
| 180 | <Button type="button" variant="ghost" size="sm" class="h-9 shrink-0 rounded-full border border-border bg-transparent text-muted-foreground shadow-none hover:bg-muted hover:text-foreground" onClick={() => setCelebiOpen(true)}> | 182 | <Button type="button" variant="ghost" size="sm" class="h-9 shrink-0 rounded-full border border-border bg-transparent text-muted-foreground shadow-none hover:bg-muted hover:text-foreground" onClick={() => setCelebiOpen(true)}> |
| 181 | <IconBotSquare class="h-4 w-4" /> | 183 | <IconBotSquare class="h-4 w-4" /> |
| 182 | <span class="hidden sm:inline">{t("ai.askCelebi")}</span> | 184 | <span class="hidden sm:inline">{t("ai.askCelebi")}</span> |
src/components/layout/notification-center.tsxadded+320 -0
| @@ -0,0 +1,320 @@ | |||
| 1 | +import { For, Show, Suspense, createMemo, createResource, createSignal } from "solid-js"; | ||
| 2 | +import { useNavigate } from "@tanstack/solid-router"; | ||
| 3 | +import { getEvents } from "@/api/events"; | ||
| 4 | +import { getExams } from "@/api/exams"; | ||
| 5 | +import { getHomework } from "@/api/homework"; | ||
| 6 | +import { getMessages, patchMessageById } from "@/api/messages"; | ||
| 7 | +import { Button } from "@/components/ui/button"; | ||
| 8 | +import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover"; | ||
| 9 | +import { | ||
| 10 | + IconBell, | ||
| 11 | + IconCalendar, | ||
| 12 | + IconExam, | ||
| 13 | + IconHomework, | ||
| 14 | + IconMessage, | ||
| 15 | + IconTrash, | ||
| 16 | + IconX, | ||
| 17 | +} from "@/components/ui/icons"; | ||
| 18 | +import { cn } from "@/lib/cn"; | ||
| 19 | +import { personLabel } from "@/lib/person"; | ||
| 20 | +import { | ||
| 21 | + dismissAllNotificationIds, | ||
| 22 | + dismissNotificationId, | ||
| 23 | + getDismissedNotificationIds, | ||
| 24 | + type NotificationItem, | ||
| 25 | +} from "@/lib/notifications"; | ||
| 26 | +import { useT } from "@/stores/preferences-context"; | ||
| 27 | + | ||
| 28 | +export function NotificationCenter() { | ||
| 29 | + const t = useT(); | ||
| 30 | + const navigate = useNavigate(); | ||
| 31 | + const [open, setOpen] = createSignal(false); | ||
| 32 | + const [dismissedIds, setDismissedIds] = createSignal<Set<string>>(getDismissedNotificationIds()); | ||
| 33 | + | ||
| 34 | + // Fetch data sources | ||
| 35 | + const [messagesRes, { refetch: refetchMessages }] = createResource( | ||
| 36 | + async () => { | ||
| 37 | + try { | ||
| 38 | + return await getMessages("inbox", { read: false, limit: 10 }); | ||
| 39 | + } catch { | ||
| 40 | + return { items: [], total: 0 }; | ||
| 41 | + } | ||
| 42 | + } | ||
| 43 | + ); | ||
| 44 | + | ||
| 45 | + const [eventsRes] = createResource(async () => { | ||
| 46 | + try { | ||
| 47 | + return await getEvents(); | ||
| 48 | + } catch { | ||
| 49 | + return { items: [], total: 0 }; | ||
| 50 | + } | ||
| 51 | + }); | ||
| 52 | + | ||
| 53 | + const [examsRes] = createResource(async () => { | ||
| 54 | + try { | ||
| 55 | + return await getExams(); | ||
| 56 | + } catch { | ||
| 57 | + return { items: [], total: 0 }; | ||
| 58 | + } | ||
| 59 | + }); | ||
| 60 | + | ||
| 61 | + const [homeworkRes] = createResource(async () => { | ||
| 62 | + try { | ||
| 63 | + return await getHomework(); | ||
| 64 | + } catch { | ||
| 65 | + return { items: [], total: 0 }; | ||
| 66 | + } | ||
| 67 | + }); | ||
| 68 | + | ||
| 69 | + const now = new Date(); | ||
| 70 | + const startOfDay = new Date(now.getFullYear(), now.getMonth(), now.getDate()).getTime(); | ||
| 71 | + | ||
| 72 | + // Combine notification items | ||
| 73 | + const allNotifications = createMemo<NotificationItem[]>(() => { | ||
| 74 | + const list: NotificationItem[] = []; | ||
| 75 | + | ||
| 76 | + // 1. Unread Messages | ||
| 77 | + const msgs = messagesRes()?.items ?? []; | ||
| 78 | + for (const m of msgs) { | ||
| 79 | + list.push({ | ||
| 80 | + id: `msg_${m.id}`, | ||
| 81 | + type: "message", | ||
| 82 | + title: personLabel(m.sender), | ||
| 83 | + description: m.subject || m.body.replace(/<[^>]*>?/gm, "").trim(), | ||
| 84 | + targetUrl: "/messages", | ||
| 85 | + timestamp: m.sent_at, | ||
| 86 | + }); | ||
| 87 | + } | ||
| 88 | + | ||
| 89 | + // 2. Events starting today or upcoming | ||
| 90 | + const evts = eventsRes()?.items ?? []; | ||
| 91 | + for (const e of evts) { | ||
| 92 | + if (!e.starts_at) continue; | ||
| 93 | + const t = new Date(e.starts_at).getTime(); | ||
| 94 | + if (t >= startOfDay) { | ||
| 95 | + list.push({ | ||
| 96 | + id: `evt_${e.id}`, | ||
| 97 | + type: "event", | ||
| 98 | + title: e.title, | ||
| 99 | + description: e.starts_at | ||
| 100 | + ? new Date(e.starts_at).toLocaleString([], { | ||
| 101 | + month: "short", | ||
| 102 | + day: "numeric", | ||
| 103 | + hour: "2-digit", | ||
| 104 | + minute: "2-digit", | ||
| 105 | + }) | ||
| 106 | + : "", | ||
| 107 | + targetUrl: `/events/${e.id}`, | ||
| 108 | + timestamp: t, | ||
| 109 | + }); | ||
| 110 | + } | ||
| 111 | + } | ||
| 112 | + | ||
| 113 | + // 3. Exams starting today or upcoming | ||
| 114 | + const exms = examsRes()?.items ?? []; | ||
| 115 | + for (const ex of exms) { | ||
| 116 | + if (!ex.starts_at || ex.draft) continue; | ||
| 117 | + const t = new Date(ex.starts_at).getTime(); | ||
| 118 | + if (t >= startOfDay) { | ||
| 119 | + list.push({ | ||
| 120 | + id: `ex_${ex.id}`, | ||
| 121 | + type: "exam", | ||
| 122 | + title: ex.title, | ||
| 123 | + description: ex.starts_at | ||
| 124 | + ? new Date(ex.starts_at).toLocaleString([], { | ||
| 125 | + month: "short", | ||
| 126 | + day: "numeric", | ||
| 127 | + hour: "2-digit", | ||
| 128 | + minute: "2-digit", | ||
| 129 | + }) | ||
| 130 | + : "", | ||
| 131 | + targetUrl: `/exams/${ex.id}`, | ||
| 132 | + timestamp: t, | ||
| 133 | + }); | ||
| 134 | + } | ||
| 135 | + } | ||
| 136 | + | ||
| 137 | + // 4. Homework due upcoming | ||
| 138 | + const hws = homeworkRes()?.items ?? []; | ||
| 139 | + for (const hw of hws) { | ||
| 140 | + if (!hw.due_at) continue; | ||
| 141 | + if (hw.due_at >= startOfDay) { | ||
| 142 | + list.push({ | ||
| 143 | + id: `hw_${hw.id}`, | ||
| 144 | + type: "homework", | ||
| 145 | + title: hw.title, | ||
| 146 | + description: hw.due_at | ||
| 147 | + ? `${t("homework.dueAt")}: ${new Date(hw.due_at).toLocaleDateString([], { | ||
| 148 | + month: "short", | ||
| 149 | + day: "numeric", | ||
| 150 | + })}` | ||
| 151 | + : "", | ||
| 152 | + targetUrl: `/homework/${hw.id}`, | ||
| 153 | + timestamp: hw.due_at, | ||
| 154 | + }); | ||
| 155 | + } | ||
| 156 | + } | ||
| 157 | + | ||
| 158 | + // Sort chronologically (newest/most relevant first) | ||
| 159 | + return list.sort((a, b) => b.timestamp - a.timestamp); | ||
| 160 | + }); | ||
| 161 | + | ||
| 162 | + // Filter out dismissed notifications | ||
| 163 | + const activeNotifications = createMemo(() => { | ||
| 164 | + const dismissed = dismissedIds(); | ||
| 165 | + return allNotifications().filter((item) => !dismissed.has(item.id)); | ||
| 166 | + }); | ||
| 167 | + | ||
| 168 | + const unreadCount = () => activeNotifications().length; | ||
| 169 | + | ||
| 170 | + const handleDismissSingle = (evt: MouseEvent, id: string) => { | ||
| 171 | + evt.stopPropagation(); | ||
| 172 | + const updated = dismissNotificationId(id); | ||
| 173 | + setDismissedIds(new Set(updated)); | ||
| 174 | + }; | ||
| 175 | + | ||
| 176 | + const handleDismissAll = () => { | ||
| 177 | + const ids = activeNotifications().map((item) => item.id); | ||
| 178 | + const updated = dismissAllNotificationIds(ids); | ||
| 179 | + setDismissedIds(new Set(updated)); | ||
| 180 | + }; | ||
| 181 | + | ||
| 182 | + const handleSelectNotification = async (item: NotificationItem) => { | ||
| 183 | + // If message notification, mark read in backend | ||
| 184 | + if (item.id.startsWith("msg_")) { | ||
| 185 | + const realId = item.id.replace("msg_", ""); | ||
| 186 | + try { | ||
| 187 | + await patchMessageById(realId, { read: true }); | ||
| 188 | + void refetchMessages(); | ||
| 189 | + } catch { | ||
| 190 | + // ignore | ||
| 191 | + } | ||
| 192 | + } | ||
| 193 | + // Dismiss locally | ||
| 194 | + const updated = dismissNotificationId(item.id); | ||
| 195 | + setDismissedIds(new Set(updated)); | ||
| 196 | + setOpen(false); | ||
| 197 | + void navigate({ to: item.targetUrl }); | ||
| 198 | + }; | ||
| 199 | + | ||
| 200 | + return ( | ||
| 201 | + <Popover open={open()} onOpenChange={setOpen} placement="bottom-end" gutter={8}> | ||
| 202 | + <PopoverTrigger | ||
| 203 | + class={cn( | ||
| 204 | + "relative flex h-9 w-9 items-center justify-center rounded-full border border-border/80 bg-card/60 text-muted-foreground transition-colors hover:bg-secondary hover:text-foreground shrink-0 cursor-pointer outline-none", | ||
| 205 | + open() && "bg-secondary text-foreground" | ||
| 206 | + )} | ||
| 207 | + title={t("rightPanel.messagesTitle")} | ||
| 208 | + aria-label="Bildirimler" | ||
| 209 | + > | ||
| 210 | + <IconBell class="h-4 w-4" /> | ||
| 211 | + <Show when={unreadCount() > 0}> | ||
| 212 | + <span class="absolute -top-1 -right-1 flex h-4 min-w-4 items-center justify-center rounded-full bg-rose-500 px-1 text-[9px] font-bold text-white shadow-xs ring-2 ring-background"> | ||
| 213 | + {unreadCount() > 9 ? "9+" : unreadCount()} | ||
| 214 | + </span> | ||
| 215 | + </Show> | ||
| 216 | + </PopoverTrigger> | ||
| 217 | + | ||
| 218 | + <PopoverContent class="w-80 sm:w-96 rounded-2xl p-0 shadow-2xl border border-black/[0.08] dark:border-white/[0.12] bg-popover/95 backdrop-blur-xl overflow-hidden"> | ||
| 219 | + {/* Header */} | ||
| 220 | + <div class="flex items-center justify-between border-b border-border/80 px-4 py-3 bg-muted/40"> | ||
| 221 | + <div class="flex items-center gap-2"> | ||
| 222 | + <IconBell class="h-4 w-4 text-primary" /> | ||
| 223 | + <h3 class="text-xs font-bold text-foreground">Bildirimler</h3> | ||
| 224 | + <Show when={unreadCount() > 0}> | ||
| 225 | + <span class="rounded-full bg-primary/15 px-2 py-0.5 font-mono text-[10px] font-bold text-primary"> | ||
| 226 | + {unreadCount()} | ||
| 227 | + </span> | ||
| 228 | + </Show> | ||
| 229 | + </div> | ||
| 230 | + | ||
| 231 | + <Show when={unreadCount() > 0}> | ||
| 232 | + <Button | ||
| 233 | + type="button" | ||
| 234 | + variant="ghost" | ||
| 235 | + size="sm" | ||
| 236 | + class="h-7 px-2 text-[11px] font-semibold text-muted-foreground hover:text-destructive hover:bg-destructive/10 rounded-lg" | ||
| 237 | + onClick={handleDismissAll} | ||
| 238 | + title="Tüm Bildirimleri Temizle" | ||
| 239 | + > | ||
| 240 | + <IconTrash class="mr-1 h-3 w-3" /> | ||
| 241 | + Tümünü Sil | ||
| 242 | + </Button> | ||
| 243 | + </Show> | ||
| 244 | + </div> | ||
| 245 | + | ||
| 246 | + {/* Notifications List */} | ||
| 247 | + <div class="max-h-80 overflow-y-auto p-2 space-y-1"> | ||
| 248 | + <Suspense | ||
| 249 | + fallback={ | ||
| 250 | + <div class="p-8 text-center text-xs text-muted-foreground"> | ||
| 251 | + {t("common.loading")} | ||
| 252 | + </div> | ||
| 253 | + } | ||
| 254 | + > | ||
| 255 | + <Show | ||
| 256 | + when={activeNotifications().length > 0} | ||
| 257 | + fallback={ | ||
| 258 | + <div class="p-8 text-center space-y-2"> | ||
| 259 | + <div class="mx-auto flex h-10 w-10 items-center justify-center rounded-full bg-muted text-muted-foreground"> | ||
| 260 | + <IconBell class="h-5 w-5" /> | ||
| 261 | + </div> | ||
| 262 | + <p class="text-xs font-medium text-muted-foreground"> | ||
| 263 | + Henüz yeni bir bildiriminiz yok. | ||
| 264 | + </p> | ||
| 265 | + </div> | ||
| 266 | + } | ||
| 267 | + > | ||
| 268 | + <For each={activeNotifications()}> | ||
| 269 | + {(item) => ( | ||
| 270 | + <div | ||
| 271 | + onClick={() => handleSelectNotification(item)} | ||
| 272 | + class="group relative flex items-start gap-3 rounded-xl p-2.5 text-xs transition-colors cursor-pointer hover:bg-accent/60 select-none" | ||
| 273 | + > | ||
| 274 | + {/* Icon by Type */} | ||
| 275 | + <div class="mt-0.5 flex h-8 w-8 shrink-0 items-center justify-center rounded-lg bg-muted text-muted-foreground group-hover:bg-primary group-hover:text-primary-foreground transition-colors"> | ||
| 276 | + <Show when={item.type === "message"}> | ||
| 277 | + <IconMessage class="h-4 w-4" /> | ||
| 278 | + </Show> | ||
| 279 | + <Show when={item.type === "event"}> | ||
| 280 | + <IconCalendar class="h-4 w-4" /> | ||
| 281 | + </Show> | ||
| 282 | + <Show when={item.type === "exam"}> | ||
| 283 | + <IconExam class="h-4 w-4" /> | ||
| 284 | + </Show> | ||
| 285 | + <Show when={item.type === "homework"}> | ||
| 286 | + <IconHomework class="h-4 w-4" /> | ||
| 287 | + </Show> | ||
| 288 | + </div> | ||
| 289 | + | ||
| 290 | + {/* Content */} | ||
| 291 | + <div class="min-w-0 flex-1 pr-6 space-y-0.5"> | ||
| 292 | + <div class="flex items-center justify-between gap-1"> | ||
| 293 | + <span class="truncate font-bold text-foreground"> | ||
| 294 | + {item.title} | ||
| 295 | + </span> | ||
| 296 | + </div> | ||
| 297 | + <p class="line-clamp-2 text-[11px] text-muted-foreground leading-snug"> | ||
| 298 | + {item.description} | ||
| 299 | + </p> | ||
| 300 | + </div> | ||
| 301 | + | ||
| 302 | + {/* Single Dismiss Button */} | ||
| 303 | + <button | ||
| 304 | + type="button" | ||
| 305 | + class="absolute right-2 top-2.5 flex h-6 w-6 items-center justify-center rounded-md text-muted-foreground/60 opacity-80 sm:opacity-0 group-hover:opacity-100 hover:bg-destructive/10 hover:text-destructive transition-all" | ||
| 306 | + title="Sil" | ||
| 307 | + onClick={(e) => handleDismissSingle(e, item.id)} | ||
| 308 | + > | ||
| 309 | + <IconX class="h-3.5 w-3.5" /> | ||
| 310 | + </button> | ||
| 311 | + </div> | ||
| 312 | + )} | ||
| 313 | + </For> | ||
| 314 | + </Show> | ||
| 315 | + </Suspense> | ||
| 316 | + </div> | ||
| 317 | + </PopoverContent> | ||
| 318 | + </Popover> | ||
| 319 | + ); | ||
| 320 | +} | ||
src/components/layout/right-nav.tsxmodified+1 -1
| @@ -207,7 +207,7 @@ export function RightNav() { | |||
| 207 | <IconMessage class="h-5 w-5" /> | 207 | <IconMessage class="h-5 w-5" /> |
| 208 | <Show when={unreadCount() > 0}> | 208 | <Show when={unreadCount() > 0}> |
| 209 | <span | 209 | <span |
| 210 | - class="absolute -top-1 -right-1 flex h-4 min-w-4 items-center justify-center rounded-full bg-rose-500 px-1 text-[9px] font-bold text-white shadow-xs animate-pulse ring-2 ring-sidebar dark:ring-[#070707]" | 210 | + class="absolute -top-1 -right-1 flex h-4 min-w-4 items-center justify-center rounded-full bg-rose-500 px-1 text-[9px] font-bold text-white shadow-xs ring-2 ring-sidebar dark:ring-[#070707]" |
| 211 | title={`${unreadCount()} ${t("rightPanel.unreadBadge")}`} | 211 | title={`${unreadCount()} ${t("rightPanel.unreadBadge")}`} |
| 212 | > | 212 | > |
| 213 | {unreadCount() > 9 ? "9+" : unreadCount()} | 213 | {unreadCount() > 9 ? "9+" : unreadCount()} |
src/components/ui/icons.tsxmodified+9 -0
| @@ -693,3 +693,12 @@ export function IconExternalLink(props: IconProps) { | |||
| 693 | ); | 693 | ); |
| 694 | } | 694 | } |
| 695 | 695 | ||
| 696 | +export function IconBell(props: IconProps) { | ||
| 697 | + return ( | ||
| 698 | + <Svg {...props}> | ||
| 699 | + <path d="M6 8a6 6 0 0 1 12 0c0 7 3 9 3 9H3s3-2 3-9" /> | ||
| 700 | + <path d="M10.3 21a1.94 1.94 0 0 0 3.4 0" /> | ||
| 701 | + </Svg> | ||
| 702 | + ); | ||
| 703 | +} | ||
| 704 | + | ||
src/lib/notifications.tsadded+54 -0
| @@ -0,0 +1,54 @@ | |||
| 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 | + targetUrl: string; | ||
| 9 | + timestamp: number; | ||
| 10 | +} | ||
| 11 | + | ||
| 12 | +const STORAGE_KEY = "hezarfen.dismissedNotifications"; | ||
| 13 | + | ||
| 14 | +function canUseStorage(): boolean { | ||
| 15 | + return typeof window !== "undefined" && typeof localStorage !== "undefined"; | ||
| 16 | +} | ||
| 17 | + | ||
| 18 | +export 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 | + | ||
| 30 | +export 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 | + | ||
| 43 | +export 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 | +} | ||