86 lines · 3.4 KBRaw
| 1 | import { Links, Meta, Outlet, Scripts, ScrollRestoration, isRouteErrorResponse, useNavigate, useRouteLoaderData } from "react-router"; |
| 2 | import type { Route } from "./+types/root"; |
| 3 | import { refsContext, requestContext } from "./context"; |
| 4 | import { DesignSystemProvider, BOOT_SCRIPT } from "@refs/design-system"; |
| 5 | import { AppLink } from "./components/link"; |
| 6 | import { AppShell } from "./components/shell"; |
| 7 | import "./app.css"; |
| 8 | |
| 9 | const FAVICON = `data:image/svg+xml,${encodeURIComponent( |
| 10 | `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><style>g{stroke:#18181b}@media (prefers-color-scheme:dark){g{stroke:#f2f2f3}}</style><g fill="none" stroke-linecap="round"><circle cx="7" cy="3.6" r="1.9" stroke-width="1.6"/><circle cx="17" cy="3.6" r="1.9" stroke-width="1.6"/><path d="M7 6.3C7 12.25 12 10.75 12 16M17 6.3C17 12.25 12 10.75 12 16" stroke-width="2"/></g><circle cx="12" cy="20.4" r="2.3" fill="#f6821f"/></svg>`, |
| 11 | )}`; |
| 12 | |
| 13 | export 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 | |
| 20 | export 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, viewAdmin: false }, |
| 28 | nonce: import.meta.env.DEV ? undefined : context.get(requestContext).nonce, |
| 29 | }; |
| 30 | } |
| 31 | |
| 32 | export const links: Route.LinksFunction = () => [{ rel: "icon", href: FAVICON }]; |
| 33 | |
| 34 | export 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 | |
| 54 | function 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 | |
| 63 | export default function App() { |
| 64 | return ( |
| 65 | <Providers> |
| 66 | <Outlet /> |
| 67 | </Providers> |
| 68 | ); |
| 69 | } |
| 70 | |
| 71 | export 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 | } |