flagon-io/g1t

public

Where people and agents ship software together. The open-source git platform for the whole job: issues, agents, checks and deploys to the edge.

g1t/apps/web/app/components/shell.tsx

1,018 lines39,713 bytesCodeBlame
1import {
2 ArrowLeft,
3 BarChart3,
4 BookMarked,
5 BookOpen,
6 Check,
7 ChevronsUpDown,
8 Ellipsis,
9 CircleDot,
10 Code2,
11 Compass,
12 CornerDownLeft,
13 CreditCard,
14 GitPullRequest,
15 History,
16 Fingerprint,
17 KeyRound,
18 Box,
19 Layers,
20 LayoutDashboard,
21 LayoutGrid,
22 ListTree,
23 Lock,
24 LogOut,
25 Menu,
26 Plus,
27 Search,
28 Plug,
29 Settings,
30 Users,
31 Webhook,
32 PlayCircle,
33 Rocket,
34 X,
35} from "lucide-react";
36import { type ReactNode, useEffect, useMemo, useRef, useState } from "react";
37import { Form, Link, NavLink, useLocation, useNavigate, useNavigation, useSubmit } from "react-router";
38
39import type { User } from "@g1t/contracts";
40import { MICROS_PER_DOLLAR } from "@g1t/contracts";
41
42import { Mark } from "./logo";
43import { Avatar } from "./ui";
44import {
45 DropdownMenu,
46 DropdownMenuContent,
47 DropdownMenuItem,
48 DropdownMenuLabel,
49 DropdownMenuSeparator,
50 DropdownMenuTrigger,
51} from "./ui/dropdown-menu";
52
53/** What the sidebar needs, worked out by the root loader for a signed-in person. */
54export type ShellData = {
55 /** The workspace the sidebar is about: the one being looked at, or their first. */
56 workspace: { slug: string; role: "owner" | "member" } | null;
57 /** Its projects, by name: `name` is the slug in their address. */
58 repos: { namespace: string; name: string; title?: string; isPrivate: boolean }[];
59 /** The project being looked at, if any, whoever owns it. */
60 repo: {
61 namespace: string;
62 name: string;
63 member: boolean;
64 issues: number;
65 pulls: number;
66 } | null;
67 /** The workspace's agent credit, if billing is on and they may see it. */
68 creditMicros: number | null;
69 /** Whether g1t charges nothing for now, while it is being built out. */
70 free?: boolean;
71 /** What its agents have cost since the start of the month. */
72 /** This month's usage: charged, or at cost while g1t is free. */
73 monthUsageMicros: number | null;
74};
75
76function SidebarLink({
77 to,
78 icon,
79 end,
80 count,
81 also,
82 children,
83}: {
84 to: string;
85 icon: ReactNode;
86 end?: boolean;
87 count?: number;
88 /** Other path prefixes under which this link is the current one. */
89 also?: string | string[];
90 children: ReactNode;
91}) {
92 const { pathname } = useLocation();
93 return (
94 <NavLink
95 to={to}
96 end={end}
97 prefetch="intent"
98 className={({ isActive, isPending }) => {
99 const current =
100 isActive || [also ?? []].flat().some((prefix) => pathname === prefix || pathname.startsWith(prefix + "/"));
101 return `group flex h-8 items-center gap-2.5 rounded-md px-2 text-[0.8125rem] transition-colors ${
102 current
103 ? "bg-raised font-medium text-fg"
104 : isPending
105 ? "bg-raised/60 text-fg"
106 : "text-muted hover:bg-raised/60 hover:text-fg"
107 }`;
108 }}
109 >
110 <span className="shrink-0 text-faint group-hover:text-muted">{icon}</span>
111 <span className="min-w-0 grow truncate">{children}</span>
112 {count != null && count > 0 && (
113 <span className="rounded bg-line px-1.5 text-[0.6875rem] tabular-nums text-muted">
114 {count}
115 </span>
116 )}
117 </NavLink>
118 );
119}
120
121function SidebarGroup({ title, action, children }: { title: string; action?: ReactNode; children: ReactNode }) {
122 return (
123 <section className="mt-6">
124 <div className="mb-1 flex h-6 items-center justify-between px-2">
125 <h2 className="text-xs font-medium text-faint">{title}</h2>
126 {action}
127 </div>
128 <div className="space-y-px">{children}</div>
129 </section>
130 );
131}
132
133function WorkspaceSwitcher({ user, shell }: { user: User; shell: ShellData }) {
134 const current = shell.workspace?.slug;
135 return (
136 <DropdownMenu>
137 {/* The name goes to the workspace; only the arrows switch it. */}
138 <Link
139 to={current ? `/${current}` : "/workspaces/new"}
140 className="flex h-8 min-w-0 items-center gap-2 rounded-md px-1.5 transition-colors hover:bg-raised"
141 >
142 {current ? (
143 <Avatar name={current} size={20} square />
144 ) : (
145 <span className="size-5 rounded-md border border-dashed border-line-strong" />
146 )}
147 <span className="min-w-0 truncate text-sm font-medium">{current ?? "Choose a workspace"}</span>
148 {shell.workspace && (
149 <span className="shrink-0 rounded-full bg-raised px-1.5 py-px text-[0.625rem] font-medium text-muted ring-1 ring-line capitalize">
150 {shell.workspace.role}
151 </span>
152 )}
153 </Link>
154 <DropdownMenuTrigger
155 aria-label="Switch workspace"
156 className="ml-auto flex h-8 w-6 shrink-0 items-center justify-center rounded-md text-faint outline-none transition-colors hover:bg-raised hover:text-fg data-[state=open]:bg-raised data-[state=open]:text-fg"
157 >
158 <ChevronsUpDown size={14} />
159 </DropdownMenuTrigger>
160 <DropdownMenuContent align="start" className="w-60">
161 <DropdownMenuLabel>Workspaces</DropdownMenuLabel>
162 {(user.workspaces ?? []).map((membership) => (
163 <DropdownMenuItem asChild key={membership.slug}>
164 <Link to={`/${membership.slug}`}>
165 <Avatar name={membership.slug} size={16} square />
166 <span className="grow">{membership.slug}</span>
167 {membership.slug === current && <Check className="text-accent" />}
168 </Link>
169 </DropdownMenuItem>
170 ))}
171 <DropdownMenuSeparator />
172 <DropdownMenuItem asChild>
173 <Link to="/workspaces/new">
174 <Plus />
175 New workspace
176 </Link>
177 </DropdownMenuItem>
178 </DropdownMenuContent>
179 </DropdownMenu>
180 );
181}
182
183/** This month's spend against what is left, as Vercel shows a plan's usage. */
184function UsageCard({ slug, shell }: { slug: string; shell: ShellData }) {
185 if (shell.monthUsageMicros == null) return null;
186 const spent = shell.monthUsageMicros;
187 const left = shell.creditMicros;
188 const share = left != null && spent + left > 0 ? Math.min(1, spent / (spent + Math.max(left, 0))) : 0;
189 return (
190 <Link
191 to={`/${slug}/-/usage`}
192 className="block rounded-lg bg-surface p-3 ring-1 ring-line transition-colors hover:ring-line-strong"
193 >
194 <span className="flex items-baseline justify-between text-xs">
195 <span className="font-medium text-fg">Usage</span>
196 <span className="text-faint">this month</span>
197 </span>
198 <span className="mt-2 flex items-baseline justify-between">
199 <span className="font-mono text-sm tabular-nums">${(spent / MICROS_PER_DOLLAR).toFixed(2)}</span>
200 {shell.free ? (
201 <span className="text-xs text-accent">Free for now</span>
202 ) : (
203 left != null && (
204 <span className={`text-xs ${left <= 0 ? "text-warn" : "text-faint"}`}>
205 ${(left / MICROS_PER_DOLLAR).toFixed(2)} left
206 </span>
207 )
208 )}
209 </span>
210 <span className="mt-2 block h-1 overflow-hidden rounded-full bg-raised">
211 <span
212 className={`block h-full rounded-full ${left != null && left <= 0 ? "bg-warn" : "bg-accent"}`}
213 style={{ width: `${Math.max(share * 100, spent > 0 ? 3 : 0)}%` }}
214 />
215 </span>
216 </Link>
217 );
218}
219
220function AccountMenu({ user }: { user: User }) {
221 const submit = useSubmit();
222 return (
223 <DropdownMenu>
224 <DropdownMenuTrigger className="flex h-10 w-full items-center gap-2.5 rounded-md px-2 text-left outline-none transition-colors hover:bg-raised data-[state=open]:bg-raised">
225 <Avatar name={user.username} size={22} />
226 <span className="min-w-0 grow truncate text-[0.8125rem] font-medium">{user.username}</span>
227 <Ellipsis size={15} className="shrink-0 text-faint" />
228 </DropdownMenuTrigger>
229 <DropdownMenuContent align="start" side="top" className="w-56">
230 <DropdownMenuLabel>
231 Signed in as <span className="font-mono font-medium text-fg">{user.username}</span>
232 </DropdownMenuLabel>
233 <DropdownMenuSeparator />
234 <DropdownMenuItem asChild>
235 <Link to="/settings">
236 <Settings />
237 Your settings
238 </Link>
239 </DropdownMenuItem>
240 <DropdownMenuItem asChild>
241 <a href="https://docs.g1t.sh/">
242 <BookOpen />
243 Documentation
244 </a>
245 </DropdownMenuItem>
246 <DropdownMenuSeparator />
247 {/* Submitted from here: the menu closes on select, and a button
248 that has left the page cannot submit a form. */}
249 <DropdownMenuItem onSelect={() => submit(null, { method: "post", action: "/logout" })}>
250 <LogOut />
251 Sign out
252 </DropdownMenuItem>
253 </DropdownMenuContent>
254 </DropdownMenu>
255 );
256}
257
258/** A workspace's settings pages, which the sidebar slides over to. */
259const SETTINGS_PAGE = /^\/([^/]+)\/-\/(settings|people|tokens|billing|integrations|webhooks|secrets)(\/|$)/;
260
261/**
262 * The sidebar's menus are two layers, the way a phone pushes a screen: the
263 * one arriving slides in from the right over the full width, while the one
264 * leaving drifts a quarter of the way left and fades, both on one long
265 * ease-out, the fade quicker than the move so the two never blur together.
266 * Going back reverses it.
267 */
268const LAYER =
269 "absolute inset-0 [transition:translate_380ms_cubic-bezier(0.32,0.72,0,1),opacity_220ms_ease-out] will-change-[translate,opacity] motion-reduce:transition-none";
270/** One menu, filling its layer. */
271const PANEL = "h-full overflow-y-auto px-2 pb-4";
272
273/** A workspace's settings, as the sidebar shows them in place of everything else. */
274function SettingsMenu({ slug, owner, open }: { slug: string; owner: boolean; open: boolean }) {
275 return (
276 <nav
277 aria-label="Workspace settings"
278 inert={!open}
279 className={PANEL}
280 >
281 <Link
282 to={`/${slug}`}
283 className="group mt-3 flex h-8 items-center gap-2 rounded-md px-2 text-[0.8125rem] text-muted transition-colors hover:bg-raised/60 hover:text-fg"
284 >
285 <ArrowLeft size={15} className="text-faint transition-transform group-hover:-translate-x-0.5 group-hover:text-muted" />
286 <span className="truncate">{slug}</span>
287 </Link>
288 <SidebarGroup title="Settings">
289 {owner && (
290 <SidebarLink to={`/${slug}/-/settings`} icon={<Settings size={15} />}>
291 General
292 </SidebarLink>
293 )}
294 <SidebarLink to={`/${slug}/-/people`} icon={<Users size={15} />}>
295 Members
296 </SidebarLink>
297 <SidebarLink to={`/${slug}/-/tokens`} icon={<KeyRound size={15} />}>
298 Access tokens
299 </SidebarLink>
300 <SidebarLink to={`/${slug}/-/billing`} icon={<CreditCard size={15} />}>
301 Billing and plans
302 </SidebarLink>
303 <SidebarLink to={`/${slug}/-/integrations`} icon={<Plug size={15} />}>
304 Integrations
305 </SidebarLink>
306 <SidebarLink to={`/${slug}/-/secrets`} icon={<Lock size={15} />}>
307 Secrets and variables
308 </SidebarLink>
309 <SidebarLink to={`/${slug}/-/webhooks`} icon={<Webhook size={15} />}>
310 Webhooks
311 </SidebarLink>
312 </SidebarGroup>
313 </nav>
314 );
315}
316
317/**
318 * What the repository menu needs. Known from the address before the page's
319 * data arrives, so the menu is right from the first frame; the counts fill
320 * in once it does.
321 */
322type MenuRepo = {
323 namespace: string;
324 name: string;
325 member: boolean;
326 issues?: number;
327 pulls?: number;
328};
329
330function sameRepo(a: { namespace: string; name: string } | null, b: { namespace: string; name: string } | null) {
331 return (
332 a != null &&
333 b != null &&
334 a.namespace.toLowerCase() === b.namespace.toLowerCase() &&
335 a.name.toLowerCase() === b.name.toLowerCase()
336 );
337}
338
339/**
340 * A project's own menu, which the sidebar slides to while you are in it,
341 * as it does for settings: everything about the project, running and its
342 * code, and nothing else, with the way back to everything.
343 */
344function RepoMenu({ repo, isPrivate, open }: { repo: MenuRepo; isPrivate: boolean; open: boolean }) {
345 const base = `/${repo.namespace}/${repo.name}`;
346 return (
347 <nav aria-label={`${repo.namespace}/${repo.name}`} inert={!open} className={PANEL}>
348 <Link
349 to="/"
350 className="group mt-3 flex h-8 items-center gap-2 rounded-md px-2 text-[0.8125rem] text-muted transition-colors hover:bg-raised/60 hover:text-fg"
351 >
352 <ArrowLeft size={15} className="text-faint transition-transform group-hover:-translate-x-0.5 group-hover:text-muted" />
353 Mission control
354 </Link>
355 <Link
356 to={base}
357 className="mt-3 flex items-center gap-2 rounded-md px-2 py-1.5 transition-colors hover:bg-raised/60"
358 >
359 <span className="flex size-6 shrink-0 items-center justify-center rounded-md bg-raised text-muted ring-1 ring-line">
360 {isPrivate ? <Lock size={13} /> : <Box size={13} />}
361 </span>
362 <span className="min-w-0 truncate font-mono text-[0.8125rem]">
363 <span className="text-faint">{repo.namespace}/</span>
364 <span className="font-semibold text-fg">{repo.name}</span>
365 </span>
366 </Link>
367 <div className="mt-3 space-y-px">
368 <SidebarLink to={base} end icon={<LayoutGrid size={15} />}>
369 Overview
370 </SidebarLink>
371 {repo.member && (
372 <SidebarLink to={`${base}/deployments`} icon={<Rocket size={15} />}>
373 Deployments
374 </SidebarLink>
375 )}
376 </div>
377 <SidebarGroup title="Code">
378 <SidebarLink to={`${base}/code`} icon={<Code2 size={15} />} also={[`${base}/tree`, `${base}/blob`]}>
379 Code
380 </SidebarLink>
381 <SidebarLink to={`${base}/commits`} also={`${base}/commit`} icon={<History size={15} />}>
382 Commits
383 </SidebarLink>
384 </SidebarGroup>
385 <SidebarGroup title="Work">
386 <SidebarLink to={`${base}/issues`} icon={<CircleDot size={15} />} count={repo.issues}>
387 Issues
388 </SidebarLink>
389 <SidebarLink to={`${base}/pulls`} also={`${base}/pull`} icon={<GitPullRequest size={15} />} count={repo.pulls}>
390 Pull requests
391 </SidebarLink>
392 {repo.member && (
393 <SidebarLink to={`${base}/plans`} icon={<ListTree size={15} />}>
394 Plan
395 </SidebarLink>
396 )}
397 <SidebarLink to={`${base}/queue`} icon={<Layers size={15} />}>
398 Merge queue
399 </SidebarLink>
400 </SidebarGroup>
401 <SidebarGroup title="Automate">
402 <SidebarLink to={`${base}/actions`} icon={<PlayCircle size={15} />}>
403 Actions
404 </SidebarLink>
405 </SidebarGroup>
406 {repo.member && (
407 <SidebarGroup title="Project">
408 <SidebarLink to={`${base}/settings`} icon={<Settings size={15} />}>
409 Settings
410 </SidebarLink>
411 </SidebarGroup>
412 )}
413 </nav>
414 );
415}
416
417/** Your own settings, as the sidebar shows them on the settings page. */
418function AccountSettingsMenu({ open }: { open: boolean }) {
419 const { hash } = useLocation();
420 const item = (id: string, icon: ReactNode, label: string) => (
421 <Link
422 to={`/settings#${id}`}
423 className={`group flex h-8 items-center gap-2.5 rounded-md px-2 text-[0.8125rem] transition-colors ${
424 hash === `#${id}` ? "bg-raised font-medium text-fg" : "text-muted hover:bg-raised/60 hover:text-fg"
425 }`}
426 >
427 <span className="shrink-0 text-faint group-hover:text-muted">{icon}</span>
428 {label}
429 </Link>
430 );
431 return (
432 <nav
433 aria-label="Your settings"
434 inert={!open}
435 className={PANEL}
436 >
437 <Link
438 to="/"
439 className="group mt-3 flex h-8 items-center gap-2 rounded-md px-2 text-[0.8125rem] text-muted transition-colors hover:bg-raised/60 hover:text-fg"
440 >
441 <ArrowLeft size={15} className="text-faint transition-transform group-hover:-translate-x-0.5 group-hover:text-muted" />
442 Mission control
443 </Link>
444 <SidebarGroup title="Account">
445 {item("ssh-keys", <Fingerprint size={15} />, "SSH keys")}
446 {item("tokens", <KeyRound size={15} />, "Access tokens")}
447 {item("applications", <Plug size={15} />, "Connected applications")}
448 </SidebarGroup>
449 </nav>
450 );
451}
452
453function Sidebar({ user, shell, onFind }: { user: User; shell: ShellData; onFind: () => void }) {
454 const ws = shell.workspace;
455 const { pathname } = useLocation();
456 const going = useNavigation().location?.pathname;
457 // On a workspace's settings page, or on the way to one, its settings take
458 // the sidebar over.
459 const inSettings = ws != null && SETTINGS_PAGE.exec(going ?? pathname)?.[1]?.toLowerCase() === ws.slug;
460 const inAccount = (going ?? pathname) === "/settings";
461 const active = shell.repo;
462 // In a repository, or on the way into one, its own menu takes the sidebar.
463 const target = going ?? pathname;
464 const repoPath = /^\/([^/]+)\/([^/-][^/]*)(\/|$)/.exec(target);
465 const reserved = new Set(["settings", "explore", "search", "new", "workspaces", "login", "logout", "register", "verify", "forgot", "reset", "device", "oauth"]);
466 const inRepo = repoPath != null && !reserved.has(repoPath[1]) && repoPath[2] !== "-";
467 const away = inSettings || inAccount || inRepo;
468 // The repository the menu is for: the one loaded if it is the one being
469 // gone to, else what the address says, at once.
470 const targetRepo = inRepo && repoPath ? { namespace: repoPath[1], name: repoPath[2] } : null;
471 const menuRepo: MenuRepo | null = targetRepo
472 ? sameRepo(active, targetRepo)
473 ? active
474 : {
475 ...targetRepo,
476 member: (user.workspaces ?? []).some((m) => m.slug === targetRepo.namespace.toLowerCase()),
477 }
478 : null;
479 // What sits on the far side of the track. Kept while sliding back, so it
480 // does not vanish on the way out.
481 const side = useRef<"workspace" | "account" | "repo">("workspace");
482 if (inAccount) side.current = "account";
483 else if (inSettings) side.current = "workspace";
484 else if (inRepo) side.current = "repo";
485 // The repository last shown, kept for the slide back.
486 const shown = useRef<MenuRepo | null>(menuRepo);
487 if (menuRepo) shown.current = menuRepo;
488 // Moving between two of the far-side menus (settings to a repository,
489 // one repository to another) crossfades in place instead of sliding.
490 const detailKey = side.current === "repo" ? `repo:${shown.current?.namespace}/${shown.current?.name}` : side.current;
491 const lastAway = useRef(away);
492 const lastKey = useRef(detailKey);
493 const swapped = lastAway.current && away && lastKey.current !== detailKey;
494 useEffect(() => {
495 lastAway.current = away;
496 lastKey.current = detailKey;
497 });
498 // The repository being looked at is listed even when it is someone else's.
499 const listed =
500 active && !shell.repos.some((repo) => repo.namespace === active.namespace && repo.name === active.name)
501 ? [{ namespace: active.namespace, name: active.name, isPrivate: false }, ...shell.repos]
502 : shell.repos;
503 return (
504 <div className="flex h-full flex-col">
505 {/* The same height and rule as the top bar, so the two read as one line. */}
506 <div className="flex h-12 shrink-0 items-center gap-1 border-b border-line px-2">
507 <Link to="/" aria-label="g1t home" className="shrink-0 rounded-md p-1.5 hover:bg-raised">
508 <Mark className="size-5" />
509 </Link>
510 <span className="shrink-0 text-line-strong" aria-hidden="true">
511 /
512 </span>
513 <WorkspaceSwitcher user={user} shell={shell} />
514 </div>
515 <div className="px-2 pt-3">
516 <button
517 type="button"
518 onClick={onFind}
519 className="flex h-8 w-full items-center gap-2 rounded-md bg-surface px-2.5 text-[0.8125rem] text-faint ring-1 ring-line transition-colors hover:text-muted hover:ring-line-strong"
520 >
521 <Search size={14} />
522 <span className="grow text-left">Find…</span>
523 <kbd className="rounded bg-raised px-1.5 font-mono text-[0.625rem] text-muted ring-1 ring-line">⌘K</kbd>
524 </button>
525 </div>
526 <div className="relative min-h-0 grow overflow-hidden">
527 <div className={`${LAYER} ${away ? "pointer-events-none -translate-x-1/4 opacity-0" : "translate-x-0 opacity-100"}`}>
528 <nav aria-label="g1t" inert={away} className={PANEL}>
529 <div className="mt-3 space-y-px">
530 <SidebarLink to="/" end icon={<LayoutDashboard size={15} />}>
531 Mission control
532 </SidebarLink>
533 <SidebarLink to="/explore" icon={<Compass size={15} />}>
534 Explore
535 </SidebarLink>
536 </div>
537
538 <SidebarGroup
539 title="Projects"
540 action={
541 ws && (
542 <Link
543 to={`/new?workspace=${ws.slug}`}
544 aria-label="New project"
545 className="rounded p-0.5 text-faint hover:bg-raised hover:text-fg"
546 >
547 <Plus size={13} />
548 </Link>
549 )
550 }
551 >
552 {listed.length === 0 && (
553 <p className="px-2 py-1 text-xs text-faint">None yet.</p>
554 )}
555 {listed.map((repo) => {
556 const base = `/${repo.namespace}/${repo.name}`;
557 return (
558 <div key={base}>
559 <SidebarLink
560 to={base}
561 icon={repo.isPrivate ? <Lock size={15} /> : <Box size={15} />}
562 >
563 <span className="text-[0.8125rem]">
564 {repo.namespace !== ws?.slug && (
565 <span className="font-mono text-faint">{repo.namespace}/</span>
566 )}
567 {"title" in repo && repo.title ? repo.title : repo.name}
568 </span>
569 </SidebarLink>
570 </div>
571 );
572 })}
573 </SidebarGroup>
574 </nav>
575 </div>
576 <div className={`${LAYER} ${away ? "translate-x-0 opacity-100" : "pointer-events-none translate-x-full opacity-0"}`}>
577 <div key={detailKey} className={`h-full ${swapped ? "animate-[g1t-swap_220ms_ease-out]" : ""}`}>
578 {side.current === "repo" && shown.current ? (
579 <RepoMenu
580 repo={shown.current}
581 isPrivate={shell.repos.some((repo) => sameRepo(repo, shown.current) && repo.isPrivate)}
582 open={inRepo}
583 />
584 ) : side.current === "account" || !ws ? (
585 <AccountSettingsMenu open={inAccount} />
586 ) : (
587 <SettingsMenu slug={ws.slug} owner={ws.role === "owner"} open={inSettings} />
588 )}
589 </div>
590 </div>
591 </div>
592
593 {/* The workspace's own things sit at the bottom, by its usage and the account. */}
594 <div className="space-y-2 p-2">
595 {ws && (
596 <div className="space-y-px">
597 <p className="px-2 pb-1 text-[0.6875rem] font-medium uppercase tracking-wider text-faint">{ws.slug}</p>
598 <SidebarLink to={`/${ws.slug}/-/usage`} icon={<BarChart3 size={15} />}>
599 Usage
600 </SidebarLink>
601 {/* Everything else about the workspace lives in its settings, and only there. */}
602 <SidebarLink
603 to={ws.role === "owner" ? `/${ws.slug}/-/settings` : `/${ws.slug}/-/people`}
604 icon={<Settings size={15} />}
605 >
606 Settings
607 </SidebarLink>
608 </div>
609 )}
610 {ws && <UsageCard slug={ws.slug} shell={shell} />}
611 <AccountMenu user={user} />
612 </div>
613 </div>
614 );
615}
616
617/** Words for the sections a path can end in. */
618const SECTIONS: Record<string, string> = {
619 issues: "Issues",
620 pulls: "Pull requests",
621 queue: "Merge queue",
622 commits: "Commits",
623 plans: "Plan",
624 actions: "Actions",
625 deployments: "Deployments",
626 repository: "Repository",
627 dependencies: "Dependencies",
628 code: "Code",
629 secrets: "Secrets and variables",
630 settings: "Settings",
631 people: "Members",
632 tokens: "Access tokens",
633 usage: "Usage",
634 billing: "Billing and plans",
635 integrations: "Integrations",
636 webhooks: "Webhooks",
637 tree: "Code",
638 blob: "Code",
639};
640
641/** Where the page is, as a trail of links: workspace / repository / section. */
642function Breadcrumbs({ pathname }: { pathname: string }) {
643 const parts = pathname.split("/").filter(Boolean);
644 const reserved = ["settings", "explore", "new", "search", "workspaces"];
645 if (parts.length === 0) return <span className="text-sm font-medium">Mission control</span>;
646 if (reserved.includes(parts[0]!)) {
647 const words: Record<string, string> = {
648 settings: "Account",
649 explore: "Explore",
650 new: "New project",
651 search: "Search",
652 workspaces: "New workspace",
653 };
654 return <span className="text-sm font-medium">{words[parts[0]!]}</span>;
655 }
656 const [owner, second, third, fourth] = parts;
657 const trail: { label: string; to: string; mono?: boolean }[] = [{ label: owner!, to: `/${owner}`, mono: true }];
658 if (second === "-") {
659 const page = `/${owner}/-/${third}`;
660 if (third && SETTINGS_PAGE.test(page)) {
661 trail.push({ label: "Settings", to: `/${owner}/-/settings` });
662 trail.push({ label: third === "settings" ? "General" : (SECTIONS[third] ?? third), to: page });
663 } else if (third) trail.push({ label: SECTIONS[third] ?? third, to: page });
664 } else if (second) {
665 const repo = `/${owner}/${second}`;
666 trail.push({ label: second, to: repo, mono: true });
667 if (third === "pull" && fourth) trail.push({ label: `Pull request #${fourth}`, to: `${repo}/pull/${fourth}` });
668 else if (third === "issues" && fourth && fourth !== "new") trail.push({ label: `Issue #${fourth}`, to: `${repo}/issues/${fourth}` });
669 else if (third === "commit" && fourth) trail.push({ label: fourth.slice(0, 7), to: `${repo}/commit/${fourth}`, mono: true });
670 else if (third && SECTIONS[third]) {
671 trail.push({ label: SECTIONS[third]!, to: `${repo}/${third}` });
672 // A settings page names which one: Settings / Secrets and variables.
673 if (third === "settings" && fourth && SECTIONS[fourth]) {
674 trail.push({ label: SECTIONS[fourth]!, to: `${repo}/settings/${fourth}` });
675 }
676 }
677 }
678 return (
679 <nav aria-label="Where you are" className="flex min-w-0 items-center gap-1.5 text-sm">
680 {trail.map((crumb, index) => (
681 <span key={crumb.to} className="flex min-w-0 items-center gap-1.5">
682 {index > 0 && <span className="text-line-strong">/</span>}
683 <Link
684 to={crumb.to}
685 className={`truncate rounded px-1 py-0.5 transition-colors hover:bg-raised ${
686 index === trail.length - 1 ? "font-medium text-fg" : "text-muted hover:text-fg"
687 } ${crumb.mono ? "font-mono text-[0.8125rem]" : ""}`}
688 >
689 {crumb.label}
690 </Link>
691 </span>
692 ))}
693 </nav>
694 );
695}
696
697type Command = { label: string; hint?: string; to: string; icon: ReactNode };
698
699/** Everything the palette can jump to, from what the sidebar already knows. */
700function commandsFor(user: User, shell: ShellData): Command[] {
701 const commands: Command[] = [
702 { label: "Mission control", to: "/", icon: <LayoutDashboard size={15} /> },
703 { label: "Explore repositories", to: "/explore", icon: <Compass size={15} /> },
704 { label: "New project", to: "/new", icon: <Plus size={15} /> },
705 { label: "New workspace", to: "/workspaces/new", icon: <Plus size={15} /> },
706 { label: "Your settings", to: "/settings", icon: <Settings size={15} /> },
707 ];
708 const repo = shell.repo;
709 if (repo) {
710 const base = `/${repo.namespace}/${repo.name}`;
711 const name = `${repo.namespace}/${repo.name}`;
712 commands.unshift(
713 { label: "Overview", hint: name, to: base, icon: <LayoutGrid size={15} /> },
714 { label: "Code", hint: name, to: `${base}/code`, icon: <Code2 size={15} /> },
715 { label: "Issues", hint: name, to: `${base}/issues`, icon: <CircleDot size={15} /> },
716 { label: "New issue", hint: name, to: `${base}/issues/new`, icon: <Plus size={15} /> },
717 { label: "Pull requests", hint: name, to: `${base}/pulls`, icon: <GitPullRequest size={15} /> },
718 { label: "Commits", hint: name, to: `${base}/commits`, icon: <History size={15} /> },
719 ...(repo.member
720 ? [
721 { label: "Plan work", hint: name, to: `${base}/plans`, icon: <ListTree size={15} /> },
722 { label: "Deployments", hint: name, to: `${base}/deployments`, icon: <Rocket size={15} /> },
723 { label: "Secrets and variables", hint: name, to: `${base}/settings/secrets`, icon: <Lock size={15} /> },
724 { label: "Project settings", hint: name, to: `${base}/settings`, icon: <Settings size={15} /> },
725 { label: "Repository settings", hint: name, to: `${base}/settings/repository`, icon: <Settings size={15} /> },
726 ]
727 : []),
728 );
729 }
730 for (const membership of user.workspaces ?? []) {
731 commands.push(
732 { label: membership.slug, hint: "Workspace", to: `/${membership.slug}`, icon: <Avatar name={membership.slug} size={15} square /> },
733 { label: "Usage", hint: membership.slug, to: `/${membership.slug}/-/usage`, icon: <BarChart3 size={15} /> },
734 { label: "Billing and plans", hint: `${membership.slug} · Settings`, to: `/${membership.slug}/-/billing`, icon: <CreditCard size={15} /> },
735 { label: "Access tokens", hint: `${membership.slug} · Settings`, to: `/${membership.slug}/-/tokens`, icon: <KeyRound size={15} /> },
736 { label: "Integrations", hint: `${membership.slug} · Settings`, to: `/${membership.slug}/-/integrations`, icon: <Plug size={15} /> },
737 );
738 }
739 for (const listed of shell.repos) {
740 commands.push({
741 label: listed.title ?? listed.name,
742 hint: `${listed.namespace} · Project`,
743 to: `/${listed.namespace}/${listed.name}`,
744 icon: <Box size={15} />,
745 });
746 }
747 return commands;
748}
749
750function CommandPalette({
751 open,
752 onClose,
753 commands,
754}: {
755 open: boolean;
756 onClose: () => void;
757 commands: Command[];
758}) {
759 const navigate = useNavigate();
760 const [query, setQuery] = useState("");
761 const [selected, setSelected] = useState(0);
762 const input = useRef<HTMLInputElement>(null);
763 const matches = useMemo(() => {
764 const words = query.toLowerCase().split(/\s+/).filter(Boolean);
765 return commands
766 .filter((command) => {
767 const text = `${command.label} ${command.hint ?? ""}`.toLowerCase();
768 return words.every((word) => text.includes(word));
769 })
770 .slice(0, 12);
771 }, [commands, query]);
772
773 useEffect(() => {
774 if (!open) return;
775 setQuery("");
776 setSelected(0);
777 input.current?.focus();
778 }, [open]);
779 useEffect(() => setSelected(0), [query]);
780
781 if (!open) return null;
782 const go = (command: Command | undefined) => {
783 if (!command) return;
784 onClose();
785 navigate(command.to);
786 };
787 return (
788 <div className="fixed inset-0 z-50 flex items-start justify-center bg-black/50 px-4 pt-[12vh] backdrop-blur-sm" onClick={onClose}>
789 <div
790 role="dialog"
791 aria-label="Go to"
792 className="w-full max-w-xl overflow-hidden rounded-xl border border-line-strong bg-raised shadow-2xl shadow-black/60"
793 onClick={(event) => event.stopPropagation()}
794 >
795 <div className="flex items-center gap-3 border-b border-line px-4">
796 <Search size={16} className="text-faint" />
797 <input
798 ref={input}
799 value={query}
800 onChange={(event) => setQuery(event.target.value)}
801 onKeyDown={(event) => {
802 if (event.key === "ArrowDown") {
803 event.preventDefault();
804 setSelected((index) => Math.min(index + 1, matches.length - 1));
805 } else if (event.key === "ArrowUp") {
806 event.preventDefault();
807 setSelected((index) => Math.max(index - 1, 0));
808 } else if (event.key === "Enter") {
809 event.preventDefault();
810 go(matches[selected]);
811 } else if (event.key === "Escape") {
812 onClose();
813 }
814 }}
815 placeholder="Go to a repository, a page, or an action…"
816 autoComplete="off"
817 data-1p-ignore
818 className="h-12 grow border-0 bg-transparent text-sm shadow-none outline-none ring-0 placeholder:text-faint focus:outline-none focus-visible:outline-none"
819 />
820 <kbd className="rounded border border-line px-1.5 font-mono text-[0.6875rem] text-faint">esc</kbd>
821 </div>
822 <ul className="max-h-[50vh] overflow-y-auto p-1.5">
823 {matches.length === 0 && (
824 <li className="px-3 py-6 text-center text-sm text-faint">Nothing matches.</li>
825 )}
826 {matches.map((command, index) => (
827 <li key={`${command.to}-${command.label}`}>
828 <button
829 type="button"
830 onMouseEnter={() => setSelected(index)}
831 onClick={() => go(command)}
832 className={`flex w-full items-center gap-3 rounded-lg px-3 py-2 text-left text-sm ${
833 index === selected ? "bg-line text-fg" : "text-muted"
834 }`}
835 >
836 <span className="shrink-0 text-faint">{command.icon}</span>
837 <span className="min-w-0 grow truncate">{command.label}</span>
838 {command.hint && <span className="shrink-0 truncate text-xs text-faint">{command.hint}</span>}
839 {index === selected && <CornerDownLeft size={13} className="shrink-0 text-faint" />}
840 </button>
841 </li>
842 ))}
843 </ul>
844 </div>
845 </div>
846 );
847}
848
849/**
850 * A bar across the top of the page while the next one loads, as GitHub
851 * has: it appears at once, creeps towards the end while waiting, then fills
852 * and fades when the page arrives.
853 */
854export function Progress() {
855 const navigation = useNavigation();
856 const busy = navigation.state !== "idle";
857 const [width, setWidth] = useState(0);
858 const [visible, setVisible] = useState(false);
859 useEffect(() => {
860 if (busy) {
861 setVisible(true);
862 setWidth(12);
863 // Slows as it goes, never quite arriving until the page does.
864 const timer = setInterval(() => setWidth((now) => now + (90 - now) * 0.12), 200);
865 return () => clearInterval(timer);
866 }
867 setWidth((now) => (now > 0 ? 100 : 0));
868 const fade = setTimeout(() => setVisible(false), 250);
869 const reset = setTimeout(() => setWidth(0), 550);
870 return () => {
871 clearTimeout(fade);
872 clearTimeout(reset);
873 };
874 }, [busy]);
875 return (
876 <div
877 aria-hidden="true"
878 className={`pointer-events-none fixed inset-x-0 top-0 z-[60] h-[3px] transition-opacity duration-300 ${
879 visible ? "opacity-100" : "opacity-0"
880 }`}
881 >
882 <div
883 className="h-full bg-accent shadow-[0_0_10px_var(--color-accent)] transition-[width] duration-200 ease-out"
884 style={{ width: `${width}%` }}
885 />
886 </div>
887 );
888}
889
890/**
891 * The signed-in app: a sidebar with the workspace, its repositories and the
892 * sections of the one being looked at; a slim bar with search and the
893 * account; and the page.
894 */
895export function AppShell({
896 user,
897 shell,
898 banner,
899 children,
900}: {
901 user: User;
902 shell: ShellData;
903 banner?: ReactNode;
904 children: ReactNode;
905}) {
906 const submit = useSubmit();
907 const { pathname } = useLocation();
908 const [drawer, setDrawer] = useState(false);
909 const [palette, setPalette] = useState(false);
910 const commands = useMemo(() => commandsFor(user, shell), [user, shell]);
911
912 // A new page closes the drawer on small screens.
913 useEffect(() => setDrawer(false), [pathname]);
914 useEffect(() => {
915 const onKey = (event: KeyboardEvent) => {
916 if ((event.metaKey || event.ctrlKey) && event.key.toLowerCase() === "k") {
917 event.preventDefault();
918 setPalette((open) => !open);
919 }
920 };
921 window.addEventListener("keydown", onKey);
922 return () => window.removeEventListener("keydown", onKey);
923 }, []);
924
925 return (
926 <div className="min-h-screen">
927 <Progress />
928 <aside className="fixed inset-y-0 left-0 z-40 hidden w-64 border-r border-line bg-[color-mix(in_srgb,var(--color-surface)_70%,var(--color-bg))] lg:block">
929 <Sidebar user={user} shell={shell} onFind={() => setPalette(true)} />
930 </aside>
931 {drawer && (
932 <div className="fixed inset-0 z-50 lg:hidden">
933 <button
934 type="button"
935 aria-label="Close menu"
936 className="absolute inset-0 bg-black/60"
937 onClick={() => setDrawer(false)}
938 />
939 <aside className="absolute inset-y-0 left-0 w-72 border-r border-line bg-surface">
940 <button
941 type="button"
942 aria-label="Close menu"
943 onClick={() => setDrawer(false)}
944 className="absolute top-3 right-3 rounded-md p-1 text-faint hover:bg-raised hover:text-fg"
945 >
946 <X size={16} />
947 </button>
948 <Sidebar user={user} shell={shell} onFind={() => setPalette(true)} />
949 </aside>
950 </div>
951 )}
952
953 <div className="flex min-h-screen min-w-0 flex-col lg:pl-64">
954 <header className="sticky top-0 z-30 flex h-12 items-center gap-2 border-b border-line bg-bg/85 px-3 backdrop-blur sm:px-4">
955 <button
956 type="button"
957 aria-label="Open menu"
958 onClick={() => setDrawer(true)}
959 className="rounded-md p-1.5 text-muted hover:bg-raised hover:text-fg lg:hidden"
960 >
961 <Menu size={18} />
962 </button>
963 <Breadcrumbs pathname={pathname} />
964 <div className="ml-auto flex items-center gap-1.5">
965 <a
966 href="https://docs.g1t.sh/"
967 className="hidden rounded-md px-2 py-1 text-[0.8125rem] text-muted transition-colors hover:bg-raised hover:text-fg sm:block"
968 >
969 Docs
970 </a>
971 <DropdownMenu>
972 <DropdownMenuTrigger
973 aria-label="Create"
974 className="flex h-8 items-center gap-1.5 rounded-md bg-fg px-2.5 text-[0.8125rem] font-medium text-bg outline-none transition-colors hover:bg-white"
975 >
976 <Plus size={14} />
977 New
978 </DropdownMenuTrigger>
979 <DropdownMenuContent align="end">
980 {shell.repo && (
981 <DropdownMenuItem asChild>
982 <Link to={`/${shell.repo.namespace}/${shell.repo.name}/issues/new`}>
983 <CircleDot />
984 New issue
985 </Link>
986 </DropdownMenuItem>
987 )}
988 {shell.repo?.member && (
989 <DropdownMenuItem asChild>
990 <Link to={`/${shell.repo.namespace}/${shell.repo.name}/plans`}>
991 <ListTree />
992 Plan work
993 </Link>
994 </DropdownMenuItem>
995 )}
996 <DropdownMenuItem asChild>
997 <Link to="/new">
998 <Box />
999 New project
1000 </Link>
1001 </DropdownMenuItem>
1002 <DropdownMenuItem asChild>
1003 <Link to="/workspaces/new">
1004 <Users />
1005 New workspace
1006 </Link>
1007 </DropdownMenuItem>
1008 </DropdownMenuContent>
1009 </DropdownMenu>
1010 </div>
1011 </header>
1012 {banner}
1013 <main className="min-w-0 grow">{children}</main>
1014 </div>
1015 <CommandPalette open={palette} onClose={() => setPalette(false)} commands={commands} />
1016 </div>
1017 );
1018}