✨ draggable quick-action button replaces the header on phones
Browse files at this commitba7ff56f8bd1005f239f503ffdbc363d97abeaafParent: c274ff3- feat
- ✨ draggable quick-action button replaces the header on phones
13 changed files +1191 -432
- src/components/layout/app-shell.tsx +14 -3
- src/components/layout/floating-action-button.test.tsx +78 -0
- src/components/layout/floating-action-button.tsx +354 -0
- src/components/layout/mobile-quick-actions.tsx +81 -0
- src/components/layout/notification-center.tsx +18 -429
- src/components/layout/notification-feed.ts +211 -0
- src/components/layout/notification-list.tsx +272 -0
- src/i18n/locales/en.ts +3 -0
- src/i18n/locales/tr.ts +3 -0
- src/i18n/messages.ts +3 -0
- src/index.css +4 -0
- src/lib/floating-button.test.ts +75 -0
- src/lib/floating-button.ts +75 -0
src/components/layout/app-shell.tsxmodified+14 -3
| @@ -4,6 +4,7 @@ import { LogoMark } from "@/components/brand/logo-mark"; | |||
| 4 | import { AccountProfileDialog } from "@/components/users/account-profile-dialog"; | 4 | import { AccountProfileDialog } from "@/components/users/account-profile-dialog"; |
| 5 | import { CelebiPanel } from "@/components/layout/celebi-panel"; | 5 | import { CelebiPanel } from "@/components/layout/celebi-panel"; |
| 6 | import { CommandPalette } from "@/components/layout/command-palette"; | 6 | import { CommandPalette } from "@/components/layout/command-palette"; |
| 7 | +import { MobileQuickActions } from "@/components/layout/mobile-quick-actions"; | ||
| 7 | import { MobileTabBar } from "@/components/layout/mobile-tab-bar"; | 8 | import { MobileTabBar } from "@/components/layout/mobile-tab-bar"; |
| 8 | import { NavBar } from "@/components/layout/nav-bar"; | 9 | import { NavBar } from "@/components/layout/nav-bar"; |
| 9 | import { NetworkStatusBanner } from "@/components/layout/network-status-banner"; | 10 | import { NetworkStatusBanner } from "@/components/layout/network-status-banner"; |
| @@ -22,6 +23,7 @@ import { commandPaletteOpen, openCommandPalette, setCommandPaletteOpen } from "@ | |||
| 22 | import { ShellFeedProvider } from "@/stores/shell-feed-context"; | 23 | import { ShellFeedProvider } from "@/stores/shell-feed-context"; |
| 23 | import { usePreferences, useT } from "@/stores/preferences-context"; | 24 | import { usePreferences, useT } from "@/stores/preferences-context"; |
| 24 | import { cn } from "@/lib/cn"; | 25 | import { cn } from "@/lib/cn"; |
| 26 | +import { createMediaQuery } from "@/lib/create-media-query"; | ||
| 25 | import { ModuleGate } from "@/components/layout/module-gate"; | 27 | import { ModuleGate } from "@/components/layout/module-gate"; |
| 26 | 28 | ||
| 27 | const SIDEBAR_EXPANDED = "w-[260px]"; | 29 | const SIDEBAR_EXPANDED = "w-[260px]"; |
| @@ -38,6 +40,10 @@ export function AppShell(props: ParentProps) { | |||
| 38 | const collapsed = () => prefs.sidebarCollapsed(); | 40 | const collapsed = () => prefs.sidebarCollapsed(); |
| 39 | const location = useLocation(); | 41 | const location = useLocation(); |
| 40 | const fullScreen = () => location().pathname.startsWith("/exam-room/"); | 42 | const fullScreen = () => location().pathname.startsWith("/exam-room/"); |
| 43 | + // Below lg the header gives way to the floating action button. Chosen in | ||
| 44 | + // JS rather than with CSS so only one of the two notification surfaces is | ||
| 45 | + // mounted, and their dismissed state never has two in-memory copies. | ||
| 46 | + const phone = createMediaQuery("(max-width: 63.98rem)"); | ||
| 41 | const routeLabel = createMemo(() => { | 47 | const routeLabel = createMemo(() => { |
| 42 | const key = routeLabelKey(location().pathname, auth.user()?.role); | 48 | const key = routeLabelKey(location().pathname, auth.user()?.role); |
| 43 | return key ? t(key) : ""; | 49 | return key ? t(key) : ""; |
| @@ -130,7 +136,7 @@ export function AppShell(props: ParentProps) { | |||
| 130 | </Show> | 136 | </Show> |
| 131 | 137 | ||
| 132 | <main id="main-content" tabIndex={-1} class="min-w-0 flex-1 outline-hidden"> | 138 | <main id="main-content" tabIndex={-1} class="min-w-0 flex-1 outline-hidden"> |
| 133 | - <Show when={auth.user() && !fullScreen()}> | 139 | + <Show when={auth.user() && !fullScreen() && !phone()}> |
| 134 | <header class="sticky top-[env(safe-area-inset-top)] z-30 flex h-[49px] items-center gap-3 border-b border-border/70 bg-background px-4 pt-1.5 sm:px-6 lg:px-4"> | 140 | <header class="sticky top-[env(safe-area-inset-top)] z-30 flex h-[49px] items-center gap-3 border-b border-border/70 bg-background px-4 pt-1.5 sm:px-6 lg:px-4"> |
| 135 | <div class="flex min-w-0 shrink-0 items-center gap-2 sm:w-52 lg:w-[260px]"> | 141 | <div class="flex min-w-0 shrink-0 items-center gap-2 sm:w-52 lg:w-[260px]"> |
| 136 | <span class="hidden truncate text-sm font-semibold sm:block" title={routeLabel()}>{routeLabel()}</span> | 142 | <span class="hidden truncate text-sm font-semibold sm:block" title={routeLabel()}>{routeLabel()}</span> |
| @@ -175,11 +181,16 @@ export function AppShell(props: ParentProps) { | |||
| 175 | </div> | 181 | </div> |
| 176 | </main> | 182 | </main> |
| 177 | </div> | 183 | </div> |
| 178 | - {/* MobileTabBar stays inside the provider — it is a shell surface, so a | 184 | + {/* MobileTabBar and the quick actions stay inside the provider — they are |
| 179 | - useShellFeed() badge there must not throw. */} | 185 | + shell surfaces, so a useShellFeed() badge there must not throw. */} |
| 180 | <Show when={auth.user() && !fullScreen()}> | 186 | <Show when={auth.user() && !fullScreen()}> |
| 181 | <MobileTabBar onMenu={() => setMobileOpen(true)} onSearch={openCommandPalette} /> | 187 | <MobileTabBar onMenu={() => setMobileOpen(true)} onSearch={openCommandPalette} /> |
| 182 | </Show> | 188 | </Show> |
| 189 | + <Show when={auth.user() && !fullScreen() && phone()}> | ||
| 190 | + <MobileQuickActions | ||
| 191 | + hidden={mobileOpen() || celebiPanelOpen() || commandPaletteOpen() || profileOpen()} | ||
| 192 | + /> | ||
| 193 | + </Show> | ||
| 183 | </ShellFeedProvider> | 194 | </ShellFeedProvider> |
| 184 | <Show when={auth.user()}> | 195 | <Show when={auth.user()}> |
| 185 | <CelebiPanel open={celebiPanelOpen()} onOpenChange={setCelebiPanelOpen} /> | 196 | <CelebiPanel open={celebiPanelOpen()} onOpenChange={setCelebiPanelOpen} /> |
src/components/layout/floating-action-button.test.tsxadded+78 -0
| @@ -0,0 +1,78 @@ | |||
| 1 | +import { fireEvent, render, screen } from "@solidjs/testing-library"; | ||
| 2 | +import { FloatingActionButton } from "@/components/layout/floating-action-button"; | ||
| 3 | +import { IconBell } from "@/components/ui/icons"; | ||
| 4 | +import { PreferencesProvider } from "@/stores/preferences-context"; | ||
| 5 | + | ||
| 6 | +// A 400×700 layer: the hide target's top-left is (172, 632), the resting | ||
| 7 | +// bottom-right button's is (332, 632). | ||
| 8 | +const W = 400; | ||
| 9 | +const H = 700; | ||
| 10 | + | ||
| 11 | +function renderFab(onSelect = vi.fn()) { | ||
| 12 | + render(() => ( | ||
| 13 | + <PreferencesProvider> | ||
| 14 | + <FloatingActionButton | ||
| 15 | + actions={[{ id: "notifications", label: "Bildirimler", Icon: IconBell, badge: 2, onSelect }]} | ||
| 16 | + badge={3} | ||
| 17 | + hidden={false} | ||
| 18 | + /> | ||
| 19 | + </PreferencesProvider> | ||
| 20 | + )); | ||
| 21 | + return { fab: screen.getByRole("button", { name: /hızlı işlemler|quick actions/i, expanded: false }), onSelect }; | ||
| 22 | +} | ||
| 23 | + | ||
| 24 | +function drag(el: HTMLElement, from: [number, number], to: [number, number]) { | ||
| 25 | + fireEvent.pointerDown(el, { pointerId: 1, clientX: from[0], clientY: from[1], button: 0, pointerType: "touch" }); | ||
| 26 | + fireEvent.pointerMove(el, { pointerId: 1, clientX: (from[0] + to[0]) / 2, clientY: (from[1] + to[1]) / 2 }); | ||
| 27 | + fireEvent.pointerMove(el, { pointerId: 1, clientX: to[0], clientY: to[1] }); | ||
| 28 | + fireEvent.pointerUp(el, { pointerId: 1, clientX: to[0], clientY: to[1] }); | ||
| 29 | + fireEvent.click(el); | ||
| 30 | +} | ||
| 31 | + | ||
| 32 | +describe("FloatingActionButton", () => { | ||
| 33 | + beforeEach(() => { | ||
| 34 | + localStorage.clear(); | ||
| 35 | + vi.spyOn(HTMLElement.prototype, "getBoundingClientRect").mockReturnValue({ | ||
| 36 | + width: W, height: H, top: 0, left: 0, right: W, bottom: H, x: 0, y: 0, toJSON: () => ({}), | ||
| 37 | + } as DOMRect); | ||
| 38 | + HTMLElement.prototype.setPointerCapture = vi.fn(); | ||
| 39 | + }); | ||
| 40 | + afterEach(() => vi.restoreAllMocks()); | ||
| 41 | + | ||
| 42 | + it("fans its actions out on a tap and runs the one picked", () => { | ||
| 43 | + const { fab, onSelect } = renderFab(); | ||
| 44 | + fireEvent.pointerDown(fab, { pointerId: 1, clientX: 10, clientY: 10, button: 0 }); | ||
| 45 | + fireEvent.pointerUp(fab, { pointerId: 1, clientX: 12, clientY: 11 }); | ||
| 46 | + fireEvent.click(fab); | ||
| 47 | + expect(fab.getAttribute("aria-expanded")).toBe("true"); | ||
| 48 | + | ||
| 49 | + fireEvent.click(screen.getByRole("button", { name: "Bildirimler" })); | ||
| 50 | + expect(onSelect).toHaveBeenCalledOnce(); | ||
| 51 | + expect(fab.getAttribute("aria-expanded")).toBe("false"); | ||
| 52 | + }); | ||
| 53 | + | ||
| 54 | + it("remembers where it was dropped, and a drag is not a tap", () => { | ||
| 55 | + const { fab } = renderFab(); | ||
| 56 | + drag(fab, [360, 660], [60, 300]); | ||
| 57 | + expect(fab.getAttribute("aria-expanded")).toBe("false"); | ||
| 58 | + const saved = JSON.parse(localStorage.getItem("hezarfen.floatingButton")!); | ||
| 59 | + expect(saved.parked).toBe(false); | ||
| 60 | + expect(saved.x).toBeLessThan(0.1); | ||
| 61 | + expect(saved.y).toBeGreaterThan(0.3); | ||
| 62 | + expect(saved.y).toBeLessThan(0.5); | ||
| 63 | + }); | ||
| 64 | + | ||
| 65 | + it("parks on the edge when dropped on the hide target and comes back with one tap", () => { | ||
| 66 | + const { fab } = renderFab(); | ||
| 67 | + // 332 → 172 horizontally lands the button on the hide target. | ||
| 68 | + drag(fab, [360, 660], [200, 660]); | ||
| 69 | + expect(screen.queryByRole("button", { name: /hızlı işlemler|quick actions/i, expanded: false })).toBeNull(); | ||
| 70 | + const saved = JSON.parse(localStorage.getItem("hezarfen.floatingButton")!); | ||
| 71 | + // Parked, but still remembering the spot it was dragged from. | ||
| 72 | + expect(saved).toEqual({ x: 1, y: 1, parked: true }); | ||
| 73 | + | ||
| 74 | + fireEvent.click(screen.getByRole("button", { name: /göster|show quick actions/i })); | ||
| 75 | + expect(screen.getByRole("button", { name: /hızlı işlemler|quick actions/i, expanded: false })).toBeTruthy(); | ||
| 76 | + expect(JSON.parse(localStorage.getItem("hezarfen.floatingButton")!).parked).toBe(false); | ||
| 77 | + }); | ||
| 78 | +}); | ||
src/components/layout/floating-action-button.tsxadded+354 -0
| @@ -0,0 +1,354 @@ | |||
| 1 | +import { Index, Show, createEffect, createSignal, onCleanup, onMount, type Component } from "solid-js"; | ||
| 2 | +import { IconChevronLeft, IconChevronRight, IconPlus, IconX } from "@/components/ui/icons"; | ||
| 3 | +import { cn } from "@/lib/cn"; | ||
| 4 | +import { clamp01, radialOffsets, readFloatingButton, writeFloatingButton } from "@/lib/floating-button"; | ||
| 5 | +import { useT } from "@/stores/preferences-context"; | ||
| 6 | + | ||
| 7 | +export interface FloatingAction { | ||
| 8 | + id: string; | ||
| 9 | + label: string; | ||
| 10 | + Icon: Component<{ class?: string }>; | ||
| 11 | + badge?: number; | ||
| 12 | + onSelect: () => void; | ||
| 13 | +} | ||
| 14 | + | ||
| 15 | +/** Button diameter. */ | ||
| 16 | +const SIZE = 56; | ||
| 17 | +/** Gap the resting button keeps from the layer's edges. */ | ||
| 18 | +const MARGIN = 12; | ||
| 19 | +/** Radial item diameter. */ | ||
| 20 | +const ITEM = 44; | ||
| 21 | +/** Distance from the button centre to each radial item centre. */ | ||
| 22 | +const RADIUS = 84; | ||
| 23 | +/** Movement under this is a tap, not a drag — a thumb is never perfectly still. */ | ||
| 24 | +const TAP_SLOP = 8; | ||
| 25 | +/** How close the button centre has to come to the hide target to snap into it. */ | ||
| 26 | +const HIDE_REACH = 64; | ||
| 27 | + | ||
| 28 | +const badgeText = (count: number) => (count > 9 ? "9+" : String(count)); | ||
| 29 | +const clamp = (value: number, min: number, max: number) => Math.min(max, Math.max(min, value)); | ||
| 30 | + | ||
| 31 | +/** | ||
| 32 | + * The phone shell's floating action button, standing in for the desktop | ||
| 33 | + * header. Tapping it fans its actions out around it; dragging moves it | ||
| 34 | + * anywhere between the status bar and the tab bar; dropping it on the hide | ||
| 35 | + * target that rises while dragging parks it as a small tab on the nearest | ||
| 36 | + * side edge, and one tap on that tab brings it back where it was. | ||
| 37 | + * | ||
| 38 | + * Position and parked state are remembered per device (lib/floating-button). | ||
| 39 | + * Drag follows the pointer-event pattern of MobileNavSheet: pointer capture on | ||
| 40 | + * the button itself, touch-none so the browser does not claim the gesture for | ||
| 41 | + * scrolling. | ||
| 42 | + */ | ||
| 43 | +export function FloatingActionButton(props: { | ||
| 44 | + actions: FloatingAction[]; | ||
| 45 | + /** Unread total shown on the button and, as a dot, on the parked tab. */ | ||
| 46 | + badge: number; | ||
| 47 | + /** An overlay owns the screen; step aside without losing state. */ | ||
| 48 | + hidden: boolean; | ||
| 49 | +}) { | ||
| 50 | + const t = useT(); | ||
| 51 | + const saved = readFloatingButton(); | ||
| 52 | + const [pos, setPos] = createSignal({ x: saved.x, y: saved.y }); | ||
| 53 | + const [parked, setParked] = createSignal(saved.parked); | ||
| 54 | + const [open, setOpen] = createSignal(false); | ||
| 55 | + const [bounds, setBounds] = createSignal({ w: 0, h: 0 }); | ||
| 56 | + // Live top-left while a drag is under way; null at rest. | ||
| 57 | + const [drag, setDrag] = createSignal<{ left: number; top: number } | null>(null); | ||
| 58 | + const [overHide, setOverHide] = createSignal(false); | ||
| 59 | + | ||
| 60 | + let layer: HTMLDivElement | undefined; | ||
| 61 | + let fab: HTMLButtonElement | undefined; | ||
| 62 | + const measure = () => { | ||
| 63 | + if (!layer) return; | ||
| 64 | + const rect = layer.getBoundingClientRect(); | ||
| 65 | + setBounds({ w: rect.width, h: rect.height }); | ||
| 66 | + }; | ||
| 67 | + onMount(() => { | ||
| 68 | + measure(); | ||
| 69 | + window.addEventListener("resize", measure); | ||
| 70 | + onCleanup(() => window.removeEventListener("resize", measure)); | ||
| 71 | + }); | ||
| 72 | + | ||
| 73 | + const persist = () => writeFloatingButton({ ...pos(), parked: parked() }); | ||
| 74 | + | ||
| 75 | + const travelX = () => Math.max(0, bounds().w - SIZE - 2 * MARGIN); | ||
| 76 | + const travelY = () => Math.max(0, bounds().h - SIZE - 2 * MARGIN); | ||
| 77 | + const restLeft = () => MARGIN + pos().x * travelX(); | ||
| 78 | + const restTop = () => MARGIN + pos().y * travelY(); | ||
| 79 | + const hideLeft = () => bounds().w / 2 - SIZE / 2; | ||
| 80 | + const hideTop = () => bounds().h - SIZE - MARGIN; | ||
| 81 | + const left = () => (overHide() ? hideLeft() : (drag()?.left ?? restLeft())); | ||
| 82 | + const top = () => (overHide() ? hideTop() : (drag()?.top ?? restTop())); | ||
| 83 | + | ||
| 84 | + createEffect(() => { | ||
| 85 | + if (props.hidden) setOpen(false); | ||
| 86 | + }); | ||
| 87 | + createEffect(() => { | ||
| 88 | + if (!open()) return; | ||
| 89 | + const onKey = (event: KeyboardEvent) => { | ||
| 90 | + if (event.key !== "Escape") return; | ||
| 91 | + setOpen(false); | ||
| 92 | + fab?.focus(); | ||
| 93 | + }; | ||
| 94 | + window.addEventListener("keydown", onKey); | ||
| 95 | + onCleanup(() => window.removeEventListener("keydown", onKey)); | ||
| 96 | + }); | ||
| 97 | + | ||
| 98 | + let start: { x: number; y: number; left: number; top: number } | null = null; | ||
| 99 | + let moved = false; | ||
| 100 | + // A drag that ends over the button is followed by a click on mouse (not | ||
| 101 | + // always on touch), which must not toggle the menu. Reset on every press so | ||
| 102 | + // a swallowed click cannot leave the next real tap ignored. | ||
| 103 | + let suppressClick = false; | ||
| 104 | + | ||
| 105 | + const startDrag = (event: PointerEvent) => { | ||
| 106 | + if (event.pointerType === "mouse" && event.button !== 0) return; | ||
| 107 | + suppressClick = false; | ||
| 108 | + moved = false; | ||
| 109 | + start = { x: event.clientX, y: event.clientY, left: left(), top: top() }; | ||
| 110 | + (event.currentTarget as HTMLElement).setPointerCapture(event.pointerId); | ||
| 111 | + }; | ||
| 112 | + | ||
| 113 | + const moveDrag = (event: PointerEvent) => { | ||
| 114 | + if (!start) return; | ||
| 115 | + const dx = event.clientX - start.x; | ||
| 116 | + const dy = event.clientY - start.y; | ||
| 117 | + if (!moved && Math.hypot(dx, dy) < TAP_SLOP) return; | ||
| 118 | + if (!moved) { | ||
| 119 | + moved = true; | ||
| 120 | + setOpen(false); | ||
| 121 | + } | ||
| 122 | + const next = { | ||
| 123 | + left: clamp(start.left + dx, 0, bounds().w - SIZE), | ||
| 124 | + top: clamp(start.top + dy, 0, bounds().h - SIZE), | ||
| 125 | + }; | ||
| 126 | + setDrag(next); | ||
| 127 | + const hit = Math.hypot(next.left - hideLeft(), next.top - hideTop()) < HIDE_REACH; | ||
| 128 | + if (hit && !overHide()) navigator.vibrate?.(10); | ||
| 129 | + setOverHide(hit); | ||
| 130 | + }; | ||
| 131 | + | ||
| 132 | + const endDrag = (commit: boolean) => { | ||
| 133 | + if (!start) return; | ||
| 134 | + start = null; | ||
| 135 | + if (!moved) return; | ||
| 136 | + suppressClick = true; | ||
| 137 | + const last = drag(); | ||
| 138 | + if (commit && overHide()) { | ||
| 139 | + // Parked at the spot it was dragged from, so a tap on the tab puts it | ||
| 140 | + // back there rather than on the hide target. | ||
| 141 | + setParked(true); | ||
| 142 | + persist(); | ||
| 143 | + } else if (commit && last) { | ||
| 144 | + setPos({ | ||
| 145 | + x: travelX() ? clamp01((last.left - MARGIN) / travelX()) : pos().x, | ||
| 146 | + y: travelY() ? clamp01((last.top - MARGIN) / travelY()) : pos().y, | ||
| 147 | + }); | ||
| 148 | + persist(); | ||
| 149 | + } | ||
| 150 | + setDrag(null); | ||
| 151 | + setOverHide(false); | ||
| 152 | + }; | ||
| 153 | + | ||
| 154 | + const toggle = () => { | ||
| 155 | + if (suppressClick) { | ||
| 156 | + suppressClick = false; | ||
| 157 | + return; | ||
| 158 | + } | ||
| 159 | + setOpen(!open()); | ||
| 160 | + }; | ||
| 161 | + | ||
| 162 | + const restore = () => { | ||
| 163 | + setParked(false); | ||
| 164 | + persist(); | ||
| 165 | + }; | ||
| 166 | + | ||
| 167 | + const select = (action: FloatingAction) => { | ||
| 168 | + setOpen(false); | ||
| 169 | + action.onSelect(); | ||
| 170 | + }; | ||
| 171 | + | ||
| 172 | + const centreX = () => left() + SIZE / 2; | ||
| 173 | + const centreY = () => top() + SIZE / 2; | ||
| 174 | + const offsets = () => | ||
| 175 | + radialOffsets( | ||
| 176 | + props.actions.length, | ||
| 177 | + bounds().w ? centreX() / bounds().w : 1, | ||
| 178 | + bounds().h ? centreY() / bounds().h : 1, | ||
| 179 | + RADIUS, | ||
| 180 | + ); | ||
| 181 | + // Top-left of item i, kept inside the layer as a last resort for fans the | ||
| 182 | + // heading alone does not keep on screen. | ||
| 183 | + const itemPos = (i: number) => { | ||
| 184 | + const offset = offsets()[i] ?? { dx: 0, dy: 0 }; | ||
| 185 | + return { | ||
| 186 | + left: clamp(centreX() + offset.dx - ITEM / 2, 4, bounds().w - ITEM - 4), | ||
| 187 | + top: clamp(centreY() + offset.dy - ITEM / 2, 4, bounds().h - ITEM - 4), | ||
| 188 | + }; | ||
| 189 | + }; | ||
| 190 | + const parkedRight = () => pos().x >= 0.5; | ||
| 191 | + const dragging = () => drag() !== null; | ||
| 192 | + | ||
| 193 | + return ( | ||
| 194 | + <> | ||
| 195 | + <Show when={open()}> | ||
| 196 | + <button | ||
| 197 | + type="button" | ||
| 198 | + tabIndex={-1} | ||
| 199 | + aria-label={t("nav.close")} | ||
| 200 | + class="fixed inset-0 z-50 cursor-default bg-black/25 lg:hidden" | ||
| 201 | + onClick={() => setOpen(false)} | ||
| 202 | + /> | ||
| 203 | + </Show> | ||
| 204 | + | ||
| 205 | + {/* The layer spans the space between the status bar and the tab bar, so | ||
| 206 | + every position inside it is reachable and none hides under either. | ||
| 207 | + It stays mounted while hidden or parked to keep its measurements. */} | ||
| 208 | + <div | ||
| 209 | + ref={(el) => { | ||
| 210 | + layer = el; | ||
| 211 | + }} | ||
| 212 | + class={cn( | ||
| 213 | + "pointer-events-none fixed inset-x-0 top-[env(safe-area-inset-top)] z-50 transition-opacity duration-200 lg:hidden", | ||
| 214 | + "bottom-[calc(3.5rem+max(env(safe-area-inset-bottom),var(--android-nav-inset,0px)))]", | ||
| 215 | + props.hidden || !bounds().w ? "invisible opacity-0" : "visible opacity-100", | ||
| 216 | + )} | ||
| 217 | + > | ||
| 218 | + <Show when={dragging()}> | ||
| 219 | + <div class="absolute inset-x-0 bottom-0 h-44 bg-gradient-to-t from-black/30 to-transparent" /> | ||
| 220 | + <div | ||
| 221 | + class="absolute flex flex-col items-center" | ||
| 222 | + style={{ left: `${hideLeft()}px`, top: `${hideTop()}px`, width: `${SIZE}px` }} | ||
| 223 | + > | ||
| 224 | + <span class="absolute -top-7 whitespace-nowrap rounded-full bg-background/90 px-2 py-0.5 text-[11px] font-semibold text-foreground"> | ||
| 225 | + {t("fab.hide")} | ||
| 226 | + </span> | ||
| 227 | + <span | ||
| 228 | + class={cn( | ||
| 229 | + "flex items-center justify-center rounded-full border-2 transition-all duration-150", | ||
| 230 | + overHide() | ||
| 231 | + ? "h-[68px] w-[68px] -m-1.5 border-destructive bg-destructive text-destructive-foreground" | ||
| 232 | + : "h-14 w-14 border-white/80 bg-black/40 text-white", | ||
| 233 | + )} | ||
| 234 | + > | ||
| 235 | + <IconX class="h-6 w-6" /> | ||
| 236 | + </span> | ||
| 237 | + </div> | ||
| 238 | + </Show> | ||
| 239 | + | ||
| 240 | + <Show when={!parked()}> | ||
| 241 | + <div id="fab-actions" role="group" aria-label={t("fab.label")}> | ||
| 242 | + <Index each={props.actions}> | ||
| 243 | + {(action, i) => { | ||
| 244 | + const at = () => (open() ? itemPos(i) : { left: centreX() - ITEM / 2, top: centreY() - ITEM / 2 }); | ||
| 245 | + return ( | ||
| 246 | + <button | ||
| 247 | + type="button" | ||
| 248 | + tabIndex={open() ? 0 : -1} | ||
| 249 | + aria-hidden={open() ? undefined : "true"} | ||
| 250 | + aria-label={action().label} | ||
| 251 | + title={action().label} | ||
| 252 | + class={cn( | ||
| 253 | + "absolute left-0 top-0 flex items-center justify-center rounded-full border border-border bg-surface-base text-foreground shadow-lg outline-hidden", | ||
| 254 | + "transition-[transform,opacity] duration-200 ease-out focus-visible:ring-2 focus-visible:ring-ring", | ||
| 255 | + open() ? "pointer-events-auto opacity-100" : "pointer-events-none opacity-0", | ||
| 256 | + )} | ||
| 257 | + style={{ | ||
| 258 | + width: `${ITEM}px`, | ||
| 259 | + height: `${ITEM}px`, | ||
| 260 | + transform: `translate3d(${at().left}px, ${at().top}px, 0) scale(${open() ? 1 : 0.5})`, | ||
| 261 | + "transition-delay": open() ? `${i * 30}ms` : "0ms", | ||
| 262 | + }} | ||
| 263 | + onClick={() => select(action())} | ||
| 264 | + > | ||
| 265 | + {(() => { | ||
| 266 | + const Icon = action().Icon; | ||
| 267 | + return <Icon class="h-5 w-5" />; | ||
| 268 | + })()} | ||
| 269 | + <Show when={(action().badge ?? 0) > 0}> | ||
| 270 | + <span class="absolute -right-1 -top-1 flex h-5 min-w-5 items-center justify-center rounded-full bg-destructive px-1 text-[11px] font-bold text-destructive-foreground ring-2 ring-background"> | ||
| 271 | + {badgeText(action().badge ?? 0)} | ||
| 272 | + </span> | ||
| 273 | + </Show> | ||
| 274 | + </button> | ||
| 275 | + ); | ||
| 276 | + }} | ||
| 277 | + </Index> | ||
| 278 | + </div> | ||
| 279 | + | ||
| 280 | + <button | ||
| 281 | + type="button" | ||
| 282 | + ref={(el) => { | ||
| 283 | + fab = el; | ||
| 284 | + }} | ||
| 285 | + aria-label={t("fab.label")} | ||
| 286 | + aria-expanded={open()} | ||
| 287 | + aria-controls="fab-actions" | ||
| 288 | + class={cn( | ||
| 289 | + "pointer-events-auto absolute left-0 top-0 flex touch-none select-none items-center justify-center rounded-full bg-primary text-primary-foreground shadow-lg shadow-black/20 outline-hidden", | ||
| 290 | + "focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background", | ||
| 291 | + dragging() && "shadow-xl", | ||
| 292 | + overHide() && "opacity-80", | ||
| 293 | + )} | ||
| 294 | + // Scale lives in the transform, after the translate: Tailwind's | ||
| 295 | + // scale-* sets the separate `scale` property, which applies after | ||
| 296 | + // `transform` and would scale the position along with the button. | ||
| 297 | + style={{ | ||
| 298 | + width: `${SIZE}px`, | ||
| 299 | + height: `${SIZE}px`, | ||
| 300 | + transform: `translate3d(${left()}px, ${top()}px, 0) scale(${overHide() ? 0.9 : dragging() ? 1.05 : 1})`, | ||
| 301 | + transition: dragging() && !overHide() ? "none" : "transform 200ms ease-out, opacity 150ms", | ||
| 302 | + }} | ||
| 303 | + onPointerDown={startDrag} | ||
| 304 | + onPointerMove={moveDrag} | ||
| 305 | + onPointerUp={() => endDrag(true)} | ||
| 306 | + onPointerCancel={() => endDrag(false)} | ||
| 307 | + onClick={toggle} | ||
| 308 | + > | ||
| 309 | + <IconPlus class={cn("h-6 w-6 transition-transform duration-200", open() && "rotate-45")} /> | ||
| 310 | + <Show when={props.badge > 0 && !open()}> | ||
| 311 | + <span class="absolute -right-0.5 -top-0.5 flex h-5 min-w-5 items-center justify-center rounded-full bg-destructive px-1 text-[11px] font-bold text-destructive-foreground ring-2 ring-background"> | ||
| 312 | + {badgeText(props.badge)} | ||
| 313 | + </span> | ||
| 314 | + </Show> | ||
| 315 | + </button> | ||
| 316 | + </Show> | ||
| 317 | + | ||
| 318 | + <Show when={parked()}> | ||
| 319 | + {/* Parked: a slim tab on the side the button rested nearest. The | ||
| 320 | + visible pill is narrow; the button around it is a full thumb | ||
| 321 | + target. */} | ||
| 322 | + <button | ||
| 323 | + type="button" | ||
| 324 | + aria-label={t("fab.restore")} | ||
| 325 | + title={t("fab.restore")} | ||
| 326 | + class={cn( | ||
| 327 | + "pointer-events-auto absolute flex h-14 w-8 items-center outline-hidden focus-visible:ring-2 focus-visible:ring-ring", | ||
| 328 | + parkedRight() ? "right-0 justify-end" : "left-0 justify-start", | ||
| 329 | + )} | ||
| 330 | + style={{ top: `${restTop()}px` }} | ||
| 331 | + onClick={restore} | ||
| 332 | + > | ||
| 333 | + <span | ||
| 334 | + class={cn( | ||
| 335 | + "relative flex h-12 w-4 items-center justify-center bg-foreground/35 text-background backdrop-blur-sm", | ||
| 336 | + parkedRight() ? "rounded-l-full" : "rounded-r-full", | ||
| 337 | + )} | ||
| 338 | + > | ||
| 339 | + {parkedRight() ? <IconChevronLeft class="h-3.5 w-3.5" /> : <IconChevronRight class="h-3.5 w-3.5" />} | ||
| 340 | + <Show when={props.badge > 0}> | ||
| 341 | + <span | ||
| 342 | + class={cn( | ||
| 343 | + "absolute -top-1 h-2.5 w-2.5 rounded-full bg-destructive ring-2 ring-background", | ||
| 344 | + parkedRight() ? "left-0" : "right-0", | ||
| 345 | + )} | ||
| 346 | + /> | ||
| 347 | + </Show> | ||
| 348 | + </span> | ||
| 349 | + </button> | ||
| 350 | + </Show> | ||
| 351 | + </div> | ||
| 352 | + </> | ||
| 353 | + ); | ||
| 354 | +} | ||
src/components/layout/mobile-quick-actions.tsxadded+81 -0
| @@ -0,0 +1,81 @@ | |||
| 1 | +import { createEffect, createMemo, createSignal } from "solid-js"; | ||
| 2 | +import { useNavigate } from "@tanstack/solid-router"; | ||
| 3 | +import { FloatingActionButton, type FloatingAction } from "@/components/layout/floating-action-button"; | ||
| 4 | +import { createNotificationFeed } from "@/components/layout/notification-feed"; | ||
| 5 | +import { NotificationList } from "@/components/layout/notification-list"; | ||
| 6 | +import { SidePanel } from "@/components/ui/side-panel"; | ||
| 7 | +import { IconBell, IconMessage, IconSparkles } from "@/components/ui/icons"; | ||
| 8 | +import { openCelebiPanel } from "@/stores/celebi-panel"; | ||
| 9 | +import { useModules } from "@/stores/modules-context"; | ||
| 10 | +import { useShellFeed } from "@/stores/shell-feed-context"; | ||
| 11 | +import { useT } from "@/stores/preferences-context"; | ||
| 12 | + | ||
| 13 | +/** | ||
| 14 | + * What the desktop header offers — messages, notifications, Çelebi — on a | ||
| 15 | + * phone, behind the floating action button. Notifications open in a panel | ||
| 16 | + * rather than the header's popover: a popover anchored to a button the user | ||
| 17 | + * can drag anywhere would open off screen. | ||
| 18 | + */ | ||
| 19 | +export function MobileQuickActions(props: { hidden: boolean }) { | ||
| 20 | + const t = useT(); | ||
| 21 | + const navigate = useNavigate(); | ||
| 22 | + const modules = useModules(); | ||
| 23 | + const feed = useShellFeed(); | ||
| 24 | + const notifications = createNotificationFeed(); | ||
| 25 | + const [notificationsOpen, setNotificationsOpen] = createSignal(false); | ||
| 26 | + | ||
| 27 | + createEffect(() => { | ||
| 28 | + if (notificationsOpen()) notifications.refreshAll(); | ||
| 29 | + }); | ||
| 30 | + | ||
| 31 | + const actions = createMemo(() => { | ||
| 32 | + const list: FloatingAction[] = []; | ||
| 33 | + if (modules.isEnabled("messages")) { | ||
| 34 | + list.push({ | ||
| 35 | + id: "messages", | ||
| 36 | + label: t("nav.messages"), | ||
| 37 | + Icon: IconMessage, | ||
| 38 | + badge: feed.unreadMessages().total, | ||
| 39 | + onSelect: () => void navigate({ to: "/messages" }), | ||
| 40 | + }); | ||
| 41 | + } | ||
| 42 | + list.push({ | ||
| 43 | + id: "notifications", | ||
| 44 | + label: t("notifications.title"), | ||
| 45 | + Icon: IconBell, | ||
| 46 | + badge: notifications.unreadCount(), | ||
| 47 | + onSelect: () => setNotificationsOpen(true), | ||
| 48 | + }); | ||
| 49 | + if (modules.isEnabled("chatbot")) { | ||
| 50 | + list.push({ id: "celebi", label: t("ai.askCelebi"), Icon: IconSparkles, onSelect: openCelebiPanel }); | ||
| 51 | + } | ||
| 52 | + return list; | ||
| 53 | + }); | ||
| 54 | + | ||
| 55 | + return ( | ||
| 56 | + <> | ||
| 57 | + {/* Unread messages are notifications too, so this one count covers both. */} | ||
| 58 | + <FloatingActionButton | ||
| 59 | + actions={actions()} | ||
| 60 | + badge={notifications.unreadCount()} | ||
| 61 | + hidden={props.hidden || notificationsOpen()} | ||
| 62 | + /> | ||
| 63 | + <SidePanel | ||
| 64 | + open={notificationsOpen()} | ||
| 65 | + onOpenChange={setNotificationsOpen} | ||
| 66 | + title={t("notifications.title")} | ||
| 67 | + bodyClass="px-2 py-2" | ||
| 68 | + > | ||
| 69 | + <NotificationList | ||
| 70 | + feed={notifications} | ||
| 71 | + showTitle={false} | ||
| 72 | + scrollClass="" | ||
| 73 | + onNavigate={(url) => { | ||
| 74 | + setNotificationsOpen(false); | ||
| 75 | + void navigate({ to: url }); | ||
| 76 | + }} | ||
| 77 | + /> | ||
| 78 | + </SidePanel> | ||
| 79 | + </> | ||
| 80 | + ); | ||
| 81 | +} | ||
src/components/layout/notification-center.tsxmodified+18 -429
| @@ -1,259 +1,29 @@ | |||
| 1 | -import { For, Show, Suspense, createEffect, createMemo, createSignal } from "solid-js"; | 1 | +import { Show, createEffect, createSignal } from "solid-js"; |
| 2 | import { useNavigate } from "@tanstack/solid-router"; | 2 | import { useNavigate } from "@tanstack/solid-router"; |
| 3 | -import { patchMessageById } from "@/api/messages"; | ||
| 4 | -import { Button } from "@/components/ui/button"; | ||
| 5 | -import { ComingSoonPanel } from "@/components/ui/coming-soon"; | ||
| 6 | import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover"; | 3 | import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover"; |
| 7 | -import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"; | 4 | +import { IconBell } from "@/components/ui/icons"; |
| 8 | -import { | ||
| 9 | - IconBell, | ||
| 10 | - IconCalendar, | ||
| 11 | - IconChevronLeft, | ||
| 12 | - IconExam, | ||
| 13 | - IconExternalLink, | ||
| 14 | - IconHomework, | ||
| 15 | - IconMessage, | ||
| 16 | - IconTrash, | ||
| 17 | - IconX, | ||
| 18 | -} from "@/components/ui/icons"; | ||
| 19 | import { cn } from "@/lib/cn"; | 5 | import { cn } from "@/lib/cn"; |
| 20 | -import { personLabel } from "@/lib/person"; | 6 | +import { createNotificationFeed } from "@/components/layout/notification-feed"; |
| 21 | -import { | 7 | +import { NotificationList } from "@/components/layout/notification-list"; |
| 22 | - dismissAllNotificationIds, | 8 | +import { useT } from "@/stores/preferences-context"; |
| 23 | - dismissNotificationId, | ||
| 24 | - getDismissedNotificationIds, | ||
| 25 | - getReadNotificationIds, | ||
| 26 | - markNotificationRead, | ||
| 27 | - type NotificationItem, | ||
| 28 | - type NotificationType, | ||
| 29 | -} from "@/lib/notifications"; | ||
| 30 | -import { usePreferences } from "@/stores/preferences-context"; | ||
| 31 | -import { useShellFeed } from "@/stores/shell-feed-context"; | ||
| 32 | 9 | ||
| 10 | +/** The header bell: the notification list in a popover. */ | ||
| 33 | export function NotificationCenter() { | 11 | export function NotificationCenter() { |
| 34 | - const { t, locale } = usePreferences(); | 12 | + const t = useT(); |
| 35 | - const dateLocale = () => (locale() === "tr" ? "tr-TR" : "en-US"); | ||
| 36 | const navigate = useNavigate(); | 13 | const navigate = useNavigate(); |
| 37 | - const feed = useShellFeed(); | 14 | + const feed = createNotificationFeed(); |
| 38 | const [open, setOpen] = createSignal(false); | 15 | const [open, setOpen] = createSignal(false); |
| 39 | - const [dismissedIds, setDismissedIds] = createSignal<Set<string>>(getDismissedNotificationIds()); | ||
| 40 | - const [readIds, setReadIds] = createSignal<Set<string>>(getReadNotificationIds()); | ||
| 41 | - const [previewNotification, setPreviewNotification] = createSignal<NotificationItem | null>(null); | ||
| 42 | - | ||
| 43 | - // Shell feed data comes from the shared poller (ShellFeedProvider): one fetch | ||
| 44 | - // per source for the whole shell instead of a duplicate set here. Every read | ||
| 45 | - // below is `feed.x()` which returns `resource.latest` — non-suspending on the | ||
| 46 | - // 60s refetch, so the unread badge (outside the popover's <Suspense>) never | ||
| 47 | - // blanks the page beside <Outlet> (AGENTS.md #6). | ||
| 48 | - const refetchMessages = feed.refetchMessages; | ||
| 49 | - const refreshAll = feed.refreshAll; | ||
| 50 | - const nowMs = feed.nowMs; | ||
| 51 | 16 | ||
| 52 | createEffect(() => { | 17 | createEffect(() => { |
| 53 | - if (open()) refreshAll(); | 18 | + if (open()) feed.refreshAll(); |
| 54 | }); | 19 | }); |
| 55 | 20 | ||
| 56 | - // An item is still notifiable until it has ended (ends_at, else starts_at). | 21 | + const unreadCount = feed.unreadCount; |
| 57 | - const notEnded = (starts: number, ends?: number | null) => (ends ?? starts) >= nowMs(); | ||
| 58 | - | ||
| 59 | - // Combine notification items | ||
| 60 | - const allNotifications = createMemo<NotificationItem[]>(() => { | ||
| 61 | - const list: NotificationItem[] = []; | ||
| 62 | - | ||
| 63 | - // 1. Unread messages — the dedicated `read=false` page from the shell feed. | ||
| 64 | - // Never filter the shared inbox page instead: it is capped and ordered by | ||
| 65 | - // id DESC, so an older unread message is simply not in it. | ||
| 66 | - const msgs = feed.unreadMessages().items; | ||
| 67 | - for (const m of msgs) { | ||
| 68 | - const body = m.body.replace(/<[^>]*>?/gm, "").trim(); | ||
| 69 | - list.push({ | ||
| 70 | - id: `msg_${m.id}`, | ||
| 71 | - type: "message", | ||
| 72 | - title: personLabel(m.sender), | ||
| 73 | - description: m.subject || body, | ||
| 74 | - preview: m.subject ? `${m.subject}\n${body}` : body, | ||
| 75 | - targetUrl: "/messages", | ||
| 76 | - timestamp: m.sent_at, | ||
| 77 | - }); | ||
| 78 | - } | ||
| 79 | - | ||
| 80 | - // 2. Events starting today or upcoming | ||
| 81 | - const evts = feed.events().items; | ||
| 82 | - for (const e of evts) { | ||
| 83 | - if (!e.starts_at) continue; | ||
| 84 | - const t = new Date(e.starts_at).getTime(); | ||
| 85 | - if (notEnded(t, e.ends_at ? new Date(e.ends_at).getTime() : null)) { | ||
| 86 | - list.push({ | ||
| 87 | - id: `evt_${e.id}`, | ||
| 88 | - type: "event", | ||
| 89 | - title: e.title, | ||
| 90 | - description: e.starts_at | ||
| 91 | - ? new Date(e.starts_at).toLocaleString(dateLocale(), { | ||
| 92 | - month: "short", | ||
| 93 | - day: "numeric", | ||
| 94 | - hour: "2-digit", | ||
| 95 | - minute: "2-digit", | ||
| 96 | - }) | ||
| 97 | - : "", | ||
| 98 | - targetUrl: `/events/${e.id}`, | ||
| 99 | - timestamp: t, | ||
| 100 | - }); | ||
| 101 | - } | ||
| 102 | - } | ||
| 103 | - | ||
| 104 | - // 3. Exams starting today or upcoming | ||
| 105 | - const exms = feed.exams().items; | ||
| 106 | - for (const ex of exms) { | ||
| 107 | - if (!ex.starts_at || ex.draft) continue; | ||
| 108 | - const t = new Date(ex.starts_at).getTime(); | ||
| 109 | - if (notEnded(t, ex.ends_at ? new Date(ex.ends_at).getTime() : null)) { | ||
| 110 | - list.push({ | ||
| 111 | - id: `ex_${ex.id}`, | ||
| 112 | - type: "exam", | ||
| 113 | - title: ex.title, | ||
| 114 | - description: ex.starts_at | ||
| 115 | - ? new Date(ex.starts_at).toLocaleString(dateLocale(), { | ||
| 116 | - month: "short", | ||
| 117 | - day: "numeric", | ||
| 118 | - hour: "2-digit", | ||
| 119 | - minute: "2-digit", | ||
| 120 | - }) | ||
| 121 | - : "", | ||
| 122 | - targetUrl: `/exams/${ex.id}`, | ||
| 123 | - timestamp: t, | ||
| 124 | - }); | ||
| 125 | - } | ||
| 126 | - } | ||
| 127 | - | ||
| 128 | - // 4. Homework due upcoming | ||
| 129 | - const hws = feed.homework().items; | ||
| 130 | - for (const hw of hws) { | ||
| 131 | - if (!hw.due_at) continue; | ||
| 132 | - if (hw.due_at >= nowMs()) { | ||
| 133 | - list.push({ | ||
| 134 | - id: `hw_${hw.id}`, | ||
| 135 | - type: "homework", | ||
| 136 | - title: hw.title, | ||
| 137 | - description: hw.due_at | ||
| 138 | - ? `${t("homework.dueAt")}: ${new Date(hw.due_at).toLocaleDateString(dateLocale(), { | ||
| 139 | - month: "short", | ||
| 140 | - day: "numeric", | ||
| 141 | - })}` | ||
| 142 | - : "", | ||
| 143 | - targetUrl: `/homework/${hw.id}`, | ||
| 144 | - timestamp: hw.due_at, | ||
| 145 | - }); | ||
| 146 | - } | ||
| 147 | - } | ||
| 148 | - | ||
| 149 | - // Sort chronologically (newest/most relevant first) | ||
| 150 | - return list.sort((a, b) => b.timestamp - a.timestamp); | ||
| 151 | - }); | ||
| 152 | - | ||
| 153 | - // Filter out dismissed notifications | ||
| 154 | - const activeNotifications = createMemo(() => { | ||
| 155 | - const dismissed = dismissedIds(); | ||
| 156 | - return allNotifications().filter((item) => !dismissed.has(item.id)); | ||
| 157 | - }); | ||
| 158 | - | ||
| 159 | - const isUnread = (item: NotificationItem) => !readIds().has(item.id); | ||
| 160 | - const unreadNotifications = createMemo(() => activeNotifications().filter(isUnread)); | ||
| 161 | - | ||
| 162 | - const notificationGroupOrder: NotificationType[] = ["message", "event", "exam", "homework"]; | ||
| 163 | - const notificationGroupLabel = (type: NotificationType) => { | ||
| 164 | - if (type === "message") return t("nav.messages"); | ||
| 165 | - if (type === "event") return t("events.title"); | ||
| 166 | - if (type === "exam") return t("exams.title"); | ||
| 167 | - return t("homework.title"); | ||
| 168 | - }; | ||
| 169 | - const notificationGroupTone = (type: NotificationType) => { | ||
| 170 | - if (type === "message") return "border-primary/20 bg-primary/[0.035]"; | ||
| 171 | - if (type === "event") return "border-violet-500/20 bg-violet-500/[0.035]"; | ||
| 172 | - if (type === "exam") return "border-amber-500/25 bg-amber-500/[0.045]"; | ||
| 173 | - return "border-emerald-500/20 bg-emerald-500/[0.035]"; | ||
| 174 | - }; | ||
| 175 | - const notificationGroupIconTone = (type: NotificationType) => { | ||
| 176 | - if (type === "message") return "bg-primary/10 text-primary-text"; | ||
| 177 | - if (type === "event") return "bg-violet-500/10 text-violet-700 dark:text-violet-300"; | ||
| 178 | - if (type === "exam") return "bg-amber-500/10 text-amber-700 dark:text-amber-300"; | ||
| 179 | - return "bg-emerald-500/10 text-emerald-700 dark:text-emerald-300"; | ||
| 180 | - }; | ||
| 181 | - const notificationGroups = createMemo(() => | ||
| 182 | - notificationGroupOrder | ||
| 183 | - .map((type) => ({ | ||
| 184 | - type, | ||
| 185 | - items: activeNotifications().filter((item) => item.type === type), | ||
| 186 | - })) | ||
| 187 | - .filter((group) => group.items.length > 0) | ||
| 188 | - ); | ||
| 189 | - | ||
| 190 | - // The list only holds the newest 10 unread messages; `total` is the real | ||
| 191 | - // server-side unread count, so the rest still counts towards the badge. | ||
| 192 | - // Those extra rows have no id here, so they cannot be dismissed one by one: | ||
| 193 | - // "dismiss all" snapshots the unread total instead. Everything at or below the | ||
| 194 | - // snapshot counts as dismissed; anything above it arrived afterwards and still | ||
| 195 | - // raises the badge (minus the new rows already listed, which the active list | ||
| 196 | - // counts). Without the snapshot, 11 unread → dismiss all → badge stuck at 1 | ||
| 197 | - // over an empty popover. | ||
| 198 | - // ponytail: the snapshot is in-memory while dismissed ids are persisted, so a | ||
| 199 | - // reload re-shows unlisted unread — upgrade path is storing it next to the ids | ||
| 200 | - // in `lib/notifications`. | ||
| 201 | - const [dismissedUnreadTotal, setDismissedUnreadTotal] = createSignal(0); | ||
| 202 | - const activeMessageCount = () => activeNotifications().filter((item) => item.type === "message").length; | ||
| 203 | - const unlistedUnread = () => { | ||
| 204 | - const page = feed.unreadMessages(); | ||
| 205 | - const beyondPage = page.total - page.items.length; | ||
| 206 | - const sinceDismissAll = page.total - dismissedUnreadTotal() - activeMessageCount(); | ||
| 207 | - return Math.max(0, Math.min(beyondPage, sinceDismissAll)); | ||
| 208 | - }; | ||
| 209 | - const unreadCount = () => unreadNotifications().length + unlistedUnread(); | ||
| 210 | - | ||
| 211 | - const handleDismissSingle = (evt: MouseEvent, id: string) => { | ||
| 212 | - evt.stopPropagation(); | ||
| 213 | - const updated = dismissNotificationId(id); | ||
| 214 | - setDismissedIds(new Set(updated)); | ||
| 215 | - }; | ||
| 216 | - | ||
| 217 | - const handleDismissAll = () => { | ||
| 218 | - const ids = activeNotifications().map((item) => item.id); | ||
| 219 | - const updated = dismissAllNotificationIds(ids); | ||
| 220 | - setDismissedIds(new Set(updated)); | ||
| 221 | - setDismissedUnreadTotal(feed.unreadMessages().total); | ||
| 222 | - }; | ||
| 223 | - | ||
| 224 | - const handleSelectNotification = async (item: NotificationItem) => { | ||
| 225 | - if (isUnread(item)) { | ||
| 226 | - setReadIds(new Set(markNotificationRead(item.id))); | ||
| 227 | - } | ||
| 228 | - | ||
| 229 | - // Message notifications also need to be marked read in the backend. | ||
| 230 | - if (item.id.startsWith("msg_")) { | ||
| 231 | - const realId = item.id.replace("msg_", ""); | ||
| 232 | - try { | ||
| 233 | - await patchMessageById(realId, { read: true }); | ||
| 234 | - void refetchMessages(); | ||
| 235 | - } catch { | ||
| 236 | - // ignore | ||
| 237 | - } | ||
| 238 | - } | ||
| 239 | - setPreviewNotification(item); | ||
| 240 | - }; | ||
| 241 | - | ||
| 242 | - const handleNavigateToNotification = () => { | ||
| 243 | - const item = previewNotification(); | ||
| 244 | - if (!item) return; | ||
| 245 | - setPreviewNotification(null); | ||
| 246 | - setOpen(false); | ||
| 247 | - void navigate({ to: item.targetUrl }); | ||
| 248 | - }; | ||
| 249 | 22 | ||
| 250 | return ( | 23 | return ( |
| 251 | <Popover | 24 | <Popover |
| 252 | open={open()} | 25 | open={open()} |
| 253 | - onOpenChange={(nextOpen) => { | 26 | + onOpenChange={setOpen} |
| 254 | - setOpen(nextOpen); | ||
| 255 | - if (!nextOpen) setPreviewNotification(null); | ||
| 256 | - }} | ||
| 257 | placement="bottom-end" | 27 | placement="bottom-end" |
| 258 | gutter={8} | 28 | gutter={8} |
| 259 | > | 29 | > |
| @@ -274,194 +44,13 @@ export function NotificationCenter() { | |||
| 274 | </PopoverTrigger> | 44 | </PopoverTrigger> |
| 275 | 45 | ||
| 276 | <PopoverContent class="w-80 sm:w-96 rounded-xl border border-border-line bg-surface-base p-0 shadow-2xl overflow-hidden"> | 46 | <PopoverContent class="w-80 sm:w-96 rounded-xl border border-border-line bg-surface-base p-0 shadow-2xl overflow-hidden"> |
| 277 | - <div class="flex items-center justify-between border-b border-border-hairline px-4 py-3"> | 47 | + <NotificationList |
| 278 | - <div class="flex items-center gap-2"> | 48 | + feed={feed} |
| 279 | - <h3 class="text-sm font-semibold text-text-strong">{t("notifications.title")}</h3> | 49 | + onNavigate={(url) => { |
| 280 | - <Show when={unreadCount() > 0}> | 50 | + setOpen(false); |
| 281 | - <span class="rounded-full bg-primary/15 px-2 py-0.5 font-mono text-[11px] font-bold text-primary-text"> | 51 | + void navigate({ to: url }); |
| 282 | - {unreadCount()} | 52 | + }} |
| 283 | - </span> | 53 | + /> |
| 284 | - </Show> | ||
| 285 | - </div> | ||
| 286 | - | ||
| 287 | - <Show when={unreadCount() > 0}> | ||
| 288 | - <Button | ||
| 289 | - type="button" | ||
| 290 | - variant="ghost" | ||
| 291 | - size="sm" | ||
| 292 | - class="h-7 rounded-lg px-2 text-[11px] font-semibold text-text-subtle hover:bg-destructive/10 hover:text-destructive-text" | ||
| 293 | - onClick={handleDismissAll} | ||
| 294 | - title={t("notifications.clearAll")} | ||
| 295 | - > | ||
| 296 | - <IconTrash class="mr-1 h-3 w-3" /> | ||
| 297 | - {t("notifications.clearAll")} | ||
| 298 | - </Button> | ||
| 299 | - </Show> | ||
| 300 | - </div> | ||
| 301 | - | ||
| 302 | - <Show | ||
| 303 | - when={previewNotification()} | ||
| 304 | - fallback={ | ||
| 305 | - <Tabs defaultValue="all" class="p-2"> | ||
| 306 | - <TabsList class="mx-auto mb-2 h-8 w-fit min-w-[180px] p-0.5"> | ||
| 307 | - <TabsTrigger value="all" class="h-7 flex-1 px-3 text-[11px]">{t("notifications.tabAll")}</TabsTrigger> | ||
| 308 | - <TabsTrigger value="system" class="h-7 flex-1 px-3 text-[11px]">{t("notifications.tabSystem")}</TabsTrigger> | ||
| 309 | - </TabsList> | ||
| 310 | - | ||
| 311 | - <TabsContent value="all" class="mt-0 max-h-80 space-y-1 overflow-y-auto"> | ||
| 312 | - <Suspense | ||
| 313 | - fallback={ | ||
| 314 | - <div class="p-8 text-center text-xs text-text-subtle"> | ||
| 315 | - {t("common.loading")} | ||
| 316 | - </div> | ||
| 317 | - } | ||
| 318 | - > | ||
| 319 | - <Show | ||
| 320 | - when={notificationGroups().length > 0} | ||
| 321 | - fallback={ | ||
| 322 | - <div class="space-y-2 p-8 text-center"> | ||
| 323 | - <div class="mx-auto flex h-10 w-10 items-center justify-center rounded-full bg-surface-tint text-text-subtle"> | ||
| 324 | - <IconBell class="h-5 w-5" /> | ||
| 325 | - </div> | ||
| 326 | - <p class="text-xs font-medium text-text-subtle"> | ||
| 327 | - {t("notifications.empty")} | ||
| 328 | - </p> | ||
| 329 | - </div> | ||
| 330 | - } | ||
| 331 | - > | ||
| 332 | - <div class="space-y-2"> | ||
| 333 | - <For each={notificationGroups()}> | ||
| 334 | - {(group) => ( | ||
| 335 | - <section class={cn("overflow-hidden rounded-xl border", notificationGroupTone(group.type))}> | ||
| 336 | - <div class="flex items-center justify-between gap-2 px-2.5 py-2"> | ||
| 337 | - <div class="flex min-w-0 items-center gap-2"> | ||
| 338 | - <div class={cn("flex h-6 w-6 shrink-0 items-center justify-center rounded-md", notificationGroupIconTone(group.type))}> | ||
| 339 | - <Show when={group.type === "message"}> | ||
| 340 | - <IconMessage class="h-3.5 w-3.5" /> | ||
| 341 | - </Show> | ||
| 342 | - <Show when={group.type === "event"}> | ||
| 343 | - <IconCalendar class="h-3.5 w-3.5" /> | ||
| 344 | - </Show> | ||
| 345 | - <Show when={group.type === "exam"}> | ||
| 346 | - <IconExam class="h-3.5 w-3.5" /> | ||
| 347 | - </Show> | ||
| 348 | - <Show when={group.type === "homework"}> | ||
| 349 | - <IconHomework class="h-3.5 w-3.5" /> | ||
| 350 | - </Show> | ||
| 351 | - </div> | ||
| 352 | - <span class="truncate text-[11px] font-bold uppercase tracking-[0.08em] text-text-subtle"> | ||
| 353 | - {notificationGroupLabel(group.type)} | ||
| 354 | - </span> | ||
| 355 | - </div> | ||
| 356 | - <span class="shrink-0 rounded-full bg-background/70 px-1.5 py-0.5 font-mono text-[11px] font-bold text-text-subtle"> | ||
| 357 | - {group.items.length} | ||
| 358 | - </span> | ||
| 359 | - </div> | ||
| 360 | - | ||
| 361 | - <div class="space-y-0.5 px-1 pb-1"> | ||
| 362 | - <For each={group.items}> | ||
| 363 | - {(item) => ( | ||
| 364 | - <div | ||
| 365 | - onClick={() => handleSelectNotification(item)} | ||
| 366 | - class="group relative flex cursor-pointer select-none items-start gap-3 rounded-lg p-2 text-xs transition-colors hover:bg-background/70" | ||
| 367 | - > | ||
| 368 | - <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"> | ||
| 369 | - <Show when={item.type === "message"}> | ||
| 370 | - <IconMessage class="h-4 w-4" /> | ||
| 371 | - </Show> | ||
| 372 | - <Show when={item.type === "event"}> | ||
| 373 | - <IconCalendar class="h-4 w-4" /> | ||
| 374 | - </Show> | ||
| 375 | - <Show when={item.type === "exam"}> | ||
| 376 | - <IconExam class="h-4 w-4" /> | ||
| 377 | - </Show> | ||
| 378 | - <Show when={item.type === "homework"}> | ||
| 379 | - <IconHomework class="h-4 w-4" /> | ||
| 380 | - </Show> | ||
| 381 | - </div> | ||
| 382 | - | ||
| 383 | - <div class="min-w-0 flex-1 space-y-0.5 pr-6"> | ||
| 384 | - <span class="block truncate font-semibold text-text-strong"> | ||
| 385 | - {item.title} | ||
| 386 | - </span> | ||
| 387 | - <p class="line-clamp-2 text-[11px] leading-snug text-text-subtle"> | ||
| 388 | - {item.description} | ||
| 389 | - </p> | ||
| 390 | - </div> | ||
| 391 | - | ||
| 392 | - <button | ||
| 393 | - type="button" | ||
| 394 | - 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 sm:opacity-0 sm:group-hover:opacity-100" | ||
| 395 | - title={t("notifications.dismiss")} | ||
| 396 | - onClick={(e) => handleDismissSingle(e, item.id)} | ||
| 397 | - > | ||
| 398 | - <IconX class="h-3.5 w-3.5" /> | ||
| 399 | - </button> | ||
| 400 | - </div> | ||
| 401 | - )} | ||
| 402 | - </For> | ||
| 403 | - </div> | ||
| 404 | - </section> | ||
| 405 | - )} | ||
| 406 | - </For> | ||
| 407 | - </div> | ||
| 408 | - </Show> | ||
| 409 | - </Suspense> | ||
| 410 | - </TabsContent> | ||
| 411 | - | ||
| 412 | - <TabsContent value="system" class="mt-0"> | ||
| 413 | - <ComingSoonPanel title={t("notifications.tabSystem")} class="border-0 p-3" /> | ||
| 414 | - </TabsContent> | ||
| 415 | - </Tabs> | ||
| 416 | - } | ||
| 417 | - > | ||
| 418 | - {(item) => ( | ||
| 419 | - <div class="space-y-4 p-4"> | ||
| 420 | - <div class="flex items-center gap-2"> | ||
| 421 | - <Button | ||
| 422 | - type="button" | ||
| 423 | - variant="ghost" | ||
| 424 | - size="sm" | ||
| 425 | - class="h-7 rounded-lg px-2 text-[11px] text-text-subtle" | ||
| 426 | - onClick={() => setPreviewNotification(null)} | ||
| 427 | - > | ||
| 428 | - <IconChevronLeft class="mr-1 h-3.5 w-3.5" /> | ||
| 429 | - {t("common.back")} | ||
| 430 | - </Button> | ||
| 431 | - </div> | ||
| 432 | - | ||
| 433 | - <div class="space-y-2"> | ||
| 434 | - <div class="flex items-start gap-3"> | ||
| 435 | - <div class={cn("flex h-10 w-10 shrink-0 items-center justify-center rounded-xl", notificationGroupIconTone(item().type))}> | ||
| 436 | - <Show when={item().type === "message"}> | ||
| 437 | - <IconMessage class="h-5 w-5" /> | ||
| 438 | - </Show> | ||
| 439 | - <Show when={item().type === "event"}> | ||
| 440 | - <IconCalendar class="h-5 w-5" /> | ||
| 441 | - </Show> | ||
| 442 | - <Show when={item().type === "exam"}> | ||
| 443 | - <IconExam class="h-5 w-5" /> | ||
| 444 | - </Show> | ||
| 445 | - <Show when={item().type === "homework"}> | ||
| 446 | - <IconHomework class="h-5 w-5" /> | ||
| 447 | - </Show> | ||
| 448 | - </div> | ||
| 449 | - <div class="min-w-0 space-y-1"> | ||
| 450 | - <h4 class="text-sm font-semibold leading-snug text-text-strong">{item().title}</h4> | ||
| 451 | - <p class="whitespace-pre-line text-xs leading-relaxed text-text-subtle"> | ||
| 452 | - {item().preview ?? item().description} | ||
| 453 | - </p> | ||
| 454 | - </div> | ||
| 455 | - </div> | ||
| 456 | - </div> | ||
| 457 | - | ||
| 458 | - <Button type="button" size="sm" class="w-full rounded-lg" onClick={handleNavigateToNotification}> | ||
| 459 | - <IconExternalLink class="mr-1.5 h-3.5 w-3.5" /> | ||
| 460 | - {t("notifications.goToPage")} | ||
| 461 | - </Button> | ||
| 462 | - </div> | ||
| 463 | - )} | ||
| 464 | - </Show> | ||
| 465 | </PopoverContent> | 54 | </PopoverContent> |
| 466 | </Popover> | 55 | </Popover> |
| 467 | ); | 56 | ); |
src/components/layout/notification-feed.tsadded+211 -0
| @@ -0,0 +1,211 @@ | |||
| 1 | +import { createMemo, createSignal } from "solid-js"; | ||
| 2 | +import { patchMessageById } from "@/api/messages"; | ||
| 3 | +import { personLabel } from "@/lib/person"; | ||
| 4 | +import { | ||
| 5 | + dismissAllNotificationIds, | ||
| 6 | + dismissNotificationId, | ||
| 7 | + getDismissedNotificationIds, | ||
| 8 | + getReadNotificationIds, | ||
| 9 | + markNotificationRead, | ||
| 10 | + type NotificationItem, | ||
| 11 | + type NotificationType, | ||
| 12 | +} from "@/lib/notifications"; | ||
| 13 | +import { usePreferences } from "@/stores/preferences-context"; | ||
| 14 | +import { useShellFeed } from "@/stores/shell-feed-context"; | ||
| 15 | + | ||
| 16 | +/** | ||
| 17 | + * The notification list and its read/dismissed state, apart from any chrome. | ||
| 18 | + * The desktop header shows it in a popover and the phone shell in a panel | ||
| 19 | + * opened from the floating action button; each mounts one of these, and the | ||
| 20 | + * shell mounts only one of the two at a time, so the dismissed state never | ||
| 21 | + * has two in-memory copies. | ||
| 22 | + */ | ||
| 23 | +export function createNotificationFeed() { | ||
| 24 | + const { t, locale } = usePreferences(); | ||
| 25 | + const dateLocale = () => (locale() === "tr" ? "tr-TR" : "en-US"); | ||
| 26 | + const feed = useShellFeed(); | ||
| 27 | + const [dismissedIds, setDismissedIds] = createSignal<Set<string>>(getDismissedNotificationIds()); | ||
| 28 | + const [readIds, setReadIds] = createSignal<Set<string>>(getReadNotificationIds()); | ||
| 29 | + | ||
| 30 | + // Shell feed data comes from the shared poller (ShellFeedProvider): one fetch | ||
| 31 | + // per source for the whole shell instead of a duplicate set here. Every read | ||
| 32 | + // below is `feed.x()` which returns `resource.latest` — non-suspending on the | ||
| 33 | + // 60s refetch, so the unread badge (outside the popover's <Suspense>) never | ||
| 34 | + // blanks the page beside <Outlet> (AGENTS.md #6). | ||
| 35 | + const nowMs = feed.nowMs; | ||
| 36 | + | ||
| 37 | + // An item is still notifiable until it has ended (ends_at, else starts_at). | ||
| 38 | + const notEnded = (starts: number, ends?: number | null) => (ends ?? starts) >= nowMs(); | ||
| 39 | + | ||
| 40 | + // Combine notification items | ||
| 41 | + const allNotifications = createMemo<NotificationItem[]>(() => { | ||
| 42 | + const list: NotificationItem[] = []; | ||
| 43 | + | ||
| 44 | + // 1. Unread messages — the dedicated `read=false` page from the shell feed. | ||
| 45 | + // Never filter the shared inbox page instead: it is capped and ordered by | ||
| 46 | + // id DESC, so an older unread message is simply not in it. | ||
| 47 | + const msgs = feed.unreadMessages().items; | ||
| 48 | + for (const m of msgs) { | ||
| 49 | + const body = m.body.replace(/<[^>]*>?/gm, "").trim(); | ||
| 50 | + list.push({ | ||
| 51 | + id: `msg_${m.id}`, | ||
| 52 | + type: "message", | ||
| 53 | + title: personLabel(m.sender), | ||
| 54 | + description: m.subject || body, | ||
| 55 | + preview: m.subject ? `${m.subject}\n${body}` : body, | ||
| 56 | + targetUrl: "/messages", | ||
| 57 | + timestamp: m.sent_at, | ||
| 58 | + }); | ||
| 59 | + } | ||
| 60 | + | ||
| 61 | + // 2. Events starting today or upcoming | ||
| 62 | + const evts = feed.events().items; | ||
| 63 | + for (const e of evts) { | ||
| 64 | + if (!e.starts_at) continue; | ||
| 65 | + const t = new Date(e.starts_at).getTime(); | ||
| 66 | + if (notEnded(t, e.ends_at ? new Date(e.ends_at).getTime() : null)) { | ||
| 67 | + list.push({ | ||
| 68 | + id: `evt_${e.id}`, | ||
| 69 | + type: "event", | ||
| 70 | + title: e.title, | ||
| 71 | + description: e.starts_at | ||
| 72 | + ? new Date(e.starts_at).toLocaleString(dateLocale(), { | ||
| 73 | + month: "short", | ||
| 74 | + day: "numeric", | ||
| 75 | + hour: "2-digit", | ||
| 76 | + minute: "2-digit", | ||
| 77 | + }) | ||
| 78 | + : "", | ||
| 79 | + targetUrl: `/events/${e.id}`, | ||
| 80 | + timestamp: t, | ||
| 81 | + }); | ||
| 82 | + } | ||
| 83 | + } | ||
| 84 | + | ||
| 85 | + // 3. Exams starting today or upcoming | ||
| 86 | + const exms = feed.exams().items; | ||
| 87 | + for (const ex of exms) { | ||
| 88 | + if (!ex.starts_at || ex.draft) continue; | ||
| 89 | + const t = new Date(ex.starts_at).getTime(); | ||
| 90 | + if (notEnded(t, ex.ends_at ? new Date(ex.ends_at).getTime() : null)) { | ||
| 91 | + list.push({ | ||
| 92 | + id: `ex_${ex.id}`, | ||
| 93 | + type: "exam", | ||
| 94 | + title: ex.title, | ||
| 95 | + description: ex.starts_at | ||
| 96 | + ? new Date(ex.starts_at).toLocaleString(dateLocale(), { | ||
| 97 | + month: "short", | ||
| 98 | + day: "numeric", | ||
| 99 | + hour: "2-digit", | ||
| 100 | + minute: "2-digit", | ||
| 101 | + }) | ||
| 102 | + : "", | ||
| 103 | + targetUrl: `/exams/${ex.id}`, | ||
| 104 | + timestamp: t, | ||
| 105 | + }); | ||
| 106 | + } | ||
| 107 | + } | ||
| 108 | + | ||
| 109 | + // 4. Homework due upcoming | ||
| 110 | + const hws = feed.homework().items; | ||
| 111 | + for (const hw of hws) { | ||
| 112 | + if (!hw.due_at) continue; | ||
| 113 | + if (hw.due_at >= nowMs()) { | ||
| 114 | + list.push({ | ||
| 115 | + id: `hw_${hw.id}`, | ||
| 116 | + type: "homework", | ||
| 117 | + title: hw.title, | ||
| 118 | + description: hw.due_at | ||
| 119 | + ? `${t("homework.dueAt")}: ${new Date(hw.due_at).toLocaleDateString(dateLocale(), { | ||
| 120 | + month: "short", | ||
| 121 | + day: "numeric", | ||
| 122 | + })}` | ||
| 123 | + : "", | ||
| 124 | + targetUrl: `/homework/${hw.id}`, | ||
| 125 | + timestamp: hw.due_at, | ||
| 126 | + }); | ||
| 127 | + } | ||
| 128 | + } | ||
| 129 | + | ||
| 130 | + // Sort chronologically (newest/most relevant first) | ||
| 131 | + return list.sort((a, b) => b.timestamp - a.timestamp); | ||
| 132 | + }); | ||
| 133 | + | ||
| 134 | + // Filter out dismissed notifications | ||
| 135 | + const activeNotifications = createMemo(() => { | ||
| 136 | + const dismissed = dismissedIds(); | ||
| 137 | + return allNotifications().filter((item) => !dismissed.has(item.id)); | ||
| 138 | + }); | ||
| 139 | + | ||
| 140 | + const isUnread = (item: NotificationItem) => !readIds().has(item.id); | ||
| 141 | + const unreadNotifications = createMemo(() => activeNotifications().filter(isUnread)); | ||
| 142 | + | ||
| 143 | + const notificationGroupOrder: NotificationType[] = ["message", "event", "exam", "homework"]; | ||
| 144 | + const notificationGroups = createMemo(() => | ||
| 145 | + notificationGroupOrder | ||
| 146 | + .map((type) => ({ | ||
| 147 | + type, | ||
| 148 | + items: activeNotifications().filter((item) => item.type === type), | ||
| 149 | + })) | ||
| 150 | + .filter((group) => group.items.length > 0) | ||
| 151 | + ); | ||
| 152 | + | ||
| 153 | + // The list only holds the newest 10 unread messages; `total` is the real | ||
| 154 | + // server-side unread count, so the rest still counts towards the badge. | ||
| 155 | + // Those extra rows have no id here, so they cannot be dismissed one by one: | ||
| 156 | + // "dismiss all" snapshots the unread total instead. Everything at or below the | ||
| 157 | + // snapshot counts as dismissed; anything above it arrived afterwards and still | ||
| 158 | + // raises the badge (minus the new rows already listed, which the active list | ||
| 159 | + // counts). Without the snapshot, 11 unread → dismiss all → badge stuck at 1 | ||
| 160 | + // over an empty popover. | ||
| 161 | + // ponytail: the snapshot is in-memory while dismissed ids are persisted, so a | ||
| 162 | + // reload re-shows unlisted unread — upgrade path is storing it next to the ids | ||
| 163 | + // in `lib/notifications`. | ||
| 164 | + const [dismissedUnreadTotal, setDismissedUnreadTotal] = createSignal(0); | ||
| 165 | + const activeMessageCount = () => activeNotifications().filter((item) => item.type === "message").length; | ||
| 166 | + const unlistedUnread = () => { | ||
| 167 | + const page = feed.unreadMessages(); | ||
| 168 | + const beyondPage = page.total - page.items.length; | ||
| 169 | + const sinceDismissAll = page.total - dismissedUnreadTotal() - activeMessageCount(); | ||
| 170 | + return Math.max(0, Math.min(beyondPage, sinceDismissAll)); | ||
| 171 | + }; | ||
| 172 | + const unreadCount = () => unreadNotifications().length + unlistedUnread(); | ||
| 173 | + | ||
| 174 | + const dismiss = (id: string) => { | ||
| 175 | + setDismissedIds(new Set(dismissNotificationId(id))); | ||
| 176 | + }; | ||
| 177 | + | ||
| 178 | + const dismissAll = () => { | ||
| 179 | + const ids = activeNotifications().map((item) => item.id); | ||
| 180 | + setDismissedIds(new Set(dismissAllNotificationIds(ids))); | ||
| 181 | + setDismissedUnreadTotal(feed.unreadMessages().total); | ||
| 182 | + }; | ||
| 183 | + | ||
| 184 | + const markRead = async (item: NotificationItem) => { | ||
| 185 | + if (isUnread(item)) { | ||
| 186 | + setReadIds(new Set(markNotificationRead(item.id))); | ||
| 187 | + } | ||
| 188 | + | ||
| 189 | + // Message notifications also need to be marked read in the backend. | ||
| 190 | + if (item.id.startsWith("msg_")) { | ||
| 191 | + const realId = item.id.replace("msg_", ""); | ||
| 192 | + try { | ||
| 193 | + await patchMessageById(realId, { read: true }); | ||
| 194 | + void feed.refetchMessages(); | ||
| 195 | + } catch { | ||
| 196 | + // ignore | ||
| 197 | + } | ||
| 198 | + } | ||
| 199 | + }; | ||
| 200 | + | ||
| 201 | + return { | ||
| 202 | + groups: notificationGroups, | ||
| 203 | + unreadCount, | ||
| 204 | + dismiss, | ||
| 205 | + dismissAll, | ||
| 206 | + markRead, | ||
| 207 | + refreshAll: feed.refreshAll, | ||
| 208 | + }; | ||
| 209 | +} | ||
| 210 | + | ||
| 211 | +export type NotificationFeed = ReturnType<typeof createNotificationFeed>; | ||
src/components/layout/notification-list.tsxadded+272 -0
| @@ -0,0 +1,272 @@ | |||
| 1 | +import { For, Show, Suspense, createSignal } from "solid-js"; | ||
| 2 | +import { Button } from "@/components/ui/button"; | ||
| 3 | +import { ComingSoonPanel } from "@/components/ui/coming-soon"; | ||
| 4 | +import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"; | ||
| 5 | +import { | ||
| 6 | + IconBell, | ||
| 7 | + IconCalendar, | ||
| 8 | + IconChevronLeft, | ||
| 9 | + IconExam, | ||
| 10 | + IconExternalLink, | ||
| 11 | + IconHomework, | ||
| 12 | + IconMessage, | ||
| 13 | + IconTrash, | ||
| 14 | + IconX, | ||
| 15 | +} from "@/components/ui/icons"; | ||
| 16 | +import { cn } from "@/lib/cn"; | ||
| 17 | +import type { NotificationItem, NotificationType } from "@/lib/notifications"; | ||
| 18 | +import type { NotificationFeed } from "@/components/layout/notification-feed"; | ||
| 19 | +import { useT } from "@/stores/preferences-context"; | ||
| 20 | + | ||
| 21 | +/** | ||
| 22 | + * The notification list — header with "clear all", grouped rows, and the | ||
| 23 | + * preview of one row — for whichever surface hosts it. The preview is local | ||
| 24 | + * state, so it resets whenever the host unmounts the list on close. | ||
| 25 | + */ | ||
| 26 | +export function NotificationList(props: { | ||
| 27 | + feed: NotificationFeed; | ||
| 28 | + /** Leaves the target page of a previewed notification. */ | ||
| 29 | + onNavigate: (url: string) => void; | ||
| 30 | + /** The host already names the list (a panel title). */ | ||
| 31 | + showTitle?: boolean; | ||
| 32 | + /** Height cap of the scrolling list; the popover keeps it short. */ | ||
| 33 | + scrollClass?: string; | ||
| 34 | +}) { | ||
| 35 | + const t = useT(); | ||
| 36 | + const [previewNotification, setPreviewNotification] = createSignal<NotificationItem | null>(null); | ||
| 37 | + | ||
| 38 | + const notificationGroupLabel = (type: NotificationType) => { | ||
| 39 | + if (type === "message") return t("nav.messages"); | ||
| 40 | + if (type === "event") return t("events.title"); | ||
| 41 | + if (type === "exam") return t("exams.title"); | ||
| 42 | + return t("homework.title"); | ||
| 43 | + }; | ||
| 44 | + const notificationGroupTone = (type: NotificationType) => { | ||
| 45 | + if (type === "message") return "border-primary/20 bg-primary/[0.035]"; | ||
| 46 | + if (type === "event") return "border-violet-500/20 bg-violet-500/[0.035]"; | ||
| 47 | + if (type === "exam") return "border-amber-500/25 bg-amber-500/[0.045]"; | ||
| 48 | + return "border-emerald-500/20 bg-emerald-500/[0.035]"; | ||
| 49 | + }; | ||
| 50 | + const notificationGroupIconTone = (type: NotificationType) => { | ||
| 51 | + if (type === "message") return "bg-primary/10 text-primary-text"; | ||
| 52 | + if (type === "event") return "bg-violet-500/10 text-violet-700 dark:text-violet-300"; | ||
| 53 | + if (type === "exam") return "bg-amber-500/10 text-amber-700 dark:text-amber-300"; | ||
| 54 | + return "bg-emerald-500/10 text-emerald-700 dark:text-emerald-300"; | ||
| 55 | + }; | ||
| 56 | + | ||
| 57 | + const dismissOne = (evt: MouseEvent, id: string) => { | ||
| 58 | + evt.stopPropagation(); | ||
| 59 | + props.feed.dismiss(id); | ||
| 60 | + }; | ||
| 61 | + | ||
| 62 | + const select = async (item: NotificationItem) => { | ||
| 63 | + await props.feed.markRead(item); | ||
| 64 | + setPreviewNotification(item); | ||
| 65 | + }; | ||
| 66 | + | ||
| 67 | + const goTo = () => { | ||
| 68 | + const item = previewNotification(); | ||
| 69 | + if (!item) return; | ||
| 70 | + setPreviewNotification(null); | ||
| 71 | + props.onNavigate(item.targetUrl); | ||
| 72 | + }; | ||
| 73 | + | ||
| 74 | + return ( | ||
| 75 | + <> | ||
| 76 | + {/* Hosted under a panel title, the header row only earns its place when | ||
| 77 | + there is something to clear. */} | ||
| 78 | + <Show when={(props.showTitle ?? true) || props.feed.unreadCount() > 0}> | ||
| 79 | + <div class="flex items-center justify-between border-b border-border-hairline px-4 py-3"> | ||
| 80 | + <div class="flex items-center gap-2"> | ||
| 81 | + <Show when={props.showTitle ?? true}> | ||
| 82 | + <h3 class="text-sm font-semibold text-text-strong">{t("notifications.title")}</h3> | ||
| 83 | + </Show> | ||
| 84 | + <Show when={props.feed.unreadCount() > 0}> | ||
| 85 | + <span class="rounded-full bg-primary/15 px-2 py-0.5 font-mono text-[11px] font-bold text-primary-text"> | ||
| 86 | + {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={() => props.feed.dismissAll()} | ||
| 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 | + <Tabs defaultValue="all" class="p-2"> | ||
| 111 | + <TabsList class="mx-auto mb-2 h-8 w-fit min-w-[180px] p-0.5"> | ||
| 112 | + <TabsTrigger value="all" class="h-7 flex-1 px-3 text-[11px]">{t("notifications.tabAll")}</TabsTrigger> | ||
| 113 | + <TabsTrigger value="system" class="h-7 flex-1 px-3 text-[11px]">{t("notifications.tabSystem")}</TabsTrigger> | ||
| 114 | + </TabsList> | ||
| 115 | + | ||
| 116 | + <TabsContent value="all" class={cn("mt-0 space-y-1 overflow-y-auto", props.scrollClass ?? "max-h-80")}> | ||
| 117 | + <Suspense | ||
| 118 | + fallback={ | ||
| 119 | + <div class="p-8 text-center text-xs text-text-subtle"> | ||
| 120 | + {t("common.loading")} | ||
| 121 | + </div> | ||
| 122 | + } | ||
| 123 | + > | ||
| 124 | + <Show | ||
| 125 | + when={props.feed.groups().length > 0} | ||
| 126 | + fallback={ | ||
| 127 | + <div class="space-y-2 p-8 text-center"> | ||
| 128 | + <div class="mx-auto flex h-10 w-10 items-center justify-center rounded-full bg-surface-tint text-text-subtle"> | ||
| 129 | + <IconBell class="h-5 w-5" /> | ||
| 130 | + </div> | ||
| 131 | + <p class="text-xs font-medium text-text-subtle"> | ||
| 132 | + {t("notifications.empty")} | ||
| 133 | + </p> | ||
| 134 | + </div> | ||
| 135 | + } | ||
| 136 | + > | ||
| 137 | + <div class="space-y-2"> | ||
| 138 | + <For each={props.feed.groups()}> | ||
| 139 | + {(group) => ( | ||
| 140 | + <section class={cn("overflow-hidden rounded-xl border", notificationGroupTone(group.type))}> | ||
| 141 | + <div class="flex items-center justify-between gap-2 px-2.5 py-2"> | ||
| 142 | + <div class="flex min-w-0 items-center gap-2"> | ||
| 143 | + <div class={cn("flex h-6 w-6 shrink-0 items-center justify-center rounded-md", notificationGroupIconTone(group.type))}> | ||
| 144 | + <Show when={group.type === "message"}> | ||
| 145 | + <IconMessage class="h-3.5 w-3.5" /> | ||
| 146 | + </Show> | ||
| 147 | + <Show when={group.type === "event"}> | ||
| 148 | + <IconCalendar class="h-3.5 w-3.5" /> | ||
| 149 | + </Show> | ||
| 150 | + <Show when={group.type === "exam"}> | ||
| 151 | + <IconExam class="h-3.5 w-3.5" /> | ||
| 152 | + </Show> | ||
| 153 | + <Show when={group.type === "homework"}> | ||
| 154 | + <IconHomework class="h-3.5 w-3.5" /> | ||
| 155 | + </Show> | ||
| 156 | + </div> | ||
| 157 | + <span class="truncate text-[11px] font-bold uppercase tracking-[0.08em] text-text-subtle"> | ||
| 158 | + {notificationGroupLabel(group.type)} | ||
| 159 | + </span> | ||
| 160 | + </div> | ||
| 161 | + <span class="shrink-0 rounded-full bg-background/70 px-1.5 py-0.5 font-mono text-[11px] font-bold text-text-subtle"> | ||
| 162 | + {group.items.length} | ||
| 163 | + </span> | ||
| 164 | + </div> | ||
| 165 | + | ||
| 166 | + <div class="space-y-0.5 px-1 pb-1"> | ||
| 167 | + <For each={group.items}> | ||
| 168 | + {(item) => ( | ||
| 169 | + <div | ||
| 170 | + onClick={() => select(item)} | ||
| 171 | + class="group relative flex cursor-pointer select-none items-start gap-3 rounded-lg p-2 text-xs transition-colors hover:bg-background/70" | ||
| 172 | + > | ||
| 173 | + <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"> | ||
| 174 | + <Show when={item.type === "message"}> | ||
| 175 | + <IconMessage class="h-4 w-4" /> | ||
| 176 | + </Show> | ||
| 177 | + <Show when={item.type === "event"}> | ||
| 178 | + <IconCalendar class="h-4 w-4" /> | ||
| 179 | + </Show> | ||
| 180 | + <Show when={item.type === "exam"}> | ||
| 181 | + <IconExam class="h-4 w-4" /> | ||
| 182 | + </Show> | ||
| 183 | + <Show when={item.type === "homework"}> | ||
| 184 | + <IconHomework class="h-4 w-4" /> | ||
| 185 | + </Show> | ||
| 186 | + </div> | ||
| 187 | + | ||
| 188 | + <div class="min-w-0 flex-1 space-y-0.5 pr-6"> | ||
| 189 | + <span class="block truncate font-semibold text-text-strong"> | ||
| 190 | + {item.title} | ||
| 191 | + </span> | ||
| 192 | + <p class="line-clamp-2 text-[11px] leading-snug text-text-subtle"> | ||
| 193 | + {item.description} | ||
| 194 | + </p> | ||
| 195 | + </div> | ||
| 196 | + | ||
| 197 | + <button | ||
| 198 | + type="button" | ||
| 199 | + 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 sm:opacity-0 sm:group-hover:opacity-100" | ||
| 200 | + title={t("notifications.dismiss")} | ||
| 201 | + onClick={(e) => dismissOne(e, item.id)} | ||
| 202 | + > | ||
| 203 | + <IconX class="h-3.5 w-3.5" /> | ||
| 204 | + </button> | ||
| 205 | + </div> | ||
| 206 | + )} | ||
| 207 | + </For> | ||
| 208 | + </div> | ||
| 209 | + </section> | ||
| 210 | + )} | ||
| 211 | + </For> | ||
| 212 | + </div> | ||
| 213 | + </Show> | ||
| 214 | + </Suspense> | ||
| 215 | + </TabsContent> | ||
| 216 | + | ||
| 217 | + <TabsContent value="system" class="mt-0"> | ||
| 218 | + <ComingSoonPanel title={t("notifications.tabSystem")} class="border-0 p-3" /> | ||
| 219 | + </TabsContent> | ||
| 220 | + </Tabs> | ||
| 221 | + } | ||
| 222 | + > | ||
| 223 | + {(item) => ( | ||
| 224 | + <div class="space-y-4 p-4"> | ||
| 225 | + <div class="flex items-center gap-2"> | ||
| 226 | + <Button | ||
| 227 | + type="button" | ||
| 228 | + variant="ghost" | ||
| 229 | + size="sm" | ||
| 230 | + class="h-7 rounded-lg px-2 text-[11px] text-text-subtle" | ||
| 231 | + onClick={() => setPreviewNotification(null)} | ||
| 232 | + > | ||
| 233 | + <IconChevronLeft class="mr-1 h-3.5 w-3.5" /> | ||
| 234 | + {t("common.back")} | ||
| 235 | + </Button> | ||
| 236 | + </div> | ||
| 237 | + | ||
| 238 | + <div class="space-y-2"> | ||
| 239 | + <div class="flex items-start gap-3"> | ||
| 240 | + <div class={cn("flex h-10 w-10 shrink-0 items-center justify-center rounded-xl", notificationGroupIconTone(item().type))}> | ||
| 241 | + <Show when={item().type === "message"}> | ||
| 242 | + <IconMessage class="h-5 w-5" /> | ||
| 243 | + </Show> | ||
| 244 | + <Show when={item().type === "event"}> | ||
| 245 | + <IconCalendar class="h-5 w-5" /> | ||
| 246 | + </Show> | ||
| 247 | + <Show when={item().type === "exam"}> | ||
| 248 | + <IconExam class="h-5 w-5" /> | ||
| 249 | + </Show> | ||
| 250 | + <Show when={item().type === "homework"}> | ||
| 251 | + <IconHomework class="h-5 w-5" /> | ||
| 252 | + </Show> | ||
| 253 | + </div> | ||
| 254 | + <div class="min-w-0 space-y-1"> | ||
| 255 | + <h4 class="text-sm font-semibold leading-snug text-text-strong">{item().title}</h4> | ||
| 256 | + <p class="whitespace-pre-line text-xs leading-relaxed text-text-subtle"> | ||
| 257 | + {item().preview ?? item().description} | ||
| 258 | + </p> | ||
| 259 | + </div> | ||
| 260 | + </div> | ||
| 261 | + </div> | ||
| 262 | + | ||
| 263 | + <Button type="button" size="sm" class="w-full rounded-lg" onClick={goTo}> | ||
| 264 | + <IconExternalLink class="mr-1.5 h-3.5 w-3.5" /> | ||
| 265 | + {t("notifications.goToPage")} | ||
| 266 | + </Button> | ||
| 267 | + </div> | ||
| 268 | + )} | ||
| 269 | + </Show> | ||
| 270 | + </> | ||
| 271 | + ); | ||
| 272 | +} | ||
src/i18n/locales/en.tsmodified+3 -0
| @@ -398,6 +398,9 @@ export const en: Dict = { | |||
| 398 | "nav.logout": "Log out", | 398 | "nav.logout": "Log out", |
| 399 | "nav.menu": "Menu", | 399 | "nav.menu": "Menu", |
| 400 | "nav.close": "Close", | 400 | "nav.close": "Close", |
| 401 | + "fab.label": "Quick actions", | ||
| 402 | + "fab.hide": "Hide", | ||
| 403 | + "fab.restore": "Show quick actions", | ||
| 401 | "nav.collapse": "Collapse sidebar", | 404 | "nav.collapse": "Collapse sidebar", |
| 402 | "nav.expand": "Expand sidebar", | 405 | "nav.expand": "Expand sidebar", |
| 403 | "nav.account": "Account", | 406 | "nav.account": "Account", |
src/i18n/locales/tr.tsmodified+3 -0
| @@ -394,6 +394,9 @@ export const tr: Dict = { | |||
| 394 | "nav.logout": "Çıkış yap", | 394 | "nav.logout": "Çıkış yap", |
| 395 | "nav.menu": "Menü", | 395 | "nav.menu": "Menü", |
| 396 | "nav.close": "Kapat", | 396 | "nav.close": "Kapat", |
| 397 | + "fab.label": "Hızlı işlemler", | ||
| 398 | + "fab.hide": "Gizle", | ||
| 399 | + "fab.restore": "Hızlı işlemleri göster", | ||
| 397 | "nav.collapse": "Kenar çubuğunu daralt", | 400 | "nav.collapse": "Kenar çubuğunu daralt", |
| 398 | "nav.expand": "Kenar çubuğunu genişlet", | 401 | "nav.expand": "Kenar çubuğunu genişlet", |
| 399 | "nav.account": "Hesap", | 402 | "nav.account": "Hesap", |
src/i18n/messages.tsmodified+3 -0
| @@ -363,6 +363,9 @@ export type MessageKey = | |||
| 363 | | "nav.admin" | 363 | | "nav.admin" |
| 364 | | "nav.menu" | 364 | | "nav.menu" |
| 365 | | "nav.close" | 365 | | "nav.close" |
| 366 | + | "fab.label" | ||
| 367 | + | "fab.hide" | ||
| 368 | + | "fab.restore" | ||
| 366 | | "nav.collapse" | 369 | | "nav.collapse" |
| 367 | | "nav.expand" | 370 | | "nav.expand" |
| 368 | | "nav.account" | 371 | | "nav.account" |
src/index.cssmodified+4 -0
| @@ -503,6 +503,10 @@ | |||
| 503 | /* WCAG 2.4.11: a focused or anchored element must not land under the | 503 | /* WCAG 2.4.11: a focused or anchored element must not land under the |
| 504 | sticky 45px shell header. */ | 504 | sticky 45px shell header. */ |
| 505 | scroll-padding-top: calc(45px + env(safe-area-inset-top) + 0.5rem); | 505 | scroll-padding-top: calc(45px + env(safe-area-inset-top) + 0.5rem); |
| 506 | + /* Phones have no header; the floating action button replaced it. */ | ||
| 507 | + @media (width < 64rem) { | ||
| 508 | + scroll-padding-top: calc(env(safe-area-inset-top) + 0.5rem); | ||
| 509 | + } | ||
| 506 | font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; | 510 | font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; |
| 507 | -webkit-font-smoothing: antialiased; | 511 | -webkit-font-smoothing: antialiased; |
| 508 | -moz-osx-font-smoothing: grayscale; | 512 | -moz-osx-font-smoothing: grayscale; |
src/lib/floating-button.test.tsadded+75 -0
| @@ -0,0 +1,75 @@ | |||
| 1 | +import { | ||
| 2 | + DEFAULT_FLOATING_BUTTON, | ||
| 3 | + radialOffsets, | ||
| 4 | + readFloatingButton, | ||
| 5 | + writeFloatingButton, | ||
| 6 | +} from "@/lib/floating-button"; | ||
| 7 | + | ||
| 8 | +// The node project has no DOM storage; a Map-backed stand-in is enough here. | ||
| 9 | +function stubStorage() { | ||
| 10 | + const store = new Map<string, string>(); | ||
| 11 | + vi.stubGlobal("window", {}); | ||
| 12 | + vi.stubGlobal("localStorage", { | ||
| 13 | + getItem: (key: string) => store.get(key) ?? null, | ||
| 14 | + setItem: (key: string, value: string) => void store.set(key, value), | ||
| 15 | + clear: () => store.clear(), | ||
| 16 | + }); | ||
| 17 | +} | ||
| 18 | + | ||
| 19 | +describe("floating button state", () => { | ||
| 20 | + beforeEach(stubStorage); | ||
| 21 | + afterEach(() => vi.unstubAllGlobals()); | ||
| 22 | + | ||
| 23 | + it("starts bottom-right and unparked", () => { | ||
| 24 | + expect(readFloatingButton()).toEqual(DEFAULT_FLOATING_BUTTON); | ||
| 25 | + }); | ||
| 26 | + | ||
| 27 | + it("round-trips a saved spot", () => { | ||
| 28 | + writeFloatingButton({ x: 0.2, y: 0.4, parked: true }); | ||
| 29 | + expect(readFloatingButton()).toEqual({ x: 0.2, y: 0.4, parked: true }); | ||
| 30 | + }); | ||
| 31 | + | ||
| 32 | + it("clamps out-of-range shares and ignores junk", () => { | ||
| 33 | + localStorage.setItem("hezarfen.floatingButton", JSON.stringify({ x: 3, y: "top", parked: "yes" })); | ||
| 34 | + expect(readFloatingButton()).toEqual({ x: 1, y: 1, parked: false }); | ||
| 35 | + localStorage.setItem("hezarfen.floatingButton", "{not json"); | ||
| 36 | + expect(readFloatingButton()).toEqual(DEFAULT_FLOATING_BUTTON); | ||
| 37 | + }); | ||
| 38 | +}); | ||
| 39 | + | ||
| 40 | +describe("radialOffsets", () => { | ||
| 41 | + const R = 80; | ||
| 42 | + | ||
| 43 | + it("fans a bottom-right button up and to the left only", () => { | ||
| 44 | + const offsets = radialOffsets(3, 0.95, 0.95, R); | ||
| 45 | + for (const { dx, dy } of offsets) { | ||
| 46 | + expect(dx).toBeLessThanOrEqual(1e-9); | ||
| 47 | + expect(dy).toBeLessThanOrEqual(1e-9); | ||
| 48 | + } | ||
| 49 | + // Quarter circle: the ends sit straight left and straight up. | ||
| 50 | + expect(offsets[0].dx).toBeCloseTo(-R); | ||
| 51 | + expect(offsets[2].dy).toBeCloseTo(-R); | ||
| 52 | + }); | ||
| 53 | + | ||
| 54 | + it("fans a top-left button down and to the right only", () => { | ||
| 55 | + for (const { dx, dy } of radialOffsets(3, 0.05, 0.05, R)) { | ||
| 56 | + expect(dx).toBeGreaterThanOrEqual(-1e-9); | ||
| 57 | + expect(dy).toBeGreaterThanOrEqual(-1e-9); | ||
| 58 | + } | ||
| 59 | + }); | ||
| 60 | + | ||
| 61 | + it("fans away from a side edge", () => { | ||
| 62 | + for (const { dx } of radialOffsets(3, 0.02, 0.5, R)) expect(dx).toBeGreaterThan(0); | ||
| 63 | + }); | ||
| 64 | + | ||
| 65 | + it("opens upwards from the middle of the screen", () => { | ||
| 66 | + const [only] = radialOffsets(1, 0.5, 0.5, R); | ||
| 67 | + expect(only.dx).toBeCloseTo(0); | ||
| 68 | + expect(only.dy).toBeCloseTo(-R); | ||
| 69 | + }); | ||
| 70 | + | ||
| 71 | + it("keeps every item at the radius", () => { | ||
| 72 | + for (const { dx, dy } of radialOffsets(4, 0.3, 0.8, R)) expect(Math.hypot(dx, dy)).toBeCloseTo(R); | ||
| 73 | + expect(radialOffsets(0, 0.5, 0.5, R)).toEqual([]); | ||
| 74 | + }); | ||
| 75 | +}); | ||
src/lib/floating-button.tsadded+75 -0
| @@ -0,0 +1,75 @@ | |||
| 1 | +/** | ||
| 2 | + * Where the phone shell's floating action button rests. Kept per device in | ||
| 3 | + * localStorage: a spot a thumb picked on one phone means nothing on another. | ||
| 4 | + * | ||
| 5 | + * The position is stored as a share (0–1) of the travel the button has inside | ||
| 6 | + * its layer, not in pixels, so it survives a rotation or a resized window. | ||
| 7 | + */ | ||
| 8 | +export interface FloatingButtonState { | ||
| 9 | + x: number; | ||
| 10 | + y: number; | ||
| 11 | + /** Put away onto the screen edge by dropping it on the hide target. */ | ||
| 12 | + parked: boolean; | ||
| 13 | +} | ||
| 14 | + | ||
| 15 | +const STORAGE_KEY = "hezarfen.floatingButton"; | ||
| 16 | + | ||
| 17 | +/** Bottom-right, above the tab bar. */ | ||
| 18 | +export const DEFAULT_FLOATING_BUTTON: FloatingButtonState = { x: 1, y: 1, parked: false }; | ||
| 19 | + | ||
| 20 | +function canUseStorage(): boolean { | ||
| 21 | + return typeof window !== "undefined" && typeof localStorage !== "undefined"; | ||
| 22 | +} | ||
| 23 | + | ||
| 24 | +export const clamp01 = (value: number) => Math.min(1, Math.max(0, value)); | ||
| 25 | + | ||
| 26 | +const share = (value: unknown, fallback: number) => | ||
| 27 | + typeof value === "number" && Number.isFinite(value) ? clamp01(value) : fallback; | ||
| 28 | + | ||
| 29 | +export function readFloatingButton(): FloatingButtonState { | ||
| 30 | + if (!canUseStorage()) return { ...DEFAULT_FLOATING_BUTTON }; | ||
| 31 | + try { | ||
| 32 | + const raw = localStorage.getItem(STORAGE_KEY); | ||
| 33 | + if (!raw) return { ...DEFAULT_FLOATING_BUTTON }; | ||
| 34 | + const value = JSON.parse(raw) as Partial<FloatingButtonState> | null; | ||
| 35 | + return { | ||
| 36 | + x: share(value?.x, DEFAULT_FLOATING_BUTTON.x), | ||
| 37 | + y: share(value?.y, DEFAULT_FLOATING_BUTTON.y), | ||
| 38 | + parked: value?.parked === true, | ||
| 39 | + }; | ||
| 40 | + } catch { | ||
| 41 | + return { ...DEFAULT_FLOATING_BUTTON }; | ||
| 42 | + } | ||
| 43 | +} | ||
| 44 | + | ||
| 45 | +export function writeFloatingButton(state: FloatingButtonState): void { | ||
| 46 | + if (!canUseStorage()) return; | ||
| 47 | + try { | ||
| 48 | + localStorage.setItem(STORAGE_KEY, JSON.stringify(state)); | ||
| 49 | + } catch { | ||
| 50 | + // ignore — a private window or a full quota just forgets the spot | ||
| 51 | + } | ||
| 52 | +} | ||
| 53 | + | ||
| 54 | +/** | ||
| 55 | + * Offsets of the radial menu items from the button's centre. | ||
| 56 | + * | ||
| 57 | + * The fan opens towards the middle of the screen, so wherever the button was | ||
| 58 | + * dragged its items land on screen: a corner opens a quarter circle along its | ||
| 59 | + * diagonal, an edge a wider fan away from that edge, and the middle a fan | ||
| 60 | + * upwards. `cx`/`cy` are the button centre as a share of the layer; measuring | ||
| 61 | + * in that unit square keeps a corner's fan on the diagonal whatever the | ||
| 62 | + * phone's aspect ratio. | ||
| 63 | + */ | ||
| 64 | +export function radialOffsets(count: number, cx: number, cy: number, radius: number): { dx: number; dy: number }[] { | ||
| 65 | + if (count <= 0) return []; | ||
| 66 | + const vx = 0.5 - cx; | ||
| 67 | + const vy = 0.5 - cy; | ||
| 68 | + const heading = Math.hypot(vx, vy) < 0.15 ? -Math.PI / 2 : Math.atan2(vy, vx); | ||
| 69 | + const corner = Math.abs(vx) > 0.25 && Math.abs(vy) > 0.25; | ||
| 70 | + const spread = count === 1 ? 0 : corner ? Math.PI / 2 : (Math.PI * 5) / 6; | ||
| 71 | + return Array.from({ length: count }, (_, i) => { | ||
| 72 | + const angle = heading + (count === 1 ? 0 : (i / (count - 1) - 0.5) * spread); | ||
| 73 | + return { dx: Math.cos(angle) * radius, dy: Math.sin(angle) * radius }; | ||
| 74 | + }); | ||
| 75 | +} | ||