import { useEffect, useLayoutEffect, useRef } from "react"; import { Link, Outlet, useLocation } from "react-router"; import { SCREEN_LABELS, firstScreen, isScreen, type Screen } from "../auth/roles"; import { useAuth } from "../auth/useAuth"; import { replayClass } from "../lib/motion"; import { useScrollRestore } from "../lib/scrollRestore"; import { canChecklists, canStampCheck, shellScreens } from "../lib/toolAccess"; import { LockScreen } from "./LockScreen"; import { RoleSwitcher } from "./RoleSwitcher"; import { TopBar } from "./TopBar"; import { VersionBanner } from "./VersionBanner"; function chipClass(s: Screen): string { if (s === "admin") return "rchip admin"; if (s === "supervisor") return "rchip supervisor"; return "rchip"; } /** Page frame: the top bar, then the routed page inside main.wrap, and the lock screen over both. */ export function Layout() { const auth = useAuth(); const { pathname } = useLocation(); const mainRef = useRef(null); const locked = auth.status === "locked"; const me = auth.status === "ready" || locked ? auth.me : null; const screens = me ? shellScreens(me.screens, me.roles) : []; const seg = pathname.split("/")[1]; const active = isScreen(seg) && screens.includes(seg) ? seg : null; const shown = active ?? firstScreen(screens); // Back lands on the same spot; a new page starts at the top. useScrollRestore(); // A new screen eases in (the old m-enter), only while body.motion is set; the CSS keys off both. const lastPath = useRef(null); useLayoutEffect(() => { const was = lastPath.current; lastPath.current = pathname; if (was === null || was === pathname) return; return replayClass(mainRef.current, "m-enter", 700); }, [pathname]); // Keep the page mounted behind the lock, but out of reach of the keyboard and screen readers. useEffect(() => { mainRef.current?.toggleAttribute("inert", locked); }, [locked]); return ( <> : null} banner={}> {me ? ( <> {me.user.display_name} {shown ? {SCREEN_LABELS[shown]} : null} {canChecklists(me.roles) ? ( Checklists ) : null} {canStampCheck(me.roles) ? ( Stamp check ) : null} Account ) : ( Rebuild preview )}
{locked ? : null} ); }