Skip to content
refs
History
374 lines · 15.6 KBRaw
1import { Dialog as BaseDialog } from "@base-ui/react/dialog";
2import { RobotIcon, BuildingsIcon, CaretUpDownIcon, ChartLineUpIcon, CheckSquareIcon, GitBranchIcon, HouseIcon, KeyIcon, ListIcon, LockIcon, PlusIcon, SidebarIcon, SignOutIcon, TagIcon, UserIcon, UsersIcon, XIcon, type Icon } from "../icons";
3import type { NamespaceRef, Person } from "@refs/backend/contract";
4import { useEffect, useState, type ReactNode } from "react";
5import { Link } from "../provider";
6import { Button, LinkButton } from "../ui/button";
7import { cn } from "../ui/cn";
8import { Banner } from "../ui/feedback";
9import { ThemeChoices, ThemeMenu, useRailHidden } from "../theme";
10import { Tooltip } from "../ui/overlay";
11import { AuthGraphic } from "./auth-graphic";
12import { DropdownMenu } from "../ui/overlay";
13import { Avatar, LogoMark, Wordmark } from "./brand";
14
15export interface Crumb {
16 label: string;
17 href: string;
18}
19
20export interface ShellState {
21 org: string;
22 namespace: string | null;
23 namespaces: readonly NamespaceRef[];
24 user: Person | null;
25 showMembers: boolean;
26 showSecrets: boolean;
27 showLabels: boolean;
28 showAdmin: boolean;
29 pathname: string;
30 onSignOut: () => void;
31 onSwitchNamespace: (slug: string) => void;
32}
33
34
35/** The shared page column: the header, the tabs and the content all align to it, at the full width of every page. */
36const column = "w-full px-4 sm:px-6";
37
38function RailLink({ href, icon: Glyph, active, children }: { href: string; icon: Icon; active: boolean; children: ReactNode }) {
39 return (
40 <Link
41 href={href}
42 aria-current={active ? "page" : undefined}
43 className={cn(
44 "flex h-8 items-center gap-2.5 rounded-control px-2 text-compact font-medium transition-colors duration-150 pointer-coarse:h-10",
45 active ? "bg-hover text-ink" : "text-muted hover:bg-hover hover:text-ink",
46 )}
47 >
48 <Glyph size={16} weight={active ? "bold" : "regular"} className={active ? "text-ink" : "text-faint"} />
49 {children}
50 </Link>
51 );
52}
53
54function NamespaceSwitcher({ current, label, namespaces, onSwitch }: { current: string | null; label: string; namespaces: readonly NamespaceRef[]; onSwitch: (slug: string) => void }) {
55 const kind = namespaces.find((namespace) => namespace.slug === current)?.kind;
56 const Glyph = kind === "org" ? BuildingsIcon : UserIcon;
57 const face = (
58 <span className="flex min-w-0 flex-1 items-center gap-2.5">
59 <span className="flex size-7 shrink-0 items-center justify-center rounded-chip bg-control text-muted shadow-control">
60 <Glyph size={14} />
61 </span>
62 <span className="flex min-w-0 flex-col text-left leading-tight">
63 <span className="truncate text-compact font-semibold text-ink">{label}</span>
64 <span className="text-xs text-faint">{kind === "org" ? "Organization" : "Personal"}</span>
65 </span>
66 </span>
67 );
68 if (namespaces.length < 2) return <div className="flex items-center px-1.5 py-1.5">{face}</div>;
69 return (
70 <DropdownMenu>
71 <DropdownMenu.Trigger aria-label="Switch account or organization" className="flex w-full cursor-pointer items-center gap-2 rounded-control px-1.5 py-1.5 transition-colors duration-150 hover:bg-hover data-[popup-open]:bg-hover">
72 {face}
73 <CaretUpDownIcon size={14} className="shrink-0 text-faint" />
74 </DropdownMenu.Trigger>
75 <DropdownMenu.Content align="start" className="min-w-60">
76 {namespaces.map((namespace) => (
77 <DropdownMenu.Item key={namespace.slug} icon={namespace.kind === "org" ? <BuildingsIcon /> : <UserIcon />} onClick={() => onSwitch(namespace.slug)}>
78 <span className={cn("flex-1", namespace.slug === current && "font-semibold")}>{namespace.name}</span>
79 <span className="text-xs text-faint">{namespace.kind === "user" ? "Personal" : "Organization"}</span>
80 </DropdownMenu.Item>
81 ))}
82 <DropdownMenu.Separator />
83 <DropdownMenu.LinkItem href="/organizations/new" icon={<PlusIcon />}>
84 New organization
85 </DropdownMenu.LinkItem>
86 </DropdownMenu.Content>
87 </DropdownMenu>
88 );
89}
90
91function Rail({ state }: { state: ShellState }) {
92 const { pathname, namespace } = state;
93 const home = namespace ? `/${namespace}` : "/";
94 const inTasks = pathname === "/tasks" || pathname === "/tasks/new" || /^\/[^/]+\/[^/]+\/tasks(\/|$)/.test(pathname);
95 const inOverview = pathname === "/" || pathname === home;
96 return (
97 <div className="flex h-full flex-col gap-4 px-3 pt-3.5 pb-4">
98 <div className="flex h-7 items-center px-1.5">
99 <Wordmark />
100 </div>
101 <NamespaceSwitcher current={namespace} label={state.org} namespaces={state.namespaces} onSwitch={state.onSwitchNamespace} />
102 <nav aria-label="Main" className="flex flex-col gap-px">
103 <RailLink href={home} icon={HouseIcon} active={inOverview}>
104 Overview
105 </RailLink>
106 <RailLink href="/tasks" icon={CheckSquareIcon} active={inTasks}>
107 Tasks
108 </RailLink>
109 </nav>
110 <nav aria-label="Settings" className="flex flex-col gap-px">
111 <span className="caption px-2 pb-1.5 text-faint">Settings</span>
112 <RailLink href="/settings/tokens" icon={KeyIcon} active={pathname === "/settings/tokens"}>
113 Access tokens
114 </RailLink>
115 {state.showMembers && (
116 <RailLink href="/settings/members" icon={UsersIcon} active={pathname === "/settings/members"}>
117 Members
118 </RailLink>
119 )}
120 {state.showSecrets && (
121 <RailLink href="/settings/secrets" icon={LockIcon} active={pathname === "/settings/secrets"}>
122 CI secrets
123 </RailLink>
124 )}
125 {state.showSecrets && (
126 <RailLink href="/settings/agents" icon={RobotIcon} active={pathname === "/settings/agents"}>
127 Agents
128 </RailLink>
129 )}
130 {state.showLabels && (
131 <RailLink href="/tasks/labels" icon={TagIcon} active={pathname === "/tasks/labels"}>
132 Labels
133 </RailLink>
134 )}
135 {state.showAdmin && (
136 <RailLink href="/admin" icon={ChartLineUpIcon} active={pathname === "/admin"}>
137 Admin
138 </RailLink>
139 )}
140 </nav>
141 </div>
142 );
143}
144
145function MobileRail({ state }: { state: ShellState }) {
146 const [open, setOpen] = useState(false);
147 useEffect(() => setOpen(false), [state.pathname]);
148 return (
149 <BaseDialog.Root open={open} onOpenChange={setOpen}>
150 <BaseDialog.Trigger render={<Button variant="ghost" shape="square" aria-label="Open navigation" className="hit-area -ml-1.5 md:hidden" />}>
151 <ListIcon size={18} />
152 </BaseDialog.Trigger>
153 <BaseDialog.Portal>
154 <BaseDialog.Backdrop className="backdrop fixed inset-0 z-50" />
155 <BaseDialog.Popup className="fixed inset-y-0 left-0 z-50 w-[min(18rem,85vw)] bg-rail shadow-surface outline-none transition-[translate] duration-300 ease-drawer data-[ending-style]:-translate-x-full data-[starting-style]:-translate-x-full motion-reduce:transition-none">
156 <BaseDialog.Close render={<Button variant="ghost" shape="square" aria-label="Close navigation" className="hit-area absolute top-3 right-3" />}>
157 <XIcon size={16} />
158 </BaseDialog.Close>
159 <Rail state={state} />
160 </BaseDialog.Popup>
161 </BaseDialog.Portal>
162 </BaseDialog.Root>
163 );
164}
165
166/** Lets a keyboard user jump past the rail and the header. It shows only when it has focus. */
167function SkipLink() {
168 return (
169 <a href="#content" className="sr-only rounded-control bg-primary px-3 py-2 text-compact font-medium text-on-primary shadow-surface focus:not-sr-only focus:fixed focus:top-2 focus:left-2 focus:z-50">
170 Skip to content
171 </a>
172 );
173}
174
175/** Hides or shows the rail on wide screens, like the sidebar button of a desktop app. Narrow screens use the drawer. */
176function RailToggle() {
177 const [hidden, toggle] = useRailHidden();
178 const label = hidden ? "Show sidebar" : "Hide sidebar";
179 return (
180 <Tooltip content={label} side="bottom" render={<Button variant="ghost" shape="square" aria-label={label} aria-pressed={!hidden} onClick={toggle} className="hit-area -ml-1.5 max-md:hidden" />}>
181 <SidebarIcon size={16} />
182 </Tooltip>
183 );
184}
185
186function UserMenu({ user, showMembers, showAdmin, onSignOut }: { user: Person; showMembers: boolean; showAdmin: boolean; onSignOut: () => void }) {
187 return (
188 <DropdownMenu>
189 <DropdownMenu.Trigger aria-label={`Account menu for ${user.name}`} className="pressable hit-area flex cursor-pointer rounded-full">
190 <Avatar name={user.name} size="base" />
191 </DropdownMenu.Trigger>
192 <DropdownMenu.Content align="end" className="min-w-60">
193 <div className="flex items-center gap-2.5 px-2 py-2">
194 <Avatar name={user.name} size="base" />
195 <span className="flex min-w-0 flex-col leading-tight">
196 <span className="truncate text-compact font-medium">{user.name}</span>
197 <span className="truncate text-xs text-muted">{user.email}</span>
198 </span>
199 </div>
200 <DropdownMenu.Separator />
201 <DropdownMenu.LinkItem href="/settings/tokens" icon={<KeyIcon />}>
202 Access tokens
203 </DropdownMenu.LinkItem>
204 {showMembers && (
205 <DropdownMenu.LinkItem href="/settings/members" icon={<UsersIcon />}>
206 Members
207 </DropdownMenu.LinkItem>
208 )}
209 {showAdmin && (
210 <DropdownMenu.LinkItem href="/admin" icon={<ChartLineUpIcon />}>
211 Admin
212 </DropdownMenu.LinkItem>
213 )}
214 <DropdownMenu.Separator />
215 <ThemeChoices />
216 <DropdownMenu.Separator />
217 <DropdownMenu.Item icon={<SignOutIcon />} onClick={onSignOut}>
218 Sign out
219 </DropdownMenu.Item>
220 </DropdownMenu.Content>
221 </DropdownMenu>
222 );
223}
224
225function CreateMenu() {
226 return (
227 <DropdownMenu>
228 <DropdownMenu.Trigger render={<Button variant="secondary" size="sm" icon={<PlusIcon />} aria-label="Create new" />}>New</DropdownMenu.Trigger>
229 <DropdownMenu.Content align="end" className="min-w-52">
230 <DropdownMenu.LinkItem href="/new" icon={<GitBranchIcon />}>
231 New repository
232 </DropdownMenu.LinkItem>
233 <DropdownMenu.LinkItem href="/tasks/new" icon={<CheckSquareIcon />}>
234 New task
235 </DropdownMenu.LinkItem>
236 <DropdownMenu.LinkItem href="/organizations/new" icon={<BuildingsIcon />}>
237 New organization
238 </DropdownMenu.LinkItem>
239 </DropdownMenu.Content>
240 </DropdownMenu>
241 );
242}
243
244function Crumbs({ trail }: { trail: Crumb[] }) {
245 return (
246 <nav aria-label="Location" className="flex min-w-0 items-center gap-1.5 text-compact">
247 {trail.map((crumb, i) => (
248 <span key={crumb.href} className={cn("min-w-0 items-center gap-1.5", i < trail.length - 2 ? "hidden sm:flex" : "flex", i === trail.length - 2 && "max-sm:[&>span:first-child]:hidden")}>
249 {i > 0 && (
250 <span aria-hidden className="text-faint/60">
251 /
252 </span>
253 )}
254 <Link href={crumb.href} aria-current={i === trail.length - 1 ? "page" : undefined} className={cn("truncate rounded-chip hover:text-ink", i === trail.length - 1 ? "font-medium text-ink" : "text-muted")}>
255 {crumb.label}
256 </Link>
257 </span>
258 ))}
259 </nav>
260 );
261}
262
263/**
264 * The header floats over the page as a translucent material. Its hairline is a scroll edge when the header
265 * stands alone, and a fixed divider when it carries tabs.
266 */
267function Header({ subnav, children }: { subnav?: ReactNode; children: ReactNode }) {
268 return (
269 <header className={cn("material-chrome sticky top-0 z-20", subnav ? "shadow-[inset_0_-1px_0_var(--hairline)]" : "scroll-edge")}>
270 <div className={cn(column, "flex h-13 items-center gap-3")}>{children}</div>
271 {subnav && <div className={column}>{subnav}</div>}
272 </header>
273 );
274}
275
276/**
277 * The signed-in frame: a quiet rail on the left, the floating header with location and actions, then the page.
278 * `full` removes the bottom space, for a canvas such as the code map.
279 */
280export function AppShell({ state, crumbs = [], subnav, full = false, children }: { state: ShellState; crumbs?: Crumb[]; subnav?: ReactNode; full?: boolean; children: ReactNode }) {
281 const { user } = state;
282 const trail: Crumb[] = [...(state.org ? [{ label: state.org, href: state.namespace ? `/${state.namespace}` : "/" }] : []), ...crumbs];
283 if (!user) {
284 return (
285 <>
286 <SkipLink />
287 <Header subnav={subnav}>
288 <Wordmark />
289 {trail.length > 0 && <span aria-hidden className="h-4 w-px bg-line" />}
290 <Crumbs trail={trail} />
291 <div className="ml-auto flex items-center gap-2">
292 <LinkButton variant="ghost" size="sm" href="/docs">
293 Docs
294 </LinkButton>
295 <ThemeMenu />
296 <LinkButton variant="secondary" size="sm" href="/login">
297 Sign in
298 </LinkButton>
299 </div>
300 </Header>
301 <main id="content" tabIndex={-1} className={cn(column, "overflow-x-clip outline-none", full ? "pt-3 pb-0" : "py-8")}>
302 {children}
303 </main>
304 </>
305 );
306 }
307 return (
308 <div className="flex min-h-svh">
309 <SkipLink />
310 <aside className="rail sticky top-0 hidden h-svh w-[240px] shrink-0 overflow-y-auto bg-rail shadow-[inset_-1px_0_0_var(--hairline)] md:block">
311 <Rail state={state} />
312 </aside>
313 <div className="flex min-w-0 flex-1 flex-col">
314 <Header subnav={subnav}>
315 <MobileRail state={state} />
316 <RailToggle />
317 <span className="md:hidden">
318 <LogoMark className="size-5 text-ink" />
319 </span>
320 <span className="rail-hidden-only">
321 <LogoMark className="size-5 text-ink" />
322 </span>
323 <Crumbs trail={trail} />
324 <div className="ml-auto flex items-center gap-3">
325 <LinkButton variant="ghost" size="sm" href="/docs">
326 Docs
327 </LinkButton>
328 <CreateMenu />
329 <UserMenu user={user} showMembers={state.showMembers} showAdmin={state.showAdmin} onSignOut={state.onSignOut} />
330 </div>
331 </Header>
332 <main id="content" tabIndex={-1} className={cn(column, "overflow-x-clip outline-none", full ? "pt-3 pb-0" : "pt-8 pb-16")}>
333 {children}
334 </main>
335 </div>
336 </div>
337 );
338}
339
340export function PageHeader({ title, description, actions }: { title: ReactNode; description?: ReactNode; actions?: ReactNode }) {
341 return (
342 <div className="mb-8 flex flex-wrap items-end justify-between gap-4">
343 <div className="flex min-w-0 flex-col gap-1">
344 <h1 className="m-0 text-title leading-[1.2] font-semibold">{title}</h1>
345 {description && <p className="m-0 max-w-[70ch] text-base text-muted">{description}</p>}
346 </div>
347 {actions && <div className="flex items-center gap-2">{actions}</div>}
348 </div>
349 );
350}
351
352/** A sign-in or sign-up page: the form on one side, the product at work on the other. */
353export function AuthPage({ subtitle, error, children }: { subtitle: string; error?: string; children: ReactNode }) {
354 return (
355 <main className="grid min-h-svh lg:grid-cols-[minmax(0,1fr)_minmax(0,1.1fr)]">
356 <div className="flex min-w-0 flex-col px-5 py-5 sm:px-10">
357 <div className="flex h-8 items-center justify-between">
358 <Wordmark />
359 <ThemeMenu />
360 </div>
361 <div className="flex flex-1 items-center justify-center py-10">
362 <div className="flex w-full max-w-[360px] flex-col gap-6">
363 <h1 className="m-0 text-title leading-[1.2] font-semibold">{subtitle}</h1>
364 {error && <Banner variant="error" description={error} />}
365 {children}
366 </div>
367 </div>
368 </div>
369 <div className="hidden p-3 pl-0 lg:block">
370 <AuthGraphic />
371 </div>
372 </main>
373 );
374}