color, center, canceled, labeled, recognize and the rest, in UI text, help text, comments, docs and names (ColorInput, QrCenter). Migration 053 renames the two Site settings color CHECKs, whose names are the messages the admin shows. Kept: events.status's stored value 'cancelled' (shown as canceled), aria-labelledby, schema.org's EventCancelled, and old migrations as they ran. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
231 lines
8.8 KiB
TypeScript
231 lines
8.8 KiB
TypeScript
/* ═══════════════════════════════════════════════════════════════
|
|
ADMIN LAYOUT
|
|
|
|
Bare on purpose. No PageShell, no announcement banner, no
|
|
footer site map. None of that belongs around a staff tool, and
|
|
/admin should never appear in navConfig.
|
|
|
|
Three areas share this chrome: Home, the CMS and the Panel. The
|
|
wordmark names whichever one you're in, and from anywhere but
|
|
Home it's the way back to Home.
|
|
|
|
The group menus (Data, Pages, Forms, Tools, Panel: adminNav.ts)
|
|
are drawn everywhere except Home, where the card grid is the
|
|
navigation and drawing both would say the same thing twice. Each
|
|
group's name opens its first item and hovering it lists the rest;
|
|
the strip under the header lists the open group's items, which is
|
|
also the way around on a touch screen. Which groups appear depends
|
|
on the signed-in user (navFor() drops the superadmin-only ones),
|
|
but that's cosmetics.
|
|
The route guard and the API are what actually say no.
|
|
═══════════════════════════════════════════════════════════════ */
|
|
|
|
import { CaretDown } from "@phosphor-icons/react";
|
|
import { Suspense, useCallback, useEffect, useMemo, useState } from "react";
|
|
import { Link, NavLink, Outlet, useLocation, useNavigate } from "react-router-dom";
|
|
import { useAuth } from "../../lib/auth.tsx";
|
|
import { AdminTitleContext } from "../../lib/adminTitle.tsx";
|
|
import { AdminWidthContext } from "../../lib/adminWidth.tsx";
|
|
import { SITE_VERSION } from "../../lib/version.ts";
|
|
import { ROLE_LABELS, isSuper } from "../../lib/roles.ts";
|
|
import siteLogo from "../../assets/logos/NGU_Logo.svg";
|
|
import ThemeToggle from "../../components/ThemeToggle.tsx";
|
|
import {
|
|
ADMIN_HOME,
|
|
AREA_TITLES,
|
|
areaFor,
|
|
matches,
|
|
navFor,
|
|
target,
|
|
} from "./adminNav.ts";
|
|
|
|
/* One group in the header. The name opens the group's first item;
|
|
hovering it, or tabbing into it, drops down the rest. The menu is
|
|
CSS only (group-hover, group-focus-within), so it can't get stuck
|
|
open, and a keyboard reaches every item by tabbing on from the
|
|
name. A group of one has nothing to drop down. */
|
|
function GroupMenu({ item, lit, pathname }) {
|
|
const children = item.children ?? [];
|
|
const link = lit
|
|
? "font-semibold text-link"
|
|
: "text-muted transition-colors hover:text-link";
|
|
|
|
return (
|
|
<div className="group relative">
|
|
<NavLink to={target(item)} className={`flex items-center gap-1 ${link}`}>
|
|
{item.label}
|
|
{children.length > 1 && (
|
|
<CaretDown aria-hidden="true" weight="bold" className="h-2.5 w-2.5 opacity-60" />
|
|
)}
|
|
</NavLink>
|
|
|
|
{children.length > 1 && (
|
|
<div className="invisible absolute left-0 top-full z-30 pt-2 opacity-0 transition-opacity group-focus-within:visible group-focus-within:opacity-100 group-hover:visible group-hover:opacity-100">
|
|
<ul className="min-w-52 rounded-xl border border-brand/20 bg-surface p-1.5 shadow-lg">
|
|
{children.map((child) => (
|
|
<li key={child.to}>
|
|
<NavLink
|
|
to={child.to}
|
|
className={`block rounded-lg px-3 py-2 text-sm transition-colors hover:bg-tint ${
|
|
matches(pathname, child.to)
|
|
? "font-semibold text-link"
|
|
: "text-body"
|
|
}`}
|
|
>
|
|
{child.label}
|
|
</NavLink>
|
|
</li>
|
|
))}
|
|
</ul>
|
|
</div>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
export default function AdminLayout() {
|
|
const { user, logout } = useAuth();
|
|
const navigate = useNavigate();
|
|
const { pathname } = useLocation();
|
|
|
|
const area = areaFor(pathname);
|
|
const areaTitle = AREA_TITLES[area];
|
|
const isHome = area === "home";
|
|
|
|
const nav = useMemo(() => navFor(user), [user]);
|
|
|
|
const active = nav.find(
|
|
(item) =>
|
|
matches(pathname, item.to) ||
|
|
(item.children ?? []).some((child) => matches(pathname, child.to)),
|
|
);
|
|
|
|
const activeChild = (active?.children ?? []).find((child) =>
|
|
matches(pathname, child.to),
|
|
);
|
|
|
|
// What the page below has published about itself: a record
|
|
// name, or null on a list. setDetail is stable so publishing
|
|
// can't loop.
|
|
const [detail, setDetail] = useState<any>(null);
|
|
const stableSet = useCallback((value) => setDetail(value), []);
|
|
const titleContext = useMemo(() => ({ setDetail: stableSet }), [stableSet]);
|
|
|
|
// Whether the page below asked for extra room (the record editor,
|
|
// for its section nav). setWide is a state setter, so it's stable.
|
|
const [wide, setWide] = useState(false);
|
|
|
|
useEffect(() => {
|
|
// Sections only exist in the CMS. Home and Panel already say
|
|
// what they are in the area title; "Panel | NGU Admin Panel"
|
|
// would just stutter.
|
|
const section = area === "cms" ? activeChild?.label ?? active?.label : null;
|
|
document.title = [detail, section, areaTitle].filter(Boolean).join(" | ");
|
|
}, [area, areaTitle, active, activeChild, detail]);
|
|
|
|
async function handleLogout() {
|
|
await logout();
|
|
navigate("/admin/login", { replace: true });
|
|
}
|
|
|
|
const subnav = active?.children ?? [];
|
|
|
|
const wordmark = <span className="text-lg font-bold text-link">{areaTitle}</span>;
|
|
|
|
return (
|
|
<div className="flex min-h-screen flex-col bg-tint-soft">
|
|
<header className="border-b border-brand/20 bg-surface">
|
|
<div className="mx-auto flex max-w-5xl flex-wrap items-center gap-x-8 gap-y-3 px-6 py-4">
|
|
{/* Already home, so nothing to link to. */}
|
|
{isHome ? (
|
|
wordmark
|
|
) : (
|
|
<Link
|
|
to={ADMIN_HOME}
|
|
className="rounded transition-opacity hover:opacity-70"
|
|
title="Back to the admin home"
|
|
>
|
|
{wordmark}
|
|
</Link>
|
|
)}
|
|
|
|
{!isHome && (
|
|
<nav className="flex items-center gap-6 text-sm">
|
|
{nav.map((item) => (
|
|
<div key={item.label} className="flex items-center gap-6">
|
|
{item.separated && (
|
|
<span
|
|
aria-hidden="true"
|
|
className="h-4 w-px bg-muted/25"
|
|
/>
|
|
)}
|
|
<GroupMenu item={item} lit={item === active} pathname={pathname} />
|
|
</div>
|
|
))}
|
|
</nav>
|
|
)}
|
|
|
|
<div className="ml-auto flex items-center gap-4 text-sm text-muted">
|
|
<ThemeToggle className="hover:bg-tint hover:text-link" />
|
|
<span>
|
|
{user?.name || user?.email}
|
|
</span>
|
|
<button
|
|
type="button"
|
|
onClick={handleLogout}
|
|
className="rounded-full border border-muted/30 px-4 py-1.5 font-medium transition-colors hover:bg-tint hover:text-link"
|
|
>
|
|
Sign out
|
|
</button>
|
|
</div>
|
|
</div>
|
|
|
|
{/* Subnav. Only drawn where there is something to draw, so
|
|
Events and People don't get an empty gray strip. */}
|
|
{subnav.length > 0 && (
|
|
<div className="border-t border-brand/10 bg-tint-soft">
|
|
<div className="mx-auto flex max-w-5xl flex-wrap gap-6 px-6 py-2.5 text-sm">
|
|
{subnav.map((child) => (
|
|
<NavLink
|
|
key={child.to}
|
|
to={child.to}
|
|
className={
|
|
child === activeChild
|
|
? "font-semibold text-link"
|
|
: "text-muted transition-colors hover:text-link"
|
|
}
|
|
>
|
|
{child.label}
|
|
</NavLink>
|
|
))}
|
|
</div>
|
|
</div>
|
|
)}
|
|
</header>
|
|
|
|
{/* flex-1 rather than a fixed height: the footer sits at the
|
|
bottom of a short page and below the content of a long one,
|
|
without ever floating over it. */}
|
|
<main className={`mx-auto w-full flex-1 px-6 py-10 ${wide ? "max-w-[82rem]" : "max-w-5xl"}`}>
|
|
<AdminTitleContext.Provider value={titleContext}>
|
|
<AdminWidthContext.Provider value={setWide}>
|
|
<Suspense fallback={null}>
|
|
<Outlet />
|
|
</Suspense>
|
|
</AdminWidthContext.Provider>
|
|
</AdminTitleContext.Provider>
|
|
</main>
|
|
|
|
<footer className="border-t border-brand/15 bg-surface">
|
|
<div className="mx-auto flex max-w-5xl items-center gap-4 px-6 py-3">
|
|
<Link to={ADMIN_HOME} className="transition-opacity hover:opacity-70">
|
|
<img src={siteLogo} alt="NGU" className="h-6 w-auto" />
|
|
</Link>
|
|
<span className="ml-auto font-mono text-xs text-muted/70">
|
|
{SITE_VERSION}
|
|
</span>
|
|
</div>
|
|
</footer>
|
|
</div>
|
|
);
|
|
}
|