hezarfen_frontend
Clone
https://git.tahinli.com/Hezarfen/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits
| 1 | import { Show, createEffect, createMemo, createSignal, onCleanup, onMount, type ParentProps } from "solid-js"; |
| 2 | import { Dynamic } from "solid-js/web"; |
| 3 | import { Link, useLocation, useNavigate } from "@tanstack/solid-router"; |
| 4 | import { LogoMark } from "@/components/brand/logo-mark"; |
| 5 | import { AccountProfileDialog } from "@/components/users/account-profile-dialog"; |
| 6 | import { CelebiPanel } from "@/components/layout/celebi-panel"; |
| 7 | import { CommandPalette } from "@/components/layout/command-palette"; |
| 8 | import { MobileQuickActions } from "@/components/layout/mobile-quick-actions"; |
| 9 | import { MobileTabBar } from "@/components/layout/mobile-tab-bar"; |
| 10 | import { NavBar } from "@/components/layout/nav-bar"; |
| 11 | import { NetworkStatusBanner } from "@/components/layout/network-status-banner"; |
| 12 | import { NotificationCenter } from "@/components/layout/notification-center"; |
| 13 | import { routeLabelKey } from "@/components/layout/nav-items"; |
| 14 | import { ShellMessagesButton } from "@/components/layout/shell-messages-button"; |
| 15 | import { MobileNavSheet } from "@/components/layout/mobile-nav-sheet"; |
| 16 | import { SideNav } from "@/components/layout/side-nav"; |
| 17 | import { SidebarAccount } from "@/components/layout/sidebar-account"; |
| 18 | import { IconSparkles } from "@/components/ui/icons"; |
| 19 | import { TooltipTrigger } from "@/components/ui/tooltip"; |
| 20 | import { RailTip } from "@/components/layout/rail-tip"; |
| 21 | import { Toaster } from "@/components/ui/toast"; |
| 22 | import { useAuth } from "@/stores/auth-context"; |
| 23 | import { useModules } from "@/stores/modules-context"; |
| 24 | import { useSchool } from "@/stores/school-context"; |
| 25 | import { celebiPanelOpen, openCelebiPanel, setCelebiPanelOpen } from "@/stores/celebi-panel"; |
| 26 | import { commandPaletteOpen, openCommandPalette, setCommandPaletteOpen } from "@/stores/command-palette"; |
| 27 | import { quickActionsSuppressed } from "@/stores/quick-actions"; |
| 28 | import { ShellFeedProvider } from "@/stores/shell-feed-context"; |
| 29 | import { usePreferences, useT } from "@/stores/preferences-context"; |
| 30 | import { cn } from "@/lib/cn"; |
| 31 | import { createMediaQuery } from "@/lib/create-media-query"; |
| 32 | import { ModuleGate } from "@/components/layout/module-gate"; |
| 33 | |
| 34 | const SIDEBAR_EXPANDED = "w-[260px]"; |
| 35 | // 56px: the icon rail, in the 48–56px band Linear and shadcn's icon mode use. |
| 36 | const SIDEBAR_COLLAPSED = "w-14"; |
| 37 | |
| 38 | export function AppShell(props: ParentProps) { |
| 39 | const auth = useAuth(); |
| 40 | const prefs = usePreferences(); |
| 41 | const t = useT(); |
| 42 | const school = useSchool(); |
| 43 | const modules = useModules(); |
| 44 | const navigate = useNavigate(); |
| 45 | const [mobileOpen, setMobileOpen] = createSignal(false); |
| 46 | const [profileOpen, setProfileOpen] = createSignal(false); |
| 47 | const collapsed = () => prefs.sidebarCollapsed(); |
| 48 | const brandLabel = () => (school.name() ? `${t("app.name")} · ${school.name()}` : t("app.name")); |
| 49 | const location = useLocation(); |
| 50 | const fullScreen = () => location().pathname.startsWith("/exam-room/"); |
| 51 | // Below lg the header gives way to the floating action button. Chosen in |
| 52 | // JS rather than with CSS so only one of the two notification surfaces is |
| 53 | // mounted, and their dismissed state never has two in-memory copies. |
| 54 | const phone = createMediaQuery("(max-width: 63.98rem)"); |
| 55 | const routeLabel = createMemo(() => { |
| 56 | const key = routeLabelKey(location().pathname, auth.user()?.role, location().search as Record<string, unknown>); |
| 57 | return key ? t(key) : ""; |
| 58 | }); |
| 59 | // The page's own h1, when it renders one (detail pages, the 404). Watched |
| 60 | // rather than declared per route: the shell names the page with an h1 of its |
| 61 | // own only where the page has none, so no screen ends up with two. |
| 62 | let content: HTMLDivElement | undefined; |
| 63 | const [pageHeading, setPageHeading] = createSignal<string | null>(null); |
| 64 | onMount(() => { |
| 65 | if (!content || typeof MutationObserver === "undefined") return; |
| 66 | const read = () => { |
| 67 | const heading = content?.querySelector("h1:not([data-shell-title])"); |
| 68 | setPageHeading(heading ? (heading.textContent ?? "").trim() : null); |
| 69 | }; |
| 70 | read(); |
| 71 | const observer = new MutationObserver(read); |
| 72 | observer.observe(content, { childList: true, subtree: true, characterData: true }); |
| 73 | onCleanup(() => observer.disconnect()); |
| 74 | }); |
| 75 | // Unlisted routes (an instance, the 404) borrow the page's own heading. |
| 76 | const pageTitle = () => routeLabel() || pageHeading() || ""; |
| 77 | // One place names the browser tab for every shell route, from the same |
| 78 | // label the header shows. |
| 79 | createEffect(() => { |
| 80 | const label = pageTitle(); |
| 81 | document.title = label ? `${label} · ${t("app.name")}` : t("app.name"); |
| 82 | }); |
| 83 | const shellChrome = () => Boolean(auth.user()) && !fullScreen(); |
| 84 | // Room below the last row for the quick-action button resting over the tab |
| 85 | // bar, so a pager or a last card's actions can scroll clear of it. |
| 86 | // A board fills the content column edge to edge; the floating button sat |
| 87 | // on its bottom-right corner, over the canvas's own controls. |
| 88 | const boardPage = () => /^\/whiteboards\/[^/]+/.test(location().pathname); |
| 89 | const fabHidden = () => quickActionsSuppressed() || boardPage(); |
| 90 | const fabClearance = () => shellChrome() && phone() && !fabHidden(); |
| 91 | const logout = async () => { |
| 92 | await auth.logout(); |
| 93 | void navigate({ to: "/login" }); |
| 94 | }; |
| 95 | |
| 96 | return ( |
| 97 | <div class="min-h-[var(--app-viewport)] bg-background text-foreground"> |
| 98 | <a |
| 99 | href="#main-content" |
| 100 | class="sr-only focus:not-sr-only focus:fixed focus:left-3 focus:top-3 focus:z-[90] focus:rounded-md focus:bg-surface-base focus:px-3 focus:py-2 focus:text-sm focus:font-medium focus:shadow-lg focus:ring-2 focus:ring-ring" |
| 101 | > |
| 102 | {t("common.skipToContent")} |
| 103 | </a> |
| 104 | <Show when={!auth.user()}> |
| 105 | <NavBar /> |
| 106 | </Show> |
| 107 | <ShellFeedProvider> |
| 108 | <div class="flex w-full"> |
| 109 | <Show when={auth.user() && !fullScreen()}> |
| 110 | <aside |
| 111 | class={cn( |
| 112 | "sticky top-[env(safe-area-inset-top)] z-40 hidden h-[var(--app-viewport)] shrink-0 border-r border-border bg-sidebar text-sidebar-foreground transition-[width] duration-200 ease-out lg:flex lg:flex-col", |
| 113 | collapsed() ? SIDEBAR_COLLAPSED : SIDEBAR_EXPANDED, |
| 114 | )} |
| 115 | > |
| 116 | {/* Same box as the top header (49px, pt-1.5, border-border/70), so |
| 117 | the two hairlines run as one line across the page in both |
| 118 | sidebar states. */} |
| 119 | <div |
| 120 | class={cn( |
| 121 | "flex h-[49px] shrink-0 items-center border-b border-border/70 pt-1.5", |
| 122 | collapsed() ? "justify-center px-2" : "px-3", |
| 123 | )} |
| 124 | > |
| 125 | <RailTip label={brandLabel()} enabled={collapsed()}> |
| 126 | <TooltipTrigger |
| 127 | as={Link} |
| 128 | to="/" |
| 129 | class={cn( |
| 130 | "flex min-w-0 items-center gap-2.5 rounded-md outline-hidden focus-visible:ring-2 focus-visible:ring-ring", |
| 131 | collapsed() ? "justify-center" : "flex-1", |
| 132 | )} |
| 133 | aria-label={brandLabel()} |
| 134 | title={collapsed() ? undefined : brandLabel()} |
| 135 | > |
| 136 | <span class="flex h-8 w-8 shrink-0 items-center justify-center text-foreground"> |
| 137 | <LogoMark size={collapsed() ? 24 : 28} /> |
| 138 | </span> |
| 139 | <Show when={!collapsed()}> |
| 140 | <span class="truncate text-base font-semibold tracking-tight text-foreground dark:text-white 2xl:text-lg">{t("app.name")}</span> |
| 141 | </Show> |
| 142 | </TooltipTrigger> |
| 143 | </RailTip> |
| 144 | </div> |
| 145 | |
| 146 | <div class="min-h-0 flex-1 overflow-y-auto overflow-x-hidden px-1.5 py-2.5"> |
| 147 | <SideNav |
| 148 | collapsed={collapsed()} |
| 149 | onOpenCelebi={() => openCelebiPanel()} |
| 150 | onOpenProfile={() => setProfileOpen(true)} |
| 151 | /> |
| 152 | </div> |
| 153 | |
| 154 | <SidebarAccount collapsed={collapsed()} onLogout={logout} onToggleCollapse={() => prefs.toggleSidebar()} /> |
| 155 | </aside> |
| 156 | </Show> |
| 157 | |
| 158 | <Show when={auth.user()}> |
| 159 | <MobileNavSheet |
| 160 | open={mobileOpen()} |
| 161 | onClose={() => setMobileOpen(false)} |
| 162 | onLogout={logout} |
| 163 | onOpenCelebi={() => openCelebiPanel()} |
| 164 | onOpenProfile={() => setProfileOpen(true)} |
| 165 | /> |
| 166 | </Show> |
| 167 | |
| 168 | {/* The menu sheet is modal: what it covers leaves the tab order and |
| 169 | the accessibility tree while it is up. */} |
| 170 | <main class="min-w-0 flex-1" inert={mobileOpen() || undefined}> |
| 171 | <Show when={auth.user() && !fullScreen() && !phone()}> |
| 172 | <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"> |
| 173 | <div class="flex min-w-0 items-center gap-2 text-sm"> |
| 174 | {/* Account context first, Cloudflare-style: "School / Page". */} |
| 175 | <Show when={school.name()}> |
| 176 | {(name) => ( |
| 177 | <> |
| 178 | <span class="hidden max-w-56 truncate font-medium text-muted-foreground sm:block" title={name()}> |
| 179 | {name()} |
| 180 | </span> |
| 181 | <span aria-hidden="true" class="hidden shrink-0 text-muted-foreground/50 sm:block">/</span> |
| 182 | </> |
| 183 | )} |
| 184 | </Show> |
| 185 | {/* The page's h1 unless the page renders its own. */} |
| 186 | <Dynamic |
| 187 | component={pageHeading() === null ? "h1" : "span"} |
| 188 | data-shell-title="" |
| 189 | class="hidden truncate text-sm font-semibold sm:block" |
| 190 | title={pageTitle()} |
| 191 | > |
| 192 | {pageTitle()} |
| 193 | </Dynamic> |
| 194 | </div> |
| 195 | |
| 196 | <div class="min-w-0 flex-1" /> |
| 197 | |
| 198 | <div class="flex shrink-0 items-center justify-end gap-2"> |
| 199 | <Show when={modules.isEnabled("messages")}> |
| 200 | <ShellMessagesButton /> |
| 201 | </Show> |
| 202 | <NotificationCenter /> |
| 203 | <Show when={modules.isEnabled("chatbot")}> |
| 204 | <button |
| 205 | type="button" |
| 206 | class="topbar-ai-control hidden h-9 shrink-0 items-center justify-center gap-2 rounded-md px-3 text-xs font-semibold outline-hidden focus-visible:ring-2 focus-visible:ring-ring active:scale-[0.98] sm:flex" |
| 207 | aria-label={t("ai.askCelebi")} |
| 208 | title={t("ai.askCelebi")} |
| 209 | onClick={() => openCelebiPanel()} |
| 210 | > |
| 211 | <IconSparkles class="h-4 w-4" /> |
| 212 | <span>{t("ai.askCelebi")}</span> |
| 213 | </button> |
| 214 | </Show> |
| 215 | </div> |
| 216 | </header> |
| 217 | </Show> |
| 218 | <Show when={auth.user() && !fullScreen()}> |
| 219 | <NetworkStatusBanner /> |
| 220 | </Show> |
| 221 | {/* The skip link's target starts below the header, so it lands on |
| 222 | the page rather than on the header's controls. */} |
| 223 | <div |
| 224 | id="main-content" |
| 225 | tabIndex={-1} |
| 226 | ref={(el) => { |
| 227 | content = el; |
| 228 | }} |
| 229 | class={cn( |
| 230 | // Every page fills the content column, as Messages always did: a |
| 231 | // fixed 1180px cap left wide screens mostly empty margin. |
| 232 | "w-full px-4 py-6 outline-hidden sm:px-6 lg:px-10 lg:py-6", |
| 233 | shellChrome() && "pb-[calc(3.5rem+max(env(safe-area-inset-bottom),var(--android-nav-inset,0px)))] lg:pb-6", |
| 234 | fabClearance() && "pb-[calc(9rem+max(env(safe-area-inset-bottom),var(--android-nav-inset,0px)))]", |
| 235 | )} |
| 236 | > |
| 237 | {/* No header below lg, so the page title opens the content. */} |
| 238 | <Show when={shellChrome() && phone() && pageHeading() === null && pageTitle()}> |
| 239 | <h1 data-shell-title="" class="-mt-2 mb-3 truncate text-lg font-semibold tracking-tight text-text-strong"> |
| 240 | {pageTitle()} |
| 241 | </h1> |
| 242 | </Show> |
| 243 | <Show when={auth.user()} fallback={props.children}> |
| 244 | <ModuleGate>{props.children}</ModuleGate> |
| 245 | </Show> |
| 246 | </div> |
| 247 | </main> |
| 248 | </div> |
| 249 | {/* MobileTabBar and the quick actions stay inside the provider — they are |
| 250 | shell surfaces, so a useShellFeed() badge there must not throw. */} |
| 251 | <Show when={auth.user() && !fullScreen()}> |
| 252 | <div inert={mobileOpen() || undefined}> |
| 253 | <MobileTabBar onMenu={() => setMobileOpen(true)} onSearch={openCommandPalette} /> |
| 254 | </div> |
| 255 | </Show> |
| 256 | <Show when={auth.user() && !fullScreen() && phone()}> |
| 257 | <MobileQuickActions |
| 258 | hidden={mobileOpen() || celebiPanelOpen() || commandPaletteOpen() || profileOpen() || fabHidden()} |
| 259 | /> |
| 260 | </Show> |
| 261 | </ShellFeedProvider> |
| 262 | <Show when={auth.user()}> |
| 263 | <CelebiPanel open={celebiPanelOpen()} onOpenChange={setCelebiPanelOpen} /> |
| 264 | <CommandPalette |
| 265 | open={commandPaletteOpen()} |
| 266 | onOpenChange={setCommandPaletteOpen} |
| 267 | onOpenCelebi={() => openCelebiPanel()} |
| 268 | onOpenProfile={() => setProfileOpen(true)} |
| 269 | /> |
| 270 | <AccountProfileDialog open={profileOpen()} onOpenChange={setProfileOpen} /> |
| 271 | </Show> |
| 272 | <Toaster /> |
| 273 | </div> |
| 274 | ); |
| 275 | } |