Repository Public
1"use client";2import Link from "next/link";3import { usePathname } from "next/navigation";4import { useEffect, type ReactNode } from "react";5import { useI18n, type TKey } from "@/i18n";6import { useSession } from "@/shell/session";7import { ForbiddenState } from "@/ui";89export const SECTIONS: readonly { id: string; href: string; key: TKey; permission?: string }[] = [10 { id: "profile", href: "/settings", key: "settings.nav.profile" },11 { id: "security", href: "/settings/security", key: "settings.nav.security" },12 { id: "users", href: "/settings/users", key: "settings.nav.users", permission: "users.manage" },13 { id: "invitations", href: "/settings/invitations", key: "settings.nav.invitations", permission: "invitations.manage" },14 { id: "grants", href: "/settings/grants", key: "settings.nav.grants", permission: "users.manage" },15 { id: "agents", href: "/settings/agents", key: "settings.nav.agents", permission: "agents.manage" },16 { id: "system", href: "/settings/system", key: "settings.nav.system", permission: "settings.manage" },17];1819/** Settings layout: h1, sub-navigation limited to the sections the user may open, then the section. */20export function SettingsFrame({ children }: { children: ReactNode }) {21 const { t } = useI18n();22 const { can } = useSession();23 const pathname = usePathname();24 const current = SECTIONS.find((s) => s.href === pathname) ?? SECTIONS[0];25 const title = t("settings.title");26 const sectionTitle = current ? t(current.key) : title;27 useEffect(() => {28 document.title = `${sectionTitle} · ${title} · MacLustr Command Center`;29 }, [sectionTitle, title]);30 return (31 <div className="page">32 <div className="page-head"><div><h1>{title}</h1><div className="sub">{t("settings.sub")}</div></div></div>33 <nav className="mcc-tabs" aria-label={t("settings.nav.label")}>34 {SECTIONS.filter((s) => !s.permission || can(s.permission)).map((s) => (35 <Link key={s.id} href={s.href} className="mcc-tab" aria-current={pathname === s.href ? "page" : undefined} data-testid={`settings-nav-${s.id}`}>{t(s.key)}</Link>36 ))}37 </nav>38 {children}39 </div>40 );41}4243/** Section gate: an admin route opened without the permission shows the 403 state naming it. */44export function Gate({ permission, children }: { permission: string; children: ReactNode }) {45 const { can } = useSession();46 return can(permission) ? <>{children}</> : <ForbiddenState permission={permission} />;47}48