import Link from "next/link"; import { notFound, redirect } from "next/navigation"; import { cache } from "react"; import { NavLink } from "@/components/header-client"; import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert"; 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: "cases", href: "/console/cases", label: "Cases" }, { section: "people", href: "/console/people", label: "People" }, { section: "imports", href: "/console/imports", label: "Imports" }, { section: "blocks", href: "/console/blocks", label: "Blocks" }, { section: "audit", href: "/console/audit", label: "Audit" }, ]; /** The console's sections this person may see, and a reminder when sensitive actions need two-factor sign-in. */ export async function ConsoleNav() { const data = await loadConsole(); const sections = new Set
(data?.sections ?? ["overview"]); const needed = data?.twoFactorNeededFor ?? []; return ( <> {needed.length > 0 && ( Turn on two-factor sign-in Some of what you can do here needs it ({needed.join(", ")}).{" "} Turn it on in your account . )} ); }