2026-10-18 15:08:34 -06:00
|
|
|
import { useEffect, useLayoutEffect, useRef } from "react";
|
2026-10-16 07:40:29 -06:00
|
|
|
import { Link, Outlet, useLocation } from "react-router";
|
|
|
|
|
import { SCREEN_LABELS, firstScreen, isScreen, orderScreens, type Screen } from "../auth/roles";
|
|
|
|
|
import { useAuth } from "../auth/useAuth";
|
2026-10-18 15:08:34 -06:00
|
|
|
import { replayClass } from "../lib/motion";
|
|
|
|
|
import { useScrollRestore } from "../lib/scrollRestore";
|
2026-10-16 07:40:29 -06:00
|
|
|
import { LockScreen } from "./LockScreen";
|
|
|
|
|
import { RoleSwitcher } from "./RoleSwitcher";
|
2026-10-15 23:08:47 -06:00
|
|
|
import { TopBar } from "./TopBar";
|
|
|
|
|
|
2026-10-16 07:40:29 -06:00
|
|
|
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. */
|
2026-10-15 23:08:47 -06:00
|
|
|
export function Layout() {
|
2026-10-16 07:40:29 -06:00
|
|
|
const auth = useAuth();
|
|
|
|
|
const { pathname } = useLocation();
|
|
|
|
|
const mainRef = useRef<HTMLElement>(null);
|
|
|
|
|
const locked = auth.status === "locked";
|
|
|
|
|
const me = auth.status === "ready" || locked ? auth.me : null;
|
|
|
|
|
|
|
|
|
|
const screens = me ? orderScreens(me.screens) : [];
|
|
|
|
|
const seg = pathname.split("/")[1];
|
|
|
|
|
const active = isScreen(seg) && screens.includes(seg) ? seg : null;
|
|
|
|
|
const shown = active ?? firstScreen(screens);
|
|
|
|
|
|
2026-10-18 15:08:34 -06:00
|
|
|
// 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<string | null>(null);
|
|
|
|
|
useLayoutEffect(() => {
|
|
|
|
|
const was = lastPath.current;
|
|
|
|
|
lastPath.current = pathname;
|
|
|
|
|
if (was === null || was === pathname) return;
|
|
|
|
|
return replayClass(mainRef.current, "m-enter", 700);
|
|
|
|
|
}, [pathname]);
|
|
|
|
|
|
2026-10-16 07:40:29 -06:00
|
|
|
// Keep the page mounted behind the lock, but out of reach of the keyboard and screen readers.
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
mainRef.current?.toggleAttribute("inert", locked);
|
|
|
|
|
}, [locked]);
|
|
|
|
|
|
2026-10-15 23:08:47 -06:00
|
|
|
return (
|
|
|
|
|
<>
|
2026-10-16 07:40:29 -06:00
|
|
|
<TopBar nav={me && !locked ? <RoleSwitcher screens={screens} active={active} /> : null}>
|
|
|
|
|
{me ? (
|
|
|
|
|
<>
|
|
|
|
|
<b className="uname">{me.user.display_name}</b>
|
|
|
|
|
{shown ? <span className={chipClass(shown)}>{SCREEN_LABELS[shown]}</span> : null}
|
|
|
|
|
<Link className="btn sm ghost" to="/account">
|
|
|
|
|
Account
|
|
|
|
|
</Link>
|
|
|
|
|
<button className="btn sm ghost" type="button" onClick={() => void auth.signOut()}>
|
|
|
|
|
Sign out
|
|
|
|
|
</button>
|
|
|
|
|
</>
|
|
|
|
|
) : (
|
|
|
|
|
<span className="proto">Rebuild preview</span>
|
|
|
|
|
)}
|
2026-10-15 23:08:47 -06:00
|
|
|
</TopBar>
|
2026-10-16 07:40:29 -06:00
|
|
|
<div className={locked ? "blur-hidden" : undefined}>
|
|
|
|
|
<main className="wrap" id="main" ref={mainRef} aria-hidden={locked ? true : undefined}>
|
|
|
|
|
<Outlet />
|
|
|
|
|
</main>
|
|
|
|
|
</div>
|
|
|
|
|
{locked ? <LockScreen key={auth.me?.user.id ?? "unknown"} /> : null}
|
2026-10-15 23:08:47 -06:00
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
}
|