From 35cb7da929e42ef7dca71c85bed96aff4ae5f970 Mon Sep 17 00:00:00 2001 From: Philipp Traber Date: Wed, 14 Oct 2026 16:52:22 +0200 Subject: [PATCH] Separate admins' own content from managing other users MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Short links, Link pages and Overview now show only the admin's own namespace and root; other users' content moved to Administration - Administration lists users with their link/page/click counts; each opens /~/admin/users/ with that user's pages and links and lets admins create content in their namespace - other users' pages open under /~/admin/users//…, keep the Administration section active and link back to the user - one PageTable component for all lists, so search and the namespace filter (All / mine / Root, admins only) behave the same everywhere - the create dialog offers admins only their own namespace and root; the existing-link picker groups links by namespace Co-Authored-By: Claude Opus 5.5 --- src/web/app/App.tsx | 9 +- src/web/app/Shell.tsx | 4 + src/web/components/CreatePageDialog.tsx | 20 ++-- src/web/components/PageTable.tsx | 124 ++++++++++++++++++++++++ src/web/lib/scope.ts | 17 ++++ src/web/router.ts | 42 +++++++- src/web/screens/AdminScreen.tsx | 88 ++++++++++------- src/web/screens/AdminUserScreen.tsx | 109 +++++++++++++++++++++ src/web/screens/ListScreen.tsx | 123 +++-------------------- src/web/screens/OverviewScreen.tsx | 8 +- src/web/screens/detail/ContentTab.tsx | 19 +++- src/web/screens/detail/DetailScreen.tsx | 29 ++++-- src/web/screens/detail/SettingsTab.tsx | 4 +- 13 files changed, 420 insertions(+), 176 deletions(-) create mode 100644 src/web/components/PageTable.tsx create mode 100644 src/web/lib/scope.ts create mode 100644 src/web/screens/AdminUserScreen.tsx diff --git a/src/web/app/App.tsx b/src/web/app/App.tsx index 05dd65f..a271180 100644 --- a/src/web/app/App.tsx +++ b/src/web/app/App.tsx @@ -4,8 +4,9 @@ import { useApi } from "../api.js"; import { buttonClass } from "../components/ui/button.js"; import { Link } from "../components/ui/misc.js"; import { EmptyState } from "../components/ui/surface.js"; -import { parseRoute, usePath } from "../router.js"; +import { parseRoute, setViewer, usePath } from "../router.js"; import { AdminScreen } from "../screens/AdminScreen.js"; +import { AdminUserScreen } from "../screens/AdminUserScreen.js"; import { DetailScreen } from "../screens/detail/DetailScreen.js"; import { ListScreen } from "../screens/ListScreen.js"; import { OverviewScreen } from "../screens/OverviewScreen.js"; @@ -61,6 +62,7 @@ export function App() { location.href = "/~/"; }; + setViewer(me.data.username); const route = parseRoute(path); let screen; switch (route.name) { @@ -77,7 +79,10 @@ export function App() { screen = ; break; case "admin": - screen = me.data.isAdmin ? : null; + screen = me.data.isAdmin ? : null; + break; + case "adminUser": + screen = me.data.isAdmin ? : null; break; } diff --git a/src/web/app/Shell.tsx b/src/web/app/Shell.tsx index f453f20..2b0e3db 100644 --- a/src/web/app/Shell.tsx +++ b/src/web/app/Shell.tsx @@ -15,8 +15,12 @@ function sectionOf(route: Route): Section | null { case "tokens": case "admin": return route.name; + case "adminUser": + return "admin"; case "list": case "detail": + // Other users' pages are opened from Administration and stay in that section. + if (route.name === "detail" && route.owner) return "admin"; return route.kind === "redirect" ? "links" : "pages"; default: return null; diff --git a/src/web/components/CreatePageDialog.tsx b/src/web/components/CreatePageDialog.tsx index 7a82688..d37a3b9 100644 --- a/src/web/components/CreatePageDialog.tsx +++ b/src/web/components/CreatePageDialog.tsx @@ -1,6 +1,6 @@ import { useState } from "react"; import type { Me, PageKind, PageSummary } from "../../shared/api-types.js"; -import { api, ApiRequestError, errorMessage, useApi } from "../api.js"; +import { api, ApiRequestError, errorMessage } from "../api.js"; import { Button } from "./ui/button.js"; import { Dialog } from "./ui/dialog.js"; import { Field, Input, PrefixInput, Select } from "./ui/form.js"; @@ -24,15 +24,17 @@ export function CreatePageDialog({ open, onClose, onCreated, + fixedNamespace, }: { kind: PageKind; me: Me; open: boolean; onClose: () => void; onCreated: (page: PageSummary) => void; + /** Create in this namespace without offering a choice, e.g. when an admin manages another user. */ + fixedNamespace?: string; }) { - const users = useApi(me.isAdmin && open ? "/users" : null); - const [namespace, setNamespace] = useState(me.username); + const [namespace, setNamespace] = useState(fixedNamespace ?? me.username); const [code, setCode] = useState(""); const [value, setValue] = useState(""); const [busy, setBusy] = useState(false); @@ -102,16 +104,12 @@ export function CreatePageDialog({ )} - {me.isAdmin && ( - + {me.isAdmin && fixedNamespace === undefined && ( + {(id) => ( )} diff --git a/src/web/components/PageTable.tsx b/src/web/components/PageTable.tsx new file mode 100644 index 0000000..660dfa0 --- /dev/null +++ b/src/web/components/PageTable.tsx @@ -0,0 +1,124 @@ +import { useMemo, useState, type ReactNode } from "react"; +import { ChevronRight, Link2, PanelsTopLeft, Pin, Search } from "lucide-react"; +import type { PageKind, PageSummary } from "../../shared/api-types.js"; +import { detailPath } from "../router.js"; +import { Input, Select } from "./ui/form.js"; +import { CopyButton, DomainIcon, Link, publicUrl } from "./ui/misc.js"; +import { Badge, Card, EmptyState, Skeleton } from "./ui/surface.js"; + +function Row({ page, showNamespace }: { page: PageSummary; showNamespace: boolean }) { + const isLink = page.kind === "redirect"; + return ( +
+ {isLink ? ( + + ) : ( + + + + )} +
+
+ {/* The link stretches over the whole row; the copy button sits above it. */} + + {page.path} + + {showNamespace && page.namespace === "" && Root} + {page.pinned && ( + }> + Home + + )} +
+
{isLink ? page.dest : page.title}
+
+
{isLink ? page.dest : page.title}
+
{page.totalClicks.toLocaleString()}
+
+
+ +
+ +
+
+ ); +} + +export interface NamespaceOption { + label: string; + match: (namespace: string) => boolean; +} + +// Searchable list of short links or link pages. The toolbar is the same everywhere it's used; +// `namespaces` adds a namespace filter (e.g. "Mine" / "Root" for admins). +export function PageTable({ + kind, + pages, + namespaces, + empty, +}: { + kind: PageKind; + pages: PageSummary[] | undefined; + namespaces?: NamespaceOption[]; + empty: { title: string; description: string; action?: ReactNode }; +}) { + const [query, setQuery] = useState(""); + const [filter, setFilter] = useState(0); + + const ofKind = useMemo(() => (pages ?? []).filter((p) => p.kind === kind), [pages, kind]); + const q = query.trim().toLowerCase(); + const option = namespaces?.[filter]; + const visible = ofKind.filter( + (p) => + (!option || option.match(p.namespace)) && + (!q || p.path.toLowerCase().includes(q) || (p.dest ?? p.title ?? "").toLowerCase().includes(q)), + ); + + return ( + +
+
+ + setQuery(e.target.value)} aria-label="Search" /> +
+ {namespaces && ( +
+ +
+ )} +
+ +
+ + {kind === "redirect" ? "Short link" : "Page"} + {kind === "redirect" ? "Destination" : "Title"} + Clicks + +
+ + {!pages ? ( +
+ {[0, 1, 2].map((i) => ( + + ))} +
+ ) : visible.length ? ( +
+ {visible.map((p) => ( + + ))} +
+ ) : ofKind.length ? ( + } title="No matches" description={namespaces ? "Try a different search or filter." : "Try a different search."} /> + ) : ( + : } title={empty.title} description={empty.description} action={empty.action} /> + )} +
+ ); +} diff --git a/src/web/lib/scope.ts b/src/web/lib/scope.ts new file mode 100644 index 0000000..c006e31 --- /dev/null +++ b/src/web/lib/scope.ts @@ -0,0 +1,17 @@ +import type { Me, PageSummary } from "../../shared/api-types.js"; +import type { NamespaceOption } from "../components/PageTable.js"; + +// "Your" content: your own namespace, plus root for admins. Other users' content is managed under Administration. +export function isMine(page: Pick, me: Me): boolean { + return page.namespace === me.username || (me.isAdmin && page.namespace === ""); +} + +// Namespace filter for admins, who see both their own and root content. +export function myNamespaceOptions(me: Me): NamespaceOption[] | undefined { + if (!me.isAdmin) return undefined; + return [ + { label: "All", match: () => true }, + { label: `/${me.username}/`, match: (ns) => ns === me.username }, + { label: "Root /", match: (ns) => ns === "" }, + ]; +} diff --git a/src/web/router.ts b/src/web/router.ts index b55e317..9c7ca74 100644 --- a/src/web/router.ts +++ b/src/web/router.ts @@ -33,19 +33,47 @@ export function linkHandler(to: string) { export type Route = | { name: "overview" } | { name: "list"; kind: PageKind } - | { name: "detail"; kind: PageKind; id: number; tab: string | undefined } + /** `owner` is set when an admin opens another user's page via Administration. */ + | { name: "detail"; kind: PageKind; id: number; tab: string | undefined; owner?: string } | { name: "tokens" } | { name: "admin" } + | { name: "adminUser"; username: string } | { name: "notFound" }; const KIND_SEGMENT: Record = { links: "redirect", pages: "linkpage" }; +// The signed-in user. Pages of other users are addressed through Administration, +// so the admin's own and root content stays separate from managing everyone else's. +let viewer: string | null = null; +export function setViewer(username: string) { + viewer = username; +} + +const isOtherUsers = (namespace: string | undefined) => namespace !== undefined && namespace !== "" && namespace !== viewer; + export function listPath(kind: PageKind): string { return kind === "redirect" ? "/~/links" : "/~/pages"; } -export function detailPath(page: { id: number; kind: PageKind }, tab?: string): string { - return `${listPath(page.kind)}/${page.id}${tab ? `/${tab}` : ""}`; +export function adminUserPath(username: string): string { + return `/~/admin/users/${encodeURIComponent(username)}`; +} + +export function detailPath(page: { id: number; kind: PageKind; namespace?: string }, tab?: string): string { + const segment = page.kind === "redirect" ? "links" : "pages"; + const base = isOtherUsers(page.namespace) ? `${adminUserPath(page.namespace!)}/${segment}` : `/~/${segment}`; + return `${base}/${page.id}${tab ? `/${tab}` : ""}`; +} + +// Where "back" leads from a page: its owner's admin view for other users' pages, otherwise the list. +export function backPath(page: { kind: PageKind; namespace: string }): string { + return isOtherUsers(page.namespace) ? adminUserPath(page.namespace) : listPath(page.kind); +} + +// Replaces the current URL (no new history entry), e.g. to correct a link to the canonical address. +export function replacePath(to: string) { + history.replaceState(null, "", to); + listeners.forEach((l) => l()); } export function parseRoute(path: string): Route { @@ -53,6 +81,14 @@ export function parseRoute(path: string): Route { if (p === "/~") return { name: "overview" }; if (p === "/~/tokens") return { name: "tokens" }; if (p === "/~/admin") return { name: "admin" }; + + const user = p.match(/^\/~\/admin\/users\/([^/]+)(?:\/(links|pages)\/(\d+)(?:\/([a-z]+))?)?$/); + if (user) { + const username = decodeURIComponent(user[1]!); + if (!user[2]) return { name: "adminUser", username }; + return { name: "detail", kind: KIND_SEGMENT[user[2]]!, id: Number(user[3]), tab: user[4], owner: username }; + } + const m = p.match(/^\/~\/(links|pages)(?:\/(\d+)(?:\/([a-z]+))?)?$/); if (m) { const kind = KIND_SEGMENT[m[1]!]!; diff --git a/src/web/screens/AdminScreen.tsx b/src/web/screens/AdminScreen.tsx index abcdcda..61c9a27 100644 --- a/src/web/screens/AdminScreen.tsx +++ b/src/web/screens/AdminScreen.tsx @@ -1,13 +1,13 @@ import { useEffect, useState } from "react"; -import { PinOff, TriangleAlert, Users } from "lucide-react"; -import type { Conflict, PageSummary } from "../../shared/api-types.js"; +import { ChevronRight, PinOff, TriangleAlert, Users } from "lucide-react"; +import type { Conflict, Me, PageSummary } from "../../shared/api-types.js"; import { api, useApi } from "../api.js"; import { Button } from "../components/ui/button.js"; import { Select } from "../components/ui/form.js"; import { Link } from "../components/ui/misc.js"; import { Badge, Card, CardBody, CardHeader, EmptyState, PageHeader, Skeleton } from "../components/ui/surface.js"; import { useAction } from "../components/ui/toast.js"; -import { detailPath } from "../router.js"; +import { adminUserPath, detailPath } from "../router.js"; function HomepageCard({ pages, reload }: { pages: PageSummary[]; reload: () => Promise }) { const pinned = pages.find((p) => p.pinned); @@ -52,19 +52,61 @@ function HomepageCard({ pages, reload }: { pages: PageSummary[]; reload: () => P ); } -export function AdminScreen() { +function UsersCard({ users, pages, me }: { users: string[] | undefined; pages: PageSummary[] | undefined; me: Me }) { + const others = users?.filter((u) => u !== me.username); + const stats = (u: string) => { + const theirs = (pages ?? []).filter((p) => p.namespace === u); + const links = theirs.filter((p) => p.kind === "redirect").length; + const linkPages = theirs.length - links; + const clicks = theirs.reduce((sum, p) => sum + p.totalClicks, 0); + return `${links} ${links === 1 ? "link" : "links"} · ${linkPages} ${linkPages === 1 ? "page" : "pages"} · ${clicks.toLocaleString()} clicks`; + }; + + return ( + + {others.length}} + /> + {!others ? ( + + ) : others.length ? ( +
    + {others.map((u) => ( +
  • + + {u.charAt(0)} + +
    + + {u} + +
    {pages ? stats(u) : " "}
    +
    + +
  • + ))} +
