↩️ the shell back button never leaves the app
Browse files at this commit283453682c7cde56283edf462d1dffb60c0bfbe0Parent: 3eb380a- fix
- ↩️ the shell back button never leaves the app
3 changed files +79 -2
- src/components/layout/app-shell.tsx +11 -2
- src/lib/back-target.test.ts +35 -0
- src/lib/back-target.ts +33 -0
src/components/layout/app-shell.tsxmodified+11 -2
| @@ -1,5 +1,5 @@ | |||
| 1 | import { Show, createEffect, createMemo, createSignal, type ParentProps } from "solid-js"; | 1 | import { Show, createEffect, createMemo, createSignal, type ParentProps } from "solid-js"; |
| 2 | -import { Link, useLocation, useNavigate } from "@tanstack/solid-router"; | 2 | +import { Link, useCanGoBack, useLocation, useNavigate, useRouter } from "@tanstack/solid-router"; |
| 3 | import { LogoMark } from "@/components/brand/logo-mark"; | 3 | 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"; |
| @@ -21,6 +21,7 @@ import { celebiPanelOpen, openCelebiPanel, setCelebiPanelOpen } from "@/stores/c | |||
| 21 | import { commandPaletteOpen, openCommandPalette, setCommandPaletteOpen } from "@/stores/command-palette"; | 21 | import { commandPaletteOpen, openCommandPalette, setCommandPaletteOpen } from "@/stores/command-palette"; |
| 22 | import { ShellFeedProvider } from "@/stores/shell-feed-context"; | 22 | import { ShellFeedProvider } from "@/stores/shell-feed-context"; |
| 23 | import { usePreferences, useT } from "@/stores/preferences-context"; | 23 | import { usePreferences, useT } from "@/stores/preferences-context"; |
| 24 | +import { backTarget } from "@/lib/back-target"; | ||
| 24 | import { cn } from "@/lib/cn"; | 25 | import { cn } from "@/lib/cn"; |
| 25 | import { ModuleGate } from "@/components/layout/module-gate"; | 26 | import { ModuleGate } from "@/components/layout/module-gate"; |
| 26 | 27 | ||
| @@ -37,6 +38,14 @@ export function AppShell(props: ParentProps) { | |||
| 37 | const [profileOpen, setProfileOpen] = createSignal(false); | 38 | const [profileOpen, setProfileOpen] = createSignal(false); |
| 38 | const collapsed = () => prefs.sidebarCollapsed(); | 39 | const collapsed = () => prefs.sidebarCollapsed(); |
| 39 | const location = useLocation(); | 40 | const location = useLocation(); |
| 41 | + const router = useRouter(); | ||
| 42 | + const canGoBack = useCanGoBack(); | ||
| 43 | + // A deep link or a fresh tab has no in-app entry behind it, and | ||
| 44 | + // history.back() there would leave the app: go to the parent route instead. | ||
| 45 | + const goBack = () => { | ||
| 46 | + if (canGoBack()) return router.history.back(); | ||
| 47 | + void navigate({ to: backTarget(location().pathname, Object.keys(router.routesByPath)) }); | ||
| 48 | + }; | ||
| 40 | const fullScreen = () => location().pathname.startsWith("/exam-room/"); | 49 | const fullScreen = () => location().pathname.startsWith("/exam-room/"); |
| 41 | const routeLabel = createMemo(() => { | 50 | const routeLabel = createMemo(() => { |
| 42 | const key = routeLabelKey(location().pathname, auth.user()?.role); | 51 | const key = routeLabelKey(location().pathname, auth.user()?.role); |
| @@ -130,7 +139,7 @@ export function AppShell(props: ParentProps) { | |||
| 130 | <Show when={location().pathname !== "/"}> | 139 | <Show when={location().pathname !== "/"}> |
| 131 | <button | 140 | <button |
| 132 | type="button" | 141 | type="button" |
| 133 | - onClick={() => window.history.back()} | 142 | + onClick={goBack} |
| 134 | class="topbar-control flex h-9 w-9 shrink-0 items-center justify-center rounded-md outline-hidden focus-visible:ring-2 focus-visible:ring-ring" | 143 | class="topbar-control flex h-9 w-9 shrink-0 items-center justify-center rounded-md outline-hidden focus-visible:ring-2 focus-visible:ring-ring" |
| 135 | aria-label={t("common.back")} | 144 | aria-label={t("common.back")} |
| 136 | title={t("common.back")} | 145 | title={t("common.back")} |
src/lib/back-target.test.tsadded+35 -0
| @@ -0,0 +1,35 @@ | |||
| 1 | +import { backTarget } from "./back-target"; | ||
| 2 | + | ||
| 3 | +const ROUTES = [ | ||
| 4 | + "/", | ||
| 5 | + "/exams", | ||
| 6 | + "/exams/$id", | ||
| 7 | + "/exams/$id/live", | ||
| 8 | + "/instances/$id", | ||
| 9 | + "/exam-room/$id", | ||
| 10 | + "/courses", | ||
| 11 | + "/management/classes", | ||
| 12 | + "/management/classes/$id", | ||
| 13 | + "/management/settings", | ||
| 14 | + "/profile/$userId", | ||
| 15 | +]; | ||
| 16 | + | ||
| 17 | +it("goes from a detail page to its list", () => { | ||
| 18 | + expect(backTarget("/exams/42", ROUTES)).toBe("/exams"); | ||
| 19 | + expect(backTarget("/management/classes/7", ROUTES)).toBe("/management/classes"); | ||
| 20 | +}); | ||
| 21 | + | ||
| 22 | +it("goes from a nested page to the record it belongs to", () => { | ||
| 23 | + expect(backTarget("/exams/42/live", ROUTES)).toBe("/exams/42"); | ||
| 24 | +}); | ||
| 25 | + | ||
| 26 | +it("uses the hub for details that have no list of their own", () => { | ||
| 27 | + expect(backTarget("/instances/7", ROUTES)).toBe("/courses"); | ||
| 28 | + expect(backTarget("/exam-room/3", ROUTES)).toBe("/exams"); | ||
| 29 | + expect(backTarget("/profile/u-1", ROUTES)).toBe("/"); | ||
| 30 | +}); | ||
| 31 | + | ||
| 32 | +it("falls back to home when no ancestor route exists", () => { | ||
| 33 | + expect(backTarget("/management/settings", ROUTES)).toBe("/"); | ||
| 34 | + expect(backTarget("/exams", ROUTES)).toBe("/"); | ||
| 35 | +}); | ||
src/lib/back-target.tsadded+33 -0
| @@ -0,0 +1,33 @@ | |||
| 1 | +/** | ||
| 2 | + * Where the shell's back button goes when there is no in-app history entry to | ||
| 3 | + * return to (a deep link, a new tab, a reload). Going `history.back()` there | ||
| 4 | + * leaves the app, so we walk up the path to the nearest route that exists. | ||
| 5 | + */ | ||
| 6 | + | ||
| 7 | +/** Detail routes whose natural parent is not their path prefix. */ | ||
| 8 | +const PARENT_OVERRIDES: Record<string, string> = { | ||
| 9 | + instances: "/courses", | ||
| 10 | + "exam-room": "/exams", | ||
| 11 | + profile: "/", | ||
| 12 | +}; | ||
| 13 | + | ||
| 14 | +const matches = (pattern: string, segments: string[]) => { | ||
| 15 | + const parts = pattern.split("/").filter(Boolean); | ||
| 16 | + return parts.length === segments.length && parts.every((part, i) => part.startsWith("$") || part === segments[i]); | ||
| 17 | +}; | ||
| 18 | + | ||
| 19 | +/** | ||
| 20 | + * @param pathname current location pathname | ||
| 21 | + * @param routePaths every registered route path, e.g. `/exams/$id` | ||
| 22 | + */ | ||
| 23 | +export function backTarget(pathname: string, routePaths: readonly string[]): string { | ||
| 24 | + const segments = pathname.split("/").filter(Boolean); | ||
| 25 | + const override = segments.length > 1 ? PARENT_OVERRIDES[segments[0]] : undefined; | ||
| 26 | + if (override && segments.length === 2) return override; | ||
| 27 | + | ||
| 28 | + for (let depth = segments.length - 1; depth > 0; depth -= 1) { | ||
| 29 | + const candidate = segments.slice(0, depth); | ||
| 30 | + if (routePaths.some((pattern) => matches(pattern, candidate))) return `/${candidate.join("/")}`; | ||
| 31 | + } | ||
| 32 | + return "/"; | ||
| 33 | +} | ||