g1t/apps/sudo/app/root.tsx

80 lines3,239 bytesCodeBlame
1import { isRouteErrorResponse, Links, Meta, Outlet, useRouteLoaderData } from "react-router";
2
3import type { Route } from "./+types/root";
4import "./app.css";
5import displayFont from "@g1t/theme/fonts/bricolage-grotesque-latin.woff2?url";
6import sansFont from "@g1t/theme/fonts/hanken-grotesk-latin.woff2?url";
7import { MobileBar, Sidebar } from "./components/shell";
8import { ButtonLink } from "./components/ui";
9import type { NavCounts } from "./lib/nav";
10import { identity, statusAdmin } from "./lib/services.server";
11import { settle } from "./lib/settle";
12import { requireStaff } from "./lib/staff";
13
14export const links: Route.LinksFunction = () => [
15 { rel: "icon", type: "image/svg+xml", href: "/favicon.svg" },
16 // The faces are served by sudo itself (@g1t/theme); these two are on every page.
17 { rel: "preload", href: sansFont, as: "font", type: "font/woff2", crossOrigin: "anonymous" },
18 { rel: "preload", href: displayFont, as: "font", type: "font/woff2", crossOrigin: "anonymous" },
19];
20
21export const meta: Route.MetaFunction = () => [
22 { title: "sudo · g1t" },
23 { name: "robots", content: "noindex, nofollow" },
24];
25
26export async function loader({ context }: Route.LoaderArgs) {
27 const { email } = requireStaff(context);
28 // The sidebar's counts: a service that does not answer shows none.
29 const [waitlist, incidents] = await Promise.all([settle(identity.waitlistPending()), settle(statusAdmin.openCount())]);
30 const counts: NavCounts = { waitlist: waitlist.ok ? waitlist.value : 0, incidents: incidents.ok ? incidents.value : 0 };
31 return { email, counts };
32}
33
34export function Layout({ children }: { children: React.ReactNode }) {
35 const root = useRouteLoaderData<typeof loader>("root");
36 return (
37 <html lang="en">
38 <head>
39 <meta charSet="utf-8" />
40 <meta name="viewport" content="width=device-width, initial-scale=1" />
41 <meta name="theme-color" content="#0f0f11" />
42 <Meta />
43 <Links />
44 </head>
45 <body className="min-h-screen">
46 <Sidebar email={root?.email} counts={root?.counts} />
47 <MobileBar email={root?.email} counts={root?.counts} />
48 <div className="lg:pl-60">{children}</div>
49 {/* No <Scripts />: sudo ships no JavaScript, and its policy allows none. */}
50 </body>
51 </html>
52 );
53}
54
55export default function App() {
56 return <Outlet />;
57}
58
59export function ErrorBoundary({ error }: Route.ErrorBoundaryProps) {
60 let title = "Something went wrong";
61 let details = "An unexpected error occurred.";
62 if (isRouteErrorResponse(error)) {
63 title = error.status === 404 ? "Not found" : `Error ${error.status}`;
64 details = typeof error.data === "string" && error.data ? error.data : error.statusText || details;
65 } else if (error instanceof Error) {
66 // Staff only: the real reason helps more than a polite one.
67 details = error.message;
68 }
69 return (
70 <main className="mx-auto max-w-xl px-4 py-24 text-center">
71 <h1 className="text-2xl font-semibold tracking-tight">{title}</h1>
72 <p className="mt-3 break-words text-muted">{details}</p>
73 <div className="mt-8">
74 <ButtonLink to="/" variant="quiet">
75 Back to the overview
76 </ButtonLink>
77 </div>
78 </main>
79 );
80}