+ ) : ( + } title="No other users yet" description="Users appear here after their first sign-in." /> + )} +
+ ); +} + +export function AdminScreen({ me }: { me: Me }) { const pages = useApi("/pages"); const users = useApi("/users"); const conflicts = useApi("/conflicts"); - const countByUser = new Map(); - for (const p of pages.data ?? []) countByUser.set(p.namespace, (countByUser.get(p.namespace) ?? 0) + 1); - return ( <> - +
+ + {pages.data ? : } @@ -80,7 +122,7 @@ export function AdminScreen() {
  • /{c.username} - + Open root link
  • @@ -90,34 +132,6 @@ export function AdminScreen() { No conflicts. )}
    - - - {users.data.length}} - /> - {!users.data ? ( - - ) : users.data.length ? ( -
      - {users.data.map((u) => ( -
    • - - {u.charAt(0)} - - /{u}/ - - {countByUser.get(u) ?? 0} {(countByUser.get(u) ?? 0) === 1 ? "page" : "pages"} - -
    • - ))} -
    - ) : ( - } title="No users yet" /> - )} -
    -
    ); diff --git a/src/web/screens/AdminUserScreen.tsx b/src/web/screens/AdminUserScreen.tsx new file mode 100644 index 0000000..cb8cb1f --- /dev/null +++ b/src/web/screens/AdminUserScreen.tsx @@ -0,0 +1,109 @@ +import { useState } from "react"; +import { ChevronRight, Link2, MousePointerClick, PanelsTopLeft, Plus, UserX } from "lucide-react"; +import type { Me, PageKind, PageSummary } from "../../shared/api-types.js"; +import { useApi } from "../api.js"; +import { CreatePageDialog } from "../components/CreatePageDialog.js"; +import { PageTable } from "../components/PageTable.js"; +import { Button, buttonClass } from "../components/ui/button.js"; +import { Link, Stat } from "../components/ui/misc.js"; +import { EmptyState, Skeleton } from "../components/ui/surface.js"; +import { useToast } from "../components/ui/toast.js"; +import { detailPath, navigate } from "../router.js"; + +// Everything one user owns, for admins. Kept apart from the admin's own and root content. +export function AdminUserScreen({ username, me }: { username: string; me: Me }) { + const users = useApi("/users"); + const pages = useApi("/pages"); + const toast = useToast(); + const [creating, setCreating] = useState(null); + + if (users.data && !users.data.includes(username)) { + return ( + } + title="User not found" + description={`Nobody called "${username}" has signed in yet.`} + action={ + + Back to Administration + + } + /> + ); + } + + const theirs = pages.data?.filter((p) => p.namespace === username); + const count = (kind: PageKind) => theirs?.filter((p) => p.kind === kind).length ?? 0; + const clicks = theirs?.reduce((sum, p) => sum + p.totalClicks, 0) ?? 0; + + return ( + <> + + +
    + + {username.charAt(0)} + +
    +

    {username}

    +

    + Manage the links and pages under /{username}/. +

    +
    +
    + + +
    +
    + +
    +
    + {theirs ? ( + <> + } /> + } /> + } /> + + ) : ( + [0, 1, 2].map((i) => ) + )} +
    + +
    +

    Link pages

    + +
    + +
    +

    Short links

    + +
    +
    + + {creating && ( + setCreating(null)} + onCreated={(page) => { + if (page.kind === "linkpage") return navigate(detailPath(page)); + toast(`Created ${page.path}`); + void pages.reload(); + }} + /> + )} + + ); +} diff --git a/src/web/screens/ListScreen.tsx b/src/web/screens/ListScreen.tsx index 0193b1c..b739695 100644 --- a/src/web/screens/ListScreen.tsx +++ b/src/web/screens/ListScreen.tsx @@ -1,19 +1,21 @@ -import { useMemo, useState } from "react"; -import { ChevronRight, Link2, PanelsTopLeft, Pin, Plus, Search } from "lucide-react"; +import { useState } from "react"; +import { Plus } from "lucide-react"; import type { Me, PageKind, PageSummary } from "../../shared/api-types.js"; import { useApi } from "../api.js"; import { CreatePageDialog } from "../components/CreatePageDialog.js"; +import { PageTable } from "../components/PageTable.js"; import { Button } from "../components/ui/button.js"; -import { Input, Select } from "../components/ui/form.js"; -import { CopyButton, DomainIcon, Link, publicUrl } from "../components/ui/misc.js"; -import { Badge, Card, EmptyState, PageHeader, Skeleton } from "../components/ui/surface.js"; +import { publicUrl } from "../components/ui/misc.js"; +import { PageHeader } from "../components/ui/surface.js"; import { useToast } from "../components/ui/toast.js"; +import { isMine, myNamespaceOptions } from "../lib/scope.js"; import { detailPath, navigate } from "../router.js"; -const COPY: Record = { +const COPY: Record = { redirect: { title: "Short links", description: "Short URLs that redirect to another address.", + adminDescription: "Your short links and root links. Other users' links are under Administration.", create: "New short link", empty: "No short links yet", emptyText: "Create your first short link to share long URLs and track clicks.", @@ -21,66 +23,19 @@ const COPY: Record - {isLink ? ( - - ) : ( - - - - )} -
    -
    - {/* The link stretches over the whole row; the copy button sits above it. */} - - {page.path} - - {page.pinned && ( - }> - Home - - )} -
    -
    {isLink ? page.dest : page.title}
    -
    -
    {isLink ? page.dest : page.title}
    -
    {page.totalClicks.toLocaleString()}
    -
    -
    - -
    - -
    - - ); -} - export function ListScreen({ kind, me }: { kind: PageKind; me: Me }) { const pages = useApi("/pages"); const toast = useToast(); - const [query, setQuery] = useState(""); - const [namespace, setNamespace] = useState(null); const [creating, setCreating] = useState(false); const copy = COPY[kind]; - const ofKind = useMemo(() => (pages.data ?? []).filter((p) => p.kind === kind), [pages.data, kind]); - const namespaces = useMemo(() => [...new Set(ofKind.map((p) => p.namespace))].sort(), [ofKind]); - const q = query.trim().toLowerCase(); - const visible = ofKind.filter( - (p) => - (namespace === null || p.namespace === namespace) && - (!q || p.path.toLowerCase().includes(q) || (p.dest ?? p.title ?? "").toLowerCase().includes(q)), - ); - const created = async (page: PageSummary) => { if (page.kind === "linkpage") return navigate(detailPath(page)); await navigator.clipboard?.writeText(publicUrl(page.path)).catch(() => {}); @@ -96,59 +51,13 @@ export function ListScreen({ kind, me }: { kind: PageKind; me: Me }) { return ( <> - - - -
    -
    - - setQuery(e.target.value)} aria-label="Search" /> -
    - {me.isAdmin && namespaces.length > 1 && ( -
    - -
    - )} -
    - -
    - - {kind === "redirect" ? "Short link" : "Page"} - {kind === "redirect" ? "Destination" : "Title"} - Clicks - -
    - - {!pages.data ? ( -
    - {[0, 1, 2].map((i) => ( - - ))} -
    - ) : visible.length ? ( -
    - {visible.map((p) => ( - - ))} -
    - ) : ofKind.length ? ( - } title="No matches" description="Try a different search or namespace." /> - ) : ( - : } title={copy.empty} description={copy.emptyText} action={createButton} /> - )} -
    - + + isMine(p, me))} + namespaces={myNamespaceOptions(me)} + empty={{ title: copy.empty, description: copy.emptyText, action: createButton }} + /> setCreating(false)} onCreated={created} /> ); diff --git a/src/web/screens/OverviewScreen.tsx b/src/web/screens/OverviewScreen.tsx index 800e956..43098c1 100644 --- a/src/web/screens/OverviewScreen.tsx +++ b/src/web/screens/OverviewScreen.tsx @@ -6,6 +6,7 @@ import { CreatePageDialog } from "../components/CreatePageDialog.js"; import { Button, buttonClass } from "../components/ui/button.js"; import { DomainIcon, Link, Stat } from "../components/ui/misc.js"; import { Alert, Card, CardHeader, EmptyState, PageHeader, Skeleton } from "../components/ui/surface.js"; +import { isMine } from "../lib/scope.js"; import { detailPath, navigate } from "../router.js"; function TopPages({ pages }: { pages: PageSummary[] }) { @@ -47,11 +48,12 @@ export function OverviewScreen({ me }: { me: Me }) { const conflicts = useApi(me.isAdmin ? "/conflicts" : null); const [creating, setCreating] = useState(null); - const all = pages.data ?? []; + // Admins see their own and root content here; other users' content is under Administration. + const all = (pages.data ?? []).filter((p) => isMine(p, me)); const links = all.filter((p) => p.kind === "redirect"); const linkPages = all.filter((p) => p.kind === "linkpage"); const clicks = all.reduce((sum, p) => sum + p.totalClicks, 0); - const scope = me.isAdmin ? "all users" : `in /${me.username}/`; + const scope = me.isAdmin ? "yours & root" : `in /${me.username}/`; return ( <> @@ -76,7 +78,7 @@ export function OverviewScreen({ me }: { me: Me }) { {conflicts.data.map((c, i) => ( {i > 0 && ", "} - + /{c.username} diff --git a/src/web/screens/detail/ContentTab.tsx b/src/web/screens/detail/ContentTab.tsx index d1c70d7..352145b 100644 --- a/src/web/screens/detail/ContentTab.tsx +++ b/src/web/screens/detail/ContentTab.tsx @@ -26,6 +26,13 @@ function hostnameOf(url: string | null | undefined): string { } } +// Groups links by namespace, with the page's own namespace first (admins can link across namespaces). +function groupByNamespace(links: PageSummary[], first: string): [string, PageSummary[]][] { + const groups = new Map([[first, []]]); + for (const link of links) groups.set(link.namespace, [...(groups.get(link.namespace) ?? []), link]); + return [...groups].filter(([, l]) => l.length > 0); +} + function BlockDialog({ editing, page, @@ -166,10 +173,14 @@ function BlockDialog({ }} > - {targets.map((p) => ( - + {groupByNamespace(targets, page.namespace).map(([ns, links]) => ( + + {links.map((p) => ( + + ))} + ))} )} diff --git a/src/web/screens/detail/DetailScreen.tsx b/src/web/screens/detail/DetailScreen.tsx index f4f89b4..6e78cf3 100644 --- a/src/web/screens/detail/DetailScreen.tsx +++ b/src/web/screens/detail/DetailScreen.tsx @@ -5,7 +5,7 @@ import { useApi } from "../../api.js"; import { buttonClass } from "../../components/ui/button.js"; import { CopyButton, DomainIcon, Link, publicUrl, Tabs } from "../../components/ui/misc.js"; import { Badge, EmptyState, Skeleton } from "../../components/ui/surface.js"; -import { detailPath, listPath } from "../../router.js"; +import { backPath, detailPath, listPath, replacePath } from "../../router.js"; import { AnalyticsTab } from "./AnalyticsTab.js"; import { AppearanceTab } from "./AppearanceTab.js"; import { ContentTab } from "./ContentTab.js"; @@ -30,10 +30,13 @@ export function DetailScreen({ id, kind, tab, me }: { id: number; kind: PageKind const page = useApi(`/pages/${id}`); const p = page.data; - // Links like /~/links/ are corrected to the right section. + // Correct the URL to the canonical one, e.g. /~/links/, or another user's + // page opened outside Administration. useEffect(() => { - if (p && p.kind !== kind) history.replaceState(null, "", detailPath(p, tab)); - }, [p, kind, tab]); + if (!p) return; + const canonical = detailPath(p, tab); + if (location.pathname.replace(/\/+$/, "") !== canonical) replacePath(canonical); + }, [p, tab]); if (page.error) { return ( @@ -67,9 +70,21 @@ export function DetailScreen({ id, kind, tab, me }: { id: number; kind: PageKind return ( <> diff --git a/src/web/screens/detail/SettingsTab.tsx b/src/web/screens/detail/SettingsTab.tsx index adefdbc..191d923 100644 --- a/src/web/screens/detail/SettingsTab.tsx +++ b/src/web/screens/detail/SettingsTab.tsx @@ -8,7 +8,7 @@ import { Field, Input, PrefixInput } from "../../components/ui/form.js"; import { CopyButton, publicUrl } from "../../components/ui/misc.js"; import { Alert, Card, CardBody, CardFooter, CardHeader } from "../../components/ui/surface.js"; import { useAction, useToast } from "../../components/ui/toast.js"; -import { listPath, navigate } from "../../router.js"; +import { backPath, navigate } from "../../router.js"; function DestinationCard({ page, onChange }: { page: PageDetail; onChange: (page: PageDetail) => void }) { const [dest, setDest] = useState(page.dest ?? ""); @@ -153,7 +153,7 @@ export function SettingsTab({ if (!ok) return; void run(async () => { await api("DELETE", `/pages/${page.id}`); - navigate(listPath(page.kind)); + navigate(backPath(page)); }, `Deleted ${page.path}`); };