/* ═══════════════════════════════════════════════════════════════
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 (
);
}
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(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);
// The header, its strip and the footer line up with the content,
// however wide the page asked for.
const column = wide ? "max-w-[82rem]" : "max-w-5xl";
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 = {areaTitle};
return (
{/* Already home, so nothing to link to. */}
{isHome ? (
wordmark
) : (
{wordmark}
)}
{!isHome && (
)}
{user?.name || user?.email}
{/* Subnav. Only drawn where there is something to draw, so
Events and People don't get an empty gray strip. */}
{subnav.length > 0 && (
{subnav.map((child) => (
{child.label}
))}
)}
{/* 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. */}