| 1 | import { |
| 2 | BookOpen, |
| 3 | ChevronDown, |
| 4 | Compass, |
| 5 | CreditCard, |
| 6 | LayoutDashboard, |
| 7 | LogIn, |
| 8 | LogOut, |
| 9 | Menu, |
| 10 | Plus, |
| 11 | Search, |
| 12 | Settings, |
| 13 | } from "lucide-react"; |
| 14 | import { useEffect, useState } from "react"; |
| 15 | import { |
| 16 | Form, |
| 17 | isRouteErrorResponse, |
| 18 | Link, |
| 19 | Links, |
| 20 | Meta, |
| 21 | NavLink, |
| 22 | Outlet, |
| 23 | Scripts, |
| 24 | ScrollRestoration, |
| 25 | type ShouldRevalidateFunctionArgs, |
| 26 | useLocation, |
| 27 | useMatches, |
| 28 | useParams, |
| 29 | useRouteError, |
| 30 | useRouteLoaderData, |
| 31 | useSubmit, |
| 32 | } from "react-router"; |
| 33 | |
| 34 | import { type User, hasAccessIn, sharedWorkspaces } from "@g1t/contracts"; |
| 35 | |
| 36 | import type { Route } from "./+types/root"; |
| 37 | import appCss from "./app.css?url"; |
| 38 | import displayFont from "@g1t/theme/fonts/bricolage-grotesque-latin.woff2?url"; |
| 39 | import sansFont from "@g1t/theme/fonts/hanken-grotesk-latin.woff2?url"; |
| 40 | import { Logo } from "./components/logo"; |
| 41 | import { Avatar, ButtonLink, SubmitButton, notACredential } from "./components/ui"; |
| 42 | import { |
| 43 | DropdownMenu, |
| 44 | DropdownMenuContent, |
| 45 | DropdownMenuItem, |
| 46 | DropdownMenuLabel, |
| 47 | DropdownMenuSeparator, |
| 48 | DropdownMenuTrigger, |
| 49 | } from "./components/ui/dropdown-menu"; |
| 50 | import { AppShell, Progress, type ShellData, useLeaving } from "./components/shell"; |
| 51 | import { SiteFooter } from "./components/footer"; |
| 52 | import { SpikeBanner } from "./components/spike-banner"; |
| 53 | import { readCookie } from "./lib/mission"; |
| 54 | import { WORKSPACE_COOKIE, workspaceFor } from "./lib/workspace-choice"; |
| 55 | import { NotFound } from "./components/not-found"; |
| 56 | import { usesAppShell } from "./lib/chrome"; |
| 57 | import { CommandPalette, type PaletteCommand, PaletteKey, usePaletteShortcut } from "./components/command-palette"; |
| 58 | import { billing, inbox, projects } from "./lib/services.server"; |
| 59 | import { countsFor, readableRepos } from "./lib/access.server"; |
| 60 | import { shortCache } from "./lib/cache.server"; |
| 61 | import { getViewer, viewerMiddleware } from "./lib/session.server"; |
| 62 | import { shortcutOf, workspaceProjects } from "./lib/workspace-projects.server"; |
| 63 | import { registrationMode } from "./lib/registration.server"; |
| 64 | import { addresses } from "./lib/addresses.server"; |
| 65 | import { useSignUpCopy } from "./lib/registration"; |
| 66 | import { RELOADED_KEY, reloadFixes } from "./lib/stale-build"; |
| 67 | |
| 68 | |
| 69 | export const links: Route.LinksFunction = () => [ |
| 70 | { rel: "icon", href: "/favicon.ico", sizes: "32x32" }, |
| 71 | { rel: "icon", type: "image/svg+xml", href: "/favicon.svg" }, |
| 72 | { rel: "icon", type: "image/png", sizes: "192x192", href: "/icon-192.png" }, |
| 73 | { rel: "apple-touch-icon", href: "/apple-touch-icon.png" }, |
| 74 | // The text and headline faces are wanted on every page, so they start |
| 75 | // loading with the stylesheet; mono waits until something uses it. |
| 76 | { rel: "preload", href: sansFont, as: "font", type: "font/woff2", crossOrigin: "anonymous" }, |
| 77 | { rel: "preload", href: displayFont, as: "font", type: "font/woff2", crossOrigin: "anonymous" }, |
| 78 | ]; |
| 79 | |
| 80 | export const middleware: Route.MiddlewareFunction[] = [viewerMiddleware]; |
| 81 | |
| 82 | export async function loader({ context, params, request }: Route.LoaderArgs) { |
| 83 | const user = getViewer(context); |
| 84 | const chosen = readCookie(request.headers.get("cookie"), WORKSPACE_COOKIE); |
| 85 | // What sign-up buttons say: Request access while g1t is invite-only. |
| 86 | const [shell, mode] = await Promise.all([ |
| 87 | user |
| 88 | ? shellFor(user, params, chosen, context).catch((error: unknown) => { |
| 89 | // A sidebar that could not be read is no reason to lose the page, |
| 90 | // or to draw it as if they were signed out. |
| 91 | console.error("root: the sidebar could not be read", error); |
| 92 | return bareShell(user, params, chosen); |
| 93 | }) |
| 94 | : visitorShell(params, context), |
| 95 | user ? Promise.resolve(null) : registrationMode(), |
| 96 | ]); |
| 97 | // Where this g1t lives, for clone lines, agent setup and link previews. |
| 98 | return { user, shell, inviteOnly: mode !== "open", addresses: addresses() }; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * The sidebar for someone not signed in: nothing of their own, only the |
| 103 | * open counts of the project being looked at. One call, and only in a |
| 104 | * project; it answers the same for a private project as a missing one. |
| 105 | */ |
| 106 | async function visitorShell(params: { owner?: string; repo?: string }, context: Route.LoaderArgs["context"]): Promise<ShellData> { |
| 107 | const path = params.owner && params.repo ? { namespace: params.owner, name: params.repo } : null; |
| 108 | const counts = path ? await countsFor(context, params) : null; |
| 109 | return { |
| 110 | workspace: null, |
| 111 | repos: [], |
| 112 | repo: path && counts?.ok ? { ...path, member: false, issues: counts.value.issues, pulls: counts.value.pulls } : null, |
| 113 | limit: null, |
| 114 | monthUsageMicros: null, |
| 115 | }; |
| 116 | } |
| 117 | |
| 118 | /** The sidebar from what the session already says, with no service asked. */ |
| 119 | function bareShell(user: User, params: { owner?: string; repo?: string }, chosen: string | null): ShellData { |
| 120 | return { |
| 121 | workspace: workspaceFor(user.workspaces ?? [], chosen, params), |
| 122 | repos: [], |
| 123 | repo: null, |
| 124 | limit: null, |
| 125 | monthUsageMicros: null, |
| 126 | }; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * The sidebar only changes with the workspace or repository being looked |
| 131 | * at, or after something was submitted: not on every page within them. |
| 132 | */ |
| 133 | export function shouldRevalidate({ |
| 134 | currentParams, |
| 135 | nextParams, |
| 136 | formMethod, |
| 137 | defaultShouldRevalidate, |
| 138 | }: ShouldRevalidateFunctionArgs) { |
| 139 | if (formMethod && formMethod !== "GET") return defaultShouldRevalidate; |
| 140 | return currentParams.owner !== nextParams.owner || currentParams.repo !== nextParams.repo; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * The sidebar: the workspace whose pages or project these are when it is |
| 145 | * one of yours, else the one you chose (lib/workspace-choice.ts); its |
| 146 | * projects; and the repository being looked at. |
| 147 | */ |
| 148 | async function shellFor( |
| 149 | user: User, |
| 150 | params: { owner?: string; repo?: string }, |
| 151 | chosen: string | null, |
| 152 | context: Route.LoaderArgs["context"], |
| 153 | ): Promise<ShellData> { |
| 154 | const memberships = user.workspaces ?? []; |
| 155 | const workspace = workspaceFor(memberships, chosen, params); |
| 156 | const path = params.owner && params.repo ? { namespace: params.owner, name: params.repo } : null; |
| 157 | const now = new Date(); |
| 158 | const monthStart = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), 1)).toISOString(); |
| 159 | // What the sidebar shows of the workspace changes rarely: kept for a few |
| 160 | // seconds per person and workspace, except just after they changed |
| 161 | // something (lib/cache.server.ts). |
| 162 | const kept = <T,>(what: string, load: () => Promise<T>) => |
| 163 | workspace ? shortCache(`shell:${what}:${user.id}:${workspace.slug}`, SHELL_TTL_MS, load) : Promise.resolve(null); |
| 164 | const [listed, counts, status, usage, limit, entitlements, shared, unread, shortcuts] = await Promise.all([ |
| 165 | // Every project, for the palette and the count; the sidebar lists only |
| 166 | // the person's pinned and recent ones (lib/pins.ts). |
| 167 | workspace ? workspaceProjects(workspace.slug, user) : Promise.resolve(null), |
| 168 | path ? countsFor(context, params) : Promise.resolve(null), |
| 169 | // Whether billing is on, without reading the account: that asks the |
| 170 | // card processor about the workspace's cards, too slow for every page. |
| 171 | kept("status", () => billing.status()).catch(() => null), |
| 172 | kept(`usage:${monthStart}`, () => billing.usage(workspace!.slug, user, monthStart)).catch(() => null), |
| 173 | kept("limit", () => billing.limit(workspace!.slug, user)).catch(() => null), |
| 174 | kept("entitlements", () => billing.entitlements(workspace!.slug)).catch(() => null), |
| 175 | sharedRepos(user), |
| 176 | // The bell's count: one query, never kept, so marking an item shows at once. |
| 177 | inbox.counts(user.username).catch(() => null), |
| 178 | // Never kept: opening a project moves it up Recent. |
| 179 | workspace ? projects.shortcuts(workspace.slug, user).catch(() => null) : Promise.resolve(null), |
| 180 | ]); |
| 181 | return { |
| 182 | workspace, |
| 183 | // Projects are what the sidebar lists: what the workspace builds and runs. |
| 184 | repos: listed?.ok ? listed.value.map(shortcutOf) : [], |
| 185 | pinned: (shortcuts?.pinned ?? []).map(shortcutOf), |
| 186 | recent: (shortcuts?.recent ?? []).map(shortcutOf), |
| 187 | repo: |
| 188 | path && counts?.ok |
| 189 | ? { |
| 190 | ...path, |
| 191 | // Until the repository's own page says what their role is. |
| 192 | member: hasAccessIn(user, path.namespace), |
| 193 | issues: counts.value.issues, |
| 194 | pulls: counts.value.pulls, |
| 195 | } |
| 196 | : null, |
| 197 | // Where the workspace stands against its usage limit, once billing is on. |
| 198 | limit: |
| 199 | status?.enabled && limit?.ok |
| 200 | ? { |
| 201 | exposureMicros: limit.value.exposureMicros, |
| 202 | ceilingMicros: limit.value.ceilingMicros, |
| 203 | state: limit.value.state, |
| 204 | comped: limit.value.trust === "internal", |
| 205 | } |
| 206 | : null, |
| 207 | free: Boolean(status?.free), |
| 208 | // A spend spike or a hold pauses new compute: the shell says so on every page. |
| 209 | compute: |
| 210 | workspace && entitlements && (entitlements.paused || entitlements.spike?.status === "open") |
| 211 | ? { paused: entitlements.paused, spike: entitlements.spike ?? null, owner: workspace.role === "owner" } |
| 212 | : null, |
| 213 | shared, |
| 214 | inbox: unread, |
| 215 | // While g1t is free every charge is zero, so usage is shown at cost. |
| 216 | // Usage at price, the one figure every page shows. |
| 217 | monthUsageMicros: usage?.ok ? (usage.value.free ? usage.value.usedMicros : (usage.value.priceMicros ?? usage.value.spentMicros)) : null, |
| 218 | }; |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * How long the sidebar's workspace answers are kept: as long as someone's |
| 223 | * own changes read fresh anyway (lib/perf.ts `PRIMARY_WINDOW_SECONDS`). |
| 224 | */ |
| 225 | const SHELL_TTL_MS = 30_000; |
| 226 | |
| 227 | /** Most repositories listed under Shared with you. */ |
| 228 | const MAX_SHARED = 20; |
| 229 | |
| 230 | /** |
| 231 | * Repositories shared with the user in workspaces they do not belong to, |
| 232 | * by their grants: only those, never the workspace's other repositories. |
| 233 | */ |
| 234 | async function sharedRepos(user: User): Promise<ShellData["shared"]> { |
| 235 | const outside = new Set(sharedWorkspaces(user)); |
| 236 | if (outside.size === 0) return []; |
| 237 | const ids = (user.grants ?? []).filter((grant) => outside.has(grant.workspace)).map((grant) => grant.repo_id).slice(0, MAX_SHARED); |
| 238 | const found = await readableRepos(ids, user); |
| 239 | return found |
| 240 | .map((repo) => ({ namespace: repo.namespace, name: repo.name, isPrivate: repo.isPrivate })) |
| 241 | .sort((a, b) => `${a.namespace}/${a.name}`.localeCompare(`${b.namespace}/${b.name}`)); |
| 242 | } |
| 243 | |
| 244 | function HeaderLink({ to, children }: { to: string; children: React.ReactNode }) { |
| 245 | return ( |
| 246 | <NavLink |
| 247 | to={to} |
| 248 | className={({ isActive }) => |
| 249 | `rounded-md px-2.5 py-1.5 text-sm transition-colors hover:bg-raised hover:text-fg ${ |
| 250 | isActive ? "text-fg" : "text-muted" |
| 251 | }` |
| 252 | } |
| 253 | > |
| 254 | {children} |
| 255 | </NavLink> |
| 256 | ); |
| 257 | } |
| 258 | |
| 259 | /** What the palette offers someone without the app's sidebar. */ |
| 260 | const PUBLIC_COMMANDS: PaletteCommand[] = [ |
| 261 | { label: "Explore", hint: "Public projects", to: "/explore", icon: <Compass size={15} /> }, |
| 262 | { label: "Search g1t", hint: "Repositories, code, issues, people", to: "/search", icon: <Search size={15} /> }, |
| 263 | { label: "Pricing", to: "/pricing", icon: <CreditCard size={15} /> }, |
| 264 | { label: "Documentation", to: "https://docs.g1t.sh/", icon: <BookOpen size={15} /> }, |
| 265 | { label: "Sign in", to: "/login", icon: <LogIn size={15} /> }, |
| 266 | { label: "Sign up", to: "/register", icon: <Plus size={15} /> }, |
| 267 | ]; |
| 268 | |
| 269 | /** Sign up, or Request access while g1t is invite-only. */ |
| 270 | function SignUpButton() { |
| 271 | const copy = useSignUpCopy(); |
| 272 | return <ButtonLink to="/register">{copy.primary}</ButtonLink>; |
| 273 | } |
| 274 | |
| 275 | function Header({ user }: { user: User | null | undefined }) { |
| 276 | const submit = useSubmit(); |
| 277 | const signUp = useSignUpCopy(); |
| 278 | const [palette, setPalette] = useState(false); |
| 279 | usePaletteShortcut(() => setPalette((open) => !open)); |
| 280 | const params = useParams(); |
| 281 | const repo = params.owner && params.repo ? `${params.owner}/${params.repo}` : null; |
| 282 | return ( |
| 283 | <header className="sticky top-0 z-40 border-b border-line bg-surface/85 backdrop-blur"> |
| 284 | <div className="mx-auto flex h-14 max-w-6xl items-center gap-2 px-4"> |
| 285 | <Link to="/" aria-label="g1t home" className="mr-2 flex"> |
| 286 | <Logo /> |
| 287 | </Link> |
| 288 | <Form action="/search" role="search" className="relative hidden grow sm:block sm:max-w-xs"> |
| 289 | <Search |
| 290 | size={14} |
| 291 | className="pointer-events-none absolute top-1/2 left-3 -translate-y-1/2 text-faint" |
| 292 | /> |
| 293 | <input |
| 294 | name="q" |
| 295 | {...notACredential()} |
| 296 | placeholder="Search g1t" |
| 297 | aria-label="Search g1t" |
| 298 | className="w-full rounded-md border border-line bg-bg py-1.5 pr-12 pl-8 text-sm outline-none transition-colors placeholder:text-faint hover:border-line-strong focus:border-accent-dim" |
| 299 | /> |
| 300 | <PaletteKey className="pointer-events-none absolute top-1/2 right-2 -translate-y-1/2 rounded bg-raised px-1.5 font-mono text-[0.625rem] text-muted ring-1 ring-line" /> |
| 301 | </Form> |
| 302 | <button |
| 303 | type="button" |
| 304 | aria-label="Search g1t" |
| 305 | onClick={() => setPalette(true)} |
| 306 | className="flex size-10 items-center justify-center rounded-md text-muted transition-colors hover:bg-raised hover:text-fg sm:hidden" |
| 307 | > |
| 308 | <Search size={18} /> |
| 309 | </button> |
| 310 | <CommandPalette |
| 311 | open={palette} |
| 312 | onOpenChange={setPalette} |
| 313 | commands={user ? [{ label: "Mission control", to: "/", icon: <LayoutDashboard size={15} /> }, ...PUBLIC_COMMANDS.slice(0, 4)] : PUBLIC_COMMANDS.map((command) => (command.to === "/register" ? { ...command, label: signUp.primary } : command))} |
| 314 | repo={repo} |
| 315 | /> |
| 316 | <nav aria-label="Main" className="hidden items-center gap-0.5 sm:flex"> |
| 317 | <HeaderLink to="/explore">Explore</HeaderLink> |
| 318 | <HeaderLink to="/pricing">Pricing</HeaderLink> |
| 319 | <HeaderLink to="https://docs.g1t.sh/">Docs</HeaderLink> |
| 320 | </nav> |
| 321 | <div className="ml-auto flex items-center gap-2"> |
| 322 | {/* On a phone the links fold into one menu, so the bar fits. */} |
| 323 | <DropdownMenu> |
| 324 | <DropdownMenuTrigger |
| 325 | aria-label="Menu" |
| 326 | className="flex size-10 items-center justify-center rounded-md text-muted outline-none transition-colors hover:bg-raised hover:text-fg focus-visible:ring-2 focus-visible:ring-accent data-[state=open]:bg-raised data-[state=open]:text-fg sm:hidden" |
| 327 | > |
| 328 | <Menu size={18} /> |
| 329 | </DropdownMenuTrigger> |
| 330 | <DropdownMenuContent align="end" className="w-52"> |
| 331 | <DropdownMenuItem asChild className="min-h-11"> |
| 332 | <Link to="/explore"> |
| 333 | <Compass /> |
| 334 | Explore |
| 335 | </Link> |
| 336 | </DropdownMenuItem> |
| 337 | <DropdownMenuItem asChild className="min-h-11"> |
| 338 | <Link to="/pricing"> |
| 339 | <CreditCard /> |
| 340 | Pricing |
| 341 | </Link> |
| 342 | </DropdownMenuItem> |
| 343 | <DropdownMenuItem asChild className="min-h-11"> |
| 344 | <Link to="https://docs.g1t.sh/"> |
| 345 | <BookOpen /> |
| 346 | Docs |
| 347 | </Link> |
| 348 | </DropdownMenuItem> |
| 349 | {!user && ( |
| 350 | <> |
| 351 | <DropdownMenuSeparator /> |
| 352 | <DropdownMenuItem asChild className="min-h-11"> |
| 353 | <Link to="/login"> |
| 354 | <LogIn /> |
| 355 | Sign in |
| 356 | </Link> |
| 357 | </DropdownMenuItem> |
| 358 | </> |
| 359 | )} |
| 360 | </DropdownMenuContent> |
| 361 | </DropdownMenu> |
| 362 | {user ? ( |
| 363 | <> |
| 364 | <Link |
| 365 | to="/new" |
| 366 | aria-label="New project" |
| 367 | className="rounded-md border border-line p-1.5 text-muted transition-colors hover:border-line-strong hover:text-fg" |
| 368 | > |
| 369 | <Plus size={16} /> |
| 370 | </Link> |
| 371 | <DropdownMenu> |
| 372 | <DropdownMenuTrigger |
| 373 | aria-label="Account menu" |
| 374 | className="flex items-center gap-1.5 rounded-md p-1 outline-none transition-colors hover:bg-raised focus-visible:ring-2 focus-visible:ring-accent data-[state=open]:bg-raised" |
| 375 | > |
| 376 | <Avatar name={user.username} image={user.avatar} size={24} /> |
| 377 | <ChevronDown size={14} className="text-faint" /> |
| 378 | </DropdownMenuTrigger> |
| 379 | <DropdownMenuContent align="end" className="w-56"> |
| 380 | <DropdownMenuLabel> |
| 381 | Signed in as{" "} |
| 382 | <span className="font-mono font-medium text-fg"> |
| 383 | {user.username} |
| 384 | </span> |
| 385 | </DropdownMenuLabel> |
| 386 | <DropdownMenuSeparator /> |
| 387 | {(user.workspaces ?? []).map((membership) => ( |
| 388 | <DropdownMenuItem asChild key={membership.slug}> |
| 389 | <Link to={`/${membership.slug}`}> |
| 390 | <Avatar name={membership.slug} image={membership.avatar} size={16} square /> |
| 391 | <span className="min-w-0 truncate">{membership.name || membership.slug}</span> |
| 392 | </Link> |
| 393 | </DropdownMenuItem> |
| 394 | ))} |
| 395 | <DropdownMenuItem asChild> |
| 396 | <Link to="/workspaces/new"> |
| 397 | <Plus /> |
| 398 | New workspace |
| 399 | </Link> |
| 400 | </DropdownMenuItem> |
| 401 | <DropdownMenuSeparator /> |
| 402 | <DropdownMenuItem asChild> |
| 403 | <Link to="/"> |
| 404 | <LayoutDashboard /> |
| 405 | Mission control |
| 406 | </Link> |
| 407 | </DropdownMenuItem> |
| 408 | <DropdownMenuItem asChild> |
| 409 | <Link to="/new"> |
| 410 | <Plus /> |
| 411 | New project |
| 412 | </Link> |
| 413 | </DropdownMenuItem> |
| 414 | <DropdownMenuSeparator /> |
| 415 | <DropdownMenuItem asChild> |
| 416 | <Link to="/settings/profile"> |
| 417 | <Settings /> |
| 418 | Settings |
| 419 | </Link> |
| 420 | </DropdownMenuItem> |
| 421 | <DropdownMenuItem asChild> |
| 422 | <Link to="https://docs.g1t.sh/quickstart/"> |
| 423 | <BookOpen /> |
| 424 | Documentation |
| 425 | </Link> |
| 426 | </DropdownMenuItem> |
| 427 | <DropdownMenuSeparator /> |
| 428 | {/* Submitted from here: the menu closes on select, and a button |
| 429 | that has left the page cannot submit a form. */} |
| 430 | <DropdownMenuItem |
| 431 | onSelect={() => submit(null, { method: "post", action: "/logout" })} |
| 432 | > |
| 433 | <LogOut /> |
| 434 | Sign out |
| 435 | </DropdownMenuItem> |
| 436 | </DropdownMenuContent> |
| 437 | </DropdownMenu> |
| 438 | </> |
| 439 | ) : ( |
| 440 | <> |
| 441 | <span className="hidden sm:contents"> |
| 442 | <HeaderLink to="/login">Sign in</HeaderLink> |
| 443 | </span> |
| 444 | <SignUpButton /> |
| 445 | </> |
| 446 | )} |
| 447 | </div> |
| 448 | </div> |
| 449 | </header> |
| 450 | ); |
| 451 | } |
| 452 | |
| 453 | /** |
| 454 | * The root's data as the browser last had it. When a navigation fails in a |
| 455 | * way that takes the root's data with it, the error is still drawn in the |
| 456 | * person's own frame, signed in, not the visitor's. Never kept on the |
| 457 | * server, where one module serves everyone. |
| 458 | */ |
| 459 | let lastRoot: Awaited<ReturnType<typeof loader>> | undefined; |
| 460 | |
| 461 | export function Layout({ children }: { children: React.ReactNode }) { |
| 462 | // Undefined when the root loader itself failed. |
| 463 | const loaded = useRouteLoaderData<typeof loader>("root"); |
| 464 | const inBrowser = typeof document !== "undefined"; |
| 465 | if (loaded && inBrowser) lastRoot = loaded; |
| 466 | const root = loaded ?? (inBrowser ? lastRoot : undefined); |
| 467 | const user = root?.user; |
| 468 | const { pathname, search } = useLocation(); |
| 469 | // /verify lands here with ?sent=1 once it has sent the link again. |
| 470 | const sentAgain = new URLSearchParams(search).get("sent") === "1"; |
| 471 | // Drawn around the error page too: a 404 keeps the sidebar out of a |
| 472 | // project or workspace the viewer cannot see. |
| 473 | const error = useRouteError(); |
| 474 | const missing = isRouteErrorResponse(error) && error.status === 404; |
| 475 | // The billing page shows the full banner itself. |
| 476 | const paused = root?.shell?.compute && root.shell.workspace && !pathname.endsWith("/-/billing") && ( |
| 477 | <SpikeBanner |
| 478 | compact |
| 479 | slug={root.shell.workspace.slug} |
| 480 | entitlements={{ paused: root.shell.compute.paused, spike: root.shell.compute.spike }} |
| 481 | owner={root.shell.compute.owner} |
| 482 | /> |
| 483 | ); |
| 484 | const verify = user && !user.verified && ( |
| 485 | <Form |
| 486 | method="post" |
| 487 | action="/verify" |
| 488 | className="flex flex-wrap items-center justify-center gap-x-3 gap-y-1 border-b border-warn/30 bg-warn/10 px-4 py-2 text-sm" |
| 489 | > |
| 490 | <span role="status"> |
| 491 | {sentAgain |
| 492 | ? "We sent the link again. It can take a minute to arrive." |
| 493 | : "Confirm your email address to create repositories and push. We sent you a link."} |
| 494 | </span> |
| 495 | <SubmitButton |
| 496 | name="intent" |
| 497 | value="resend-verification" |
| 498 | pending="Sending…" |
| 499 | className="inline-flex items-center gap-1.5 font-medium underline underline-offset-4 disabled:opacity-50" |
| 500 | > |
| 501 | {sentAgain ? "Send another" : "Send it again"} |
| 502 | </SubmitButton> |
| 503 | </Form> |
| 504 | ); |
| 505 | const leaving = useLeaving(); |
| 506 | const banner = |
| 507 | paused || verify ? ( |
| 508 | <> |
| 509 | {paused} |
| 510 | {verify} |
| 511 | </> |
| 512 | ) : null; |
| 513 | return ( |
| 514 | <html lang="en"> |
| 515 | <head> |
| 516 | <meta charSet="utf-8" /> |
| 517 | <meta name="viewport" content="width=device-width, initial-scale=1" /> |
| 518 | <meta name="theme-color" content="#0f0f11" /> |
| 519 | {/* The stylesheet before everything React Router preloads, so a slow |
| 520 | connection paints sooner (docs/research/css-shipping.md). */} |
| 521 | <link rel="stylesheet" href={appCss} precedence="default" /> |
| 522 | <Meta /> |
| 523 | <Links /> |
| 524 | </head> |
| 525 | <body className="flex min-h-screen flex-col"> |
| 526 | {/* The first stop for the keyboard: past the menus, to the page. */} |
| 527 | <a |
| 528 | href="#content" |
| 529 | className="sr-only rounded-md bg-fg px-3 py-2 text-sm font-medium text-bg focus:not-sr-only focus:fixed focus:top-2 focus:left-2 focus:z-[70]" |
| 530 | > |
| 531 | Skip to content |
| 532 | </a> |
| 533 | {root?.shell && usesAppShell(pathname, user != null) ? ( |
| 534 | <AppShell user={user ?? null} shell={root.shell} missing={missing} banner={banner}> |
| 535 | {children} |
| 536 | </AppShell> |
| 537 | ) : ( |
| 538 | <> |
| 539 | <Progress /> |
| 540 | <Header user={user} /> |
| 541 | {banner} |
| 542 | <div id="content" tabIndex={-1} {...leaving} className={`grow outline-none ${leaving.className}`}> |
| 543 | {children} |
| 544 | </div> |
| 545 | <SiteFooter user={user} /> |
| 546 | </> |
| 547 | )} |
| 548 | <ScrollRestoration /> |
| 549 | <Scripts /> |
| 550 | </body> |
| 551 | </html> |
| 552 | ); |
| 553 | } |
| 554 | |
| 555 | export default function App() { |
| 556 | return <Outlet />; |
| 557 | } |
| 558 | |
| 559 | export function ErrorBoundary({ error }: Route.ErrorBoundaryProps) { |
| 560 | const location = useLocation(); |
| 561 | const matches = useMatches(); |
| 562 | // A tab left open across a deploy: load the address again as a whole |
| 563 | // page, once, rather than show an error the reload fixes. |
| 564 | const reload = reloadFixes({ |
| 565 | error, |
| 566 | status: isRouteErrorResponse(error) ? error.status : undefined, |
| 567 | // The first page a tab loads has the key "default"; a client navigation's is its own. |
| 568 | clientNavigation: location.key !== "default", |
| 569 | caughtByCatchAll: matches.at(-1)?.id === "routes/not-found", |
| 570 | }); |
| 571 | const href = location.pathname + location.search + location.hash; |
| 572 | useEffect(() => { |
| 573 | if (!reload) return; |
| 574 | try { |
| 575 | if (sessionStorage.getItem(RELOADED_KEY) === href) return; |
| 576 | sessionStorage.setItem(RELOADED_KEY, href); |
| 577 | } catch { |
| 578 | // No session storage: reload anyway; a document load never asks again. |
| 579 | } |
| 580 | window.location.assign(href); |
| 581 | }, [reload, href]); |
| 582 | if (reload) { |
| 583 | return ( |
| 584 | <main className="mx-auto max-w-xl px-4 py-32 text-center text-sm text-muted" aria-busy="true"> |
| 585 | <title>Loading · g1t</title> |
| 586 | Loading the latest version of g1t… |
| 587 | </main> |
| 588 | ); |
| 589 | } |
| 590 | |
| 591 | let title = "Something went wrong"; |
| 592 | let details = "An unexpected error occurred. Try again in a moment."; |
| 593 | let stack: string | undefined; |
| 594 | |
| 595 | if (isRouteErrorResponse(error)) { |
| 596 | // The same page for something private and something missing. |
| 597 | if (error.status === 404) { |
| 598 | return ( |
| 599 | <> |
| 600 | <title>Not found · g1t</title> |
| 601 | <NotFound data={error.data} /> |
| 602 | </> |
| 603 | ); |
| 604 | } |
| 605 | if (typeof error.data === "string" && error.data) { |
| 606 | details = error.data; |
| 607 | } |
| 608 | } else if (import.meta.env.DEV && error instanceof Error) { |
| 609 | details = error.message; |
| 610 | stack = error.stack; |
| 611 | } |
| 612 | |
| 613 | return ( |
| 614 | <main className="mx-auto max-w-xl px-4 py-32 text-center"> |
| 615 | <h1 className="text-2xl font-semibold tracking-tight">{title}</h1> |
| 616 | <p className="mt-3 text-muted">{details}</p> |
| 617 | <div className="mt-8"> |
| 618 | <ButtonLink to="/" variant="quiet"> |
| 619 | Back to home |
| 620 | </ButtonLink> |
| 621 | </div> |
| 622 | {stack && ( |
| 623 | <pre className="mt-8 overflow-x-auto rounded-lg border border-line p-4 text-left text-xs"> |
| 624 | <code>{stack}</code> |
| 625 | </pre> |
| 626 | )} |
| 627 | </main> |
| 628 | ); |
| 629 | } |