Skip to content
refs
History
86 lines · 3.2 KBRaw
1import { Links, Meta, Outlet, Scripts, ScrollRestoration, isRouteErrorResponse, useNavigate, useRouteLoaderData } from "react-router";
2import type { Route } from "./+types/root";
3import { refsContext, requestContext } from "./context";
4import { DesignSystemProvider, BOOT_SCRIPT } from "@refs/design-system";
5import { AppLink } from "./components/link";
6import { AppShell } from "./components/shell";
7import "./app.css";
8
9const FAVICON = `data:image/svg+xml,${encodeURIComponent(
10 `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><rect x="1.5" y="1.5" width="21" height="21" rx="5.5" fill="#111113"/><path d="M6.5 8h11M6.5 12h7M6.5 16h3" stroke="#fff" stroke-width="2.2" stroke-linecap="round"/><circle cx="14.6" cy="16" r="2.1" fill="#f6821f"/></svg>`,
11)}`;
12
13export const middleware: Route.MiddlewareFunction[] = [
14 async ({ request, params, context }, next) => {
15 context.set(refsContext, await context.get(requestContext).backend.refs(request, params.ns));
16 return next();
17 },
18];
19
20export function loader({ context }: Route.LoaderArgs) {
21 const viewer = context.get(refsContext)?.viewer;
22 return {
23 org: viewer?.namespace?.name ?? "",
24 namespace: viewer?.namespace?.slug ?? null,
25 namespaces: viewer?.namespaces ?? [],
26 user: viewer?.person ?? null,
27 can: viewer?.can ?? { createRepos: false, manageMembers: false, manageCiSecrets: false, manageLabels: false, useTasks: false, reviewWaitlist: false },
28 nonce: import.meta.env.DEV ? undefined : context.get(requestContext).nonce,
29 };
30}
31
32export const links: Route.LinksFunction = () => [{ rel: "icon", href: FAVICON }];
33
34export function Layout({ children }: { children: React.ReactNode }) {
35 const nonce = useRouteLoaderData<typeof loader>("root")?.nonce;
36 return (
37 <html lang="en" suppressHydrationWarning>
38 <head>
39 <meta charSet="utf-8" />
40 <meta name="viewport" content="width=device-width, initial-scale=1" />
41 <script nonce={nonce} suppressHydrationWarning dangerouslySetInnerHTML={{ __html: BOOT_SCRIPT }} />
42 <Meta />
43 <Links />
44 </head>
45 <body className="min-h-screen bg-ground text-ink">
46 {children}
47 <ScrollRestoration nonce={nonce} />
48 <Scripts nonce={nonce} />
49 </body>
50 </html>
51 );
52}
53
54function Providers({ children }: { children: React.ReactNode }) {
55 const navigate = useNavigate();
56 return (
57 <DesignSystemProvider link={AppLink} navigate={(href) => navigate(href)}>
58 {children}
59 </DesignSystemProvider>
60 );
61}
62
63export default function App() {
64 return (
65 <Providers>
66 <Outlet />
67 </Providers>
68 );
69}
70
71export function ErrorBoundary({ error }: Route.ErrorBoundaryProps) {
72 const notFound = isRouteErrorResponse(error) && error.status === 404;
73 if (!notFound) console.error(error);
74 return (
75 <Providers>
76 <AppShell>
77 <div className="mx-auto flex max-w-xl flex-col gap-2 py-16 text-center">
78 <h1 className="m-0 text-title leading-[1.2] font-semibold">{notFound ? "Page not found" : "Something went wrong"}</h1>
79 <p className="m-0 text-base text-muted">
80 {notFound ? "It doesn't exist, or you need to sign in to see it." : "The request failed. Try again in a moment."}
81 </p>
82 </div>
83 </AppShell>
84 </Providers>
85 );
86}