import { notFound, redirect } from "next/navigation"; import { cache } from "react"; import { NavLink } from "@/components/header-client"; import { api, getMe, isModerator, unwrap, type ConsoleData, type Person } from "@/lib/api"; /** The signed-in moderator, or off to sign in; others get not found (these pages aren't for them). */ export async function requireModerator(path: string): Promise { const me = await getMe(); if (!me) redirect(`/signin?next=${encodeURIComponent(path)}`); if (!isModerator(me)) notFound(); return me; } /** The console as this person sees it (once per request); undefined if it isn't for them. */ const loadConsole = cache(async (): Promise => { const answer = await api.console.get(); if (answer.error) return undefined; return unwrap(answer); }); /** * The signed-in person and their console, or off to sign in. People who manage nothing get not found: the console * is for those controlling a product, catalog or room. */ export async function requireConsole(path: string): Promise<{ me: Person; console: ConsoleData }> { const me = await getMe(); if (!me) redirect(`/signin?next=${encodeURIComponent(path)}`); const data = await loadConsole(); if (!data) notFound(); return { me, console: data }; } type Section = ConsoleData["sections"][number]; // Settings are on the overview. const tabs: { section: Section; href: string; label: string }[] = [ { section: "overview", href: "/console", label: "Overview" }, { section: "products", href: "/console/products", label: "Products" }, { section: "catalogs", href: "/console/catalogs", label: "Catalogs" }, { section: "people", href: "/console/people", label: "People" }, { section: "audit", href: "/console/audit", label: "Audit" }, ]; /** The console's sections this person may see. */ export async function ConsoleNav() { const sections = new Set
((await loadConsole())?.sections ?? ["overview"]); return ( ); }