import { Link, Navigate, useLocation } from "@tanstack/solid-router"; import { type ParentProps, Show } from "solid-js"; import type { Role } from "@/api/client"; import { PageLoadError } from "@/components/layout/page-load-error"; import { IconHome, IconLock } from "@/components/ui/icons"; import { PageSpinner } from "@/components/ui/page-spinner"; import { hasExactRole, hasMinRole, roleInRange } from "@/lib/roles"; import { useAuth } from "@/stores/auth-context"; import { useT } from "@/stores/preferences-context"; // Pages kept for one audience that staff reach by URL or an old link: where // the school-wide view of the same data exists, point there instead. const STAFF_ALTERNATIVES: Record = { "/marks": { to: "/management/student-marks", minRole: "teacher" }, "/pomodoro": { to: "/management/pomodoros", minRole: "teacher" }, "/students": { to: "/management/students", minRole: "manager" }, "/students/attendance": { to: "/management/student-attendance", minRole: "teacher" }, "/students/exams": { to: "/management/student-marks", minRole: "teacher" }, }; function AccessDeniedCard(props: { exactRole?: Role; userRole: Role }) { const t = useT(); const location = useLocation(); // Asking "the administrator" for access reads wrong to staff — often they // are the administrator. For a page kept for students or parents, say so. const audience = () => props.exactRole && props.exactRole !== props.userRole && hasMinRole(props.userRole, "teacher") ? props.exactRole : null; const alternative = () => { if (!audience()) return null; const alt = STAFF_ALTERNATIVES[location().pathname.replace(/\/+$/, "")]; return alt && hasMinRole(props.userRole, alt.minRole) ? alt.to : null; }; return (

403

{t("common.accessDenied")}

{t("errors.accessDeniedDescription")}

} > {(role) => (

{t(role() === "parent" ? "errors.parentOnlyTitle" : "errors.studentOnlyTitle")}

{t(alternative() ? "errors.audienceOnlyWithAlternative" : "errors.audienceOnlyDescription")}

)}
{(to) => ( {t("errors.openStaffView")} )} {t("common.goHome")}
); } export function RouteGuard( props: ParentProps<{ minRole?: Role; /** Inclusive upper bound (e.g. maxRole: "manager" excludes admin). */ maxRole?: Role; /** When set, only this exact role may enter (e.g. student-only pages). */ exactRole?: Role; }>, ) { const auth = useAuth(); const allowed = (role: Role) => { if (props.exactRole) return hasExactRole(role, props.exactRole); if (props.minRole || props.maxRole) return roleInRange(role, props.minRole, props.maxRole); return true; }; return ( }> {/* A failed session read (often a rate limit) gets the same full-page state as a failed page: the cause, a retry that waits out the limit, and the details — not a bare red box. */} void auth.refresh()} />}> }> {(u) => ( }> {props.children} )} ); }