hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits
| 1 | import { For, Show, Suspense, createSignal } from "solid-js"; |
| 2 | import { Button } from "@/components/ui/button"; |
| 3 | import { ConfirmDialog } from "@/components/ui/confirm-dialog"; |
| 4 | import { |
| 5 | IconBell, |
| 6 | IconCalendar, |
| 7 | IconChevronLeft, |
| 8 | IconExam, |
| 9 | IconHomework, |
| 10 | IconMessage, |
| 11 | IconTrash, |
| 12 | IconX, |
| 13 | } from "@/components/ui/icons"; |
| 14 | import { cn } from "@/lib/cn"; |
| 15 | import { formatUnreadCount, type NotificationItem, type NotificationType } from "@/lib/notifications"; |
| 16 | import type { NotificationFeed } from "@/components/layout/notification-feed"; |
| 17 | import { useT } from "@/stores/preferences-context"; |
| 18 | |
| 19 | /** |
| 20 | * The notification list — header with "clear all", grouped rows, and the |
| 21 | * preview of one row — for whichever surface hosts it. The preview is local |
| 22 | * state, so it resets whenever the host unmounts the list on close. |
| 23 | */ |
| 24 | export function NotificationList(props: { |
| 25 | feed: NotificationFeed; |
| 26 | /** Leaves the target page of a previewed notification. */ |
| 27 | onNavigate: (url: string) => void; |
| 28 | /** The host already names the list (a panel title). */ |
| 29 | showTitle?: boolean; |
| 30 | /** Height cap of the scrolling list; the popover keeps it short. */ |
| 31 | scrollClass?: string; |
| 32 | }) { |
| 33 | const t = useT(); |
| 34 | const [previewNotification, setPreviewNotification] = createSignal<NotificationItem | null>(null); |
| 35 | // Clearing everything cannot be undone, so it asks first. |
| 36 | const [confirmClear, setConfirmClear] = createSignal(false); |
| 37 | const totalCount = () => props.feed.groups().reduce((count, group) => count + group.items.length, 0); |
| 38 | |
| 39 | const notificationGroupLabel = (type: NotificationType) => { |
| 40 | if (type === "message") return t("nav.messages"); |
| 41 | if (type === "event") return t("events.title"); |
| 42 | if (type === "exam") return t("exams.title"); |
| 43 | return t("homework.title"); |
| 44 | }; |
| 45 | const notificationGroupTone = (type: NotificationType) => { |
| 46 | if (type === "message") return "border-primary/20 bg-primary/[0.035]"; |
| 47 | if (type === "event") return "border-violet-500/20 bg-violet-500/[0.035]"; |
| 48 | if (type === "exam") return "border-amber-500/25 bg-amber-500/[0.045]"; |
| 49 | return "border-emerald-500/20 bg-emerald-500/[0.035]"; |
| 50 | }; |
| 51 | const notificationGroupIconTone = (type: NotificationType) => { |
| 52 | if (type === "message") return "bg-primary/10 text-primary-text"; |
| 53 | if (type === "event") return "bg-violet-500/10 text-violet-700 dark:text-violet-300"; |
| 54 | if (type === "exam") return "bg-amber-500/10 text-amber-700 dark:text-amber-300"; |
| 55 | return "bg-emerald-500/10 text-emerald-700 dark:text-emerald-300"; |
| 56 | }; |
| 57 | |
| 58 | const dismissOne = (evt: MouseEvent, id: string) => { |
| 59 | evt.stopPropagation(); |
| 60 | props.feed.dismiss(id); |
| 61 | }; |
| 62 | |
| 63 | // A row with a target page goes straight there (marked read on the way); |
| 64 | // only a row with nowhere to go opens the in-list preview. |
| 65 | const select = (item: NotificationItem) => { |
| 66 | void props.feed.markRead(item); |
| 67 | if (item.targetUrl) props.onNavigate(item.targetUrl); |
| 68 | else setPreviewNotification(item); |
| 69 | }; |
| 70 | |
| 71 | return ( |
| 72 | <> |
| 73 | {/* Hosted under a panel title, the header row only earns its place when |
| 74 | there is something to clear. */} |
| 75 | <Show when={(props.showTitle ?? true) || props.feed.unreadCount() > 0}> |
| 76 | <div class="flex items-center justify-between border-b border-border-hairline px-4 py-3"> |
| 77 | <div class="flex items-center gap-2"> |
| 78 | <Show when={props.showTitle ?? true}> |
| 79 | <h3 class="text-sm font-semibold text-text-strong">{t("notifications.title")}</h3> |
| 80 | </Show> |
| 81 | <Show when={props.feed.unreadCount() > 0}> |
| 82 | <span |
| 83 | class="rounded-full bg-primary/15 px-2 py-0.5 font-mono text-[11px] font-bold text-primary-text" |
| 84 | aria-label={t("notifications.unreadCount", { count: props.feed.unreadCount() })} |
| 85 | > |
| 86 | {formatUnreadCount(props.feed.unreadCount())} |
| 87 | </span> |
| 88 | </Show> |
| 89 | </div> |
| 90 | |
| 91 | <Show when={props.feed.unreadCount() > 0}> |
| 92 | <Button |
| 93 | type="button" |
| 94 | variant="ghost" |
| 95 | size="sm" |
| 96 | class="h-7 rounded-lg px-2 text-[11px] font-semibold text-text-subtle hover:bg-destructive/10 hover:text-destructive-text" |
| 97 | onClick={() => setConfirmClear(true)} |
| 98 | title={t("notifications.clearAll")} |
| 99 | > |
| 100 | <IconTrash class="mr-1 h-3 w-3" /> |
| 101 | {t("notifications.clearAll")} |
| 102 | </Button> |
| 103 | </Show> |
| 104 | </div> |
| 105 | </Show> |
| 106 | |
| 107 | <Show |
| 108 | when={previewNotification()} |
| 109 | fallback={ |
| 110 | <div class="p-2"> |
| 111 | <div class={cn("mt-0 space-y-1 overflow-y-auto", props.scrollClass ?? "max-h-[min(30rem,calc(100dvh-9rem))]")}> |
| 112 | <Suspense |
| 113 | fallback={ |
| 114 | <div class="p-8 text-center text-xs text-text-subtle"> |
| 115 | {t("common.loading")} |
| 116 | </div> |
| 117 | } |
| 118 | > |
| 119 | <Show |
| 120 | when={props.feed.groups().length > 0} |
| 121 | fallback={ |
| 122 | <div class="space-y-2 p-8 text-center"> |
| 123 | <div class="mx-auto flex h-10 w-10 items-center justify-center rounded-full bg-surface-tint text-text-subtle"> |
| 124 | <IconBell class="h-5 w-5" /> |
| 125 | </div> |
| 126 | <p class="text-xs font-medium text-text-subtle"> |
| 127 | {t("notifications.empty")} |
| 128 | </p> |
| 129 | </div> |
| 130 | } |
| 131 | > |
| 132 | <div class="space-y-2"> |
| 133 | <For each={props.feed.groups()}> |
| 134 | {(group) => ( |
| 135 | <section class={cn("overflow-hidden rounded-xl border", notificationGroupTone(group.type))}> |
| 136 | <div class="flex items-center justify-between gap-2 px-2.5 py-2"> |
| 137 | <div class="flex min-w-0 items-center gap-2"> |
| 138 | <div class={cn("flex h-6 w-6 shrink-0 items-center justify-center rounded-md", notificationGroupIconTone(group.type))}> |
| 139 | <Show when={group.type === "message"}> |
| 140 | <IconMessage class="h-3.5 w-3.5" /> |
| 141 | </Show> |
| 142 | <Show when={group.type === "event"}> |
| 143 | <IconCalendar class="h-3.5 w-3.5" /> |
| 144 | </Show> |
| 145 | <Show when={group.type === "exam"}> |
| 146 | <IconExam class="h-3.5 w-3.5" /> |
| 147 | </Show> |
| 148 | <Show when={group.type === "homework"}> |
| 149 | <IconHomework class="h-3.5 w-3.5" /> |
| 150 | </Show> |
| 151 | </div> |
| 152 | <span class="truncate text-[11px] font-bold uppercase tracking-[0.08em] text-text-subtle"> |
| 153 | {notificationGroupLabel(group.type)} |
| 154 | </span> |
| 155 | </div> |
| 156 | <span class="shrink-0 rounded-full bg-background/70 px-1.5 py-0.5 font-mono text-[11px] font-bold text-text-subtle"> |
| 157 | {group.items.length} |
| 158 | </span> |
| 159 | </div> |
| 160 | |
| 161 | <div class="space-y-0.5 px-1 pb-1"> |
| 162 | <For each={group.items}> |
| 163 | {(item) => ( |
| 164 | <div |
| 165 | role="button" |
| 166 | tabIndex={0} |
| 167 | onClick={() => select(item)} |
| 168 | onKeyDown={(event) => { |
| 169 | if (event.target !== event.currentTarget) return; |
| 170 | if (event.key === "Enter" || event.key === " ") { |
| 171 | event.preventDefault(); |
| 172 | select(item); |
| 173 | } |
| 174 | }} |
| 175 | class="group relative flex cursor-pointer select-none items-start gap-3 rounded-lg p-2 text-xs outline-hidden transition-colors hover:bg-background/70 focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring" |
| 176 | > |
| 177 | <div class="mt-0.5 flex h-8 w-8 shrink-0 items-center justify-center rounded-lg bg-background/75 text-text-subtle transition-colors group-hover:bg-primary group-hover:text-primary-foreground"> |
| 178 | <Show when={item.type === "message"}> |
| 179 | <IconMessage class="h-4 w-4" /> |
| 180 | </Show> |
| 181 | <Show when={item.type === "event"}> |
| 182 | <IconCalendar class="h-4 w-4" /> |
| 183 | </Show> |
| 184 | <Show when={item.type === "exam"}> |
| 185 | <IconExam class="h-4 w-4" /> |
| 186 | </Show> |
| 187 | <Show when={item.type === "homework"}> |
| 188 | <IconHomework class="h-4 w-4" /> |
| 189 | </Show> |
| 190 | </div> |
| 191 | |
| 192 | <div class="min-w-0 flex-1 space-y-0.5 pr-6 touch:pr-10"> |
| 193 | <span class="block truncate font-semibold text-text-strong"> |
| 194 | {item.title} |
| 195 | </span> |
| 196 | <p class="line-clamp-2 text-[11px] leading-snug text-text-subtle"> |
| 197 | {item.description} |
| 198 | </p> |
| 199 | </div> |
| 200 | |
| 201 | <button |
| 202 | type="button" |
| 203 | // A thumb-sized target on touch screens, where |
| 204 | // there is no hover to reveal it either. |
| 205 | class="absolute right-2 top-2 flex h-6 w-6 items-center justify-center rounded-md text-text-subtle/70 opacity-80 transition-all hover:bg-destructive/10 hover:text-destructive-text outline-hidden focus-visible:ring-2 focus-visible:ring-ring sm:opacity-0 sm:group-hover:opacity-100 sm:group-focus-within:opacity-100 sm:focus-visible:opacity-100 touch:right-0 touch:top-0 touch:h-11 touch:w-11 touch:opacity-80" |
| 206 | title={t("notifications.dismiss")} |
| 207 | aria-label={t("notifications.dismiss")} |
| 208 | onClick={(e) => dismissOne(e, item.id)} |
| 209 | > |
| 210 | <IconX class="h-3.5 w-3.5" /> |
| 211 | </button> |
| 212 | </div> |
| 213 | )} |
| 214 | </For> |
| 215 | </div> |
| 216 | </section> |
| 217 | )} |
| 218 | </For> |
| 219 | </div> |
| 220 | </Show> |
| 221 | </Suspense> |
| 222 | </div> |
| 223 | </div> |
| 224 | } |
| 225 | > |
| 226 | {(item) => ( |
| 227 | <div class="space-y-4 p-4"> |
| 228 | <div class="flex items-center gap-2"> |
| 229 | <Button |
| 230 | type="button" |
| 231 | variant="ghost" |
| 232 | size="sm" |
| 233 | class="h-7 rounded-lg px-2 text-[11px] text-text-subtle" |
| 234 | onClick={() => setPreviewNotification(null)} |
| 235 | > |
| 236 | <IconChevronLeft class="mr-1 h-3.5 w-3.5" /> |
| 237 | {t("common.back")} |
| 238 | </Button> |
| 239 | </div> |
| 240 | |
| 241 | <div class="space-y-2"> |
| 242 | <div class="flex items-start gap-3"> |
| 243 | <div class={cn("flex h-10 w-10 shrink-0 items-center justify-center rounded-xl", notificationGroupIconTone(item().type))}> |
| 244 | <Show when={item().type === "message"}> |
| 245 | <IconMessage class="h-5 w-5" /> |
| 246 | </Show> |
| 247 | <Show when={item().type === "event"}> |
| 248 | <IconCalendar class="h-5 w-5" /> |
| 249 | </Show> |
| 250 | <Show when={item().type === "exam"}> |
| 251 | <IconExam class="h-5 w-5" /> |
| 252 | </Show> |
| 253 | <Show when={item().type === "homework"}> |
| 254 | <IconHomework class="h-5 w-5" /> |
| 255 | </Show> |
| 256 | </div> |
| 257 | <div class="min-w-0 space-y-1"> |
| 258 | <h4 class="text-sm font-semibold leading-snug text-text-strong">{item().title}</h4> |
| 259 | <p class="whitespace-pre-line text-xs leading-relaxed text-text-subtle"> |
| 260 | {item().preview ?? item().description} |
| 261 | </p> |
| 262 | </div> |
| 263 | </div> |
| 264 | </div> |
| 265 | |
| 266 | </div> |
| 267 | )} |
| 268 | </Show> |
| 269 | |
| 270 | <ConfirmDialog |
| 271 | open={confirmClear()} |
| 272 | onOpenChange={setConfirmClear} |
| 273 | title={t("notifications.clearAllConfirmTitle")} |
| 274 | summary={t("notifications.clearAllConfirmSummary", { count: totalCount() })} |
| 275 | confirmLabel={t("notifications.clearAll")} |
| 276 | variant="destructive" |
| 277 | onConfirm={() => props.feed.dismissAll()} |
| 278 | /> |
| 279 | </> |
| 280 | ); |
| 281 | } |