Skip to content
refs
418 lines · 17.4 KBRaw
1import { Button, LinkButton } from "../ui/button";
2import { Badge, ClipboardText } from "../ui/feedback";
3import { Breadcrumbs, TabCount, Tabs } from "../ui/navigation";
4import { DropdownMenu, Popover, Tooltip } from "../ui/overlay";
5import { Empty } from "./empty";
6import { LayerCard } from "./surface";
7import {
8 ArrowClockwiseIcon,
9 BookOpenIcon,
10 CalendarIcon,
11 CaretDownIcon,
12 ClockCounterClockwiseIcon,
13 CubeIcon,
14 FileIcon,
15 FolderIcon,
16 GitBranchIcon,
17 GitForkIcon,
18 HourglassIcon,
19 TagIcon,
20} from "../icons";
21import { isCommitHash, repoLink, type CommitCheck, type CommitSummary, type LastChanges, type OnlineAgent, type RefList, type RepoSummary, type Tree, type TreeEntry } from "@refs/backend/contract";
22import { formatDate } from "../format";
23import { Link } from "../provider";
24import { AgentAvatar, Avatar } from "./brand";
25import { Ago } from "./ago";
26
27export type RepoTab = "code" | "map" | "tasks" | "pulls" | "agents" | "runs" | "settings";
28
29export function RepoTabs({ repo, current, signedIn }: { repo: RepoSummary; current: RepoTab; signedIn: boolean }) {
30 const hrefs: Record<RepoTab, string> = { code: `/${repo.name}`, map: `/${repo.name}/map`, tasks: `/${repo.name}/tasks`, pulls: `/${repo.name}/pulls`, agents: `/${repo.name}/agents`, runs: `/${repo.name}/runs`, settings: `/${repo.name}/settings` };
31 const tab = (value: RepoTab, label: string, count?: number) => ({
32 value,
33 label: (
34 <span className="flex items-center gap-1.5">
35 {label}
36 {count ? <TabCount>{count}</TabCount> : null}
37 </span>
38 ),
39 nativeButton: false,
40 render: (props: React.AnchorHTMLAttributes<HTMLAnchorElement>) => <Link {...props} href={hrefs[value]} />,
41 });
42 const tabs = [
43 tab("code", "Code"),
44 tab("map", "Map"),
45 ...(signedIn ? [tab("tasks", "Tasks", repo.openTasks), tab("pulls", "Pull requests", repo.openPulls)] : []),
46 ...(signedIn && repo.can.write ? [tab("agents", "Agents")] : []),
47 tab("runs", "Runs"),
48 ...(repo.can.admin ? [tab("settings", "Settings")] : []),
49 ];
50 return <Tabs variant="underline" listClassName="shadow-none" value={current} tabs={tabs} />;
51}
52
53function MetaItem({ icon, children }: { icon: React.ReactNode; children: React.ReactNode }) {
54 return (
55 <li className="flex min-w-0 items-center gap-1.5">
56 <span className="flex shrink-0 text-faint">{icon}</span>
57 <span className="min-w-0 truncate">{children}</span>
58 </li>
59 );
60}
61
62export function RepoTitle({ repo, signedIn, agents }: { repo: RepoSummary; signedIn: boolean; agents: readonly OnlineAgent[] }) {
63 const slash = repo.name.indexOf("/");
64 return (
65 <header className="mb-6 flex flex-col gap-3">
66 <div className="flex flex-wrap items-center justify-between gap-4">
67 <div className="flex min-w-0 items-center gap-3">
68 <h1 className="m-0 min-w-0 truncate text-heading leading-tight font-semibold tracking-[-0.02em]">
69 <span className="font-normal text-muted">{repo.name.slice(0, slash + 1)}</span>
70 <Link href={`/${repo.name}`} className="text-ink hover:text-accent-text">
71 {repo.name.slice(slash + 1)}
72 </Link>
73 </h1>
74 <Badge variant="outline">{repo.visibility === "public" ? "Public" : "Private"}</Badge>
75 </div>
76 {signedIn && (
77 <LinkButton variant="secondary" href={`/${repo.name}/fork`} icon={<GitForkIcon />}>
78 Fork <TabCount>{repo.forks}</TabCount>
79 </LinkButton>
80 )}
81 </div>
82 {repo.description && <p className="m-0 max-w-[74ch] text-callout text-muted text-pretty">{repo.description}</p>}
83 <ul className="m-0 flex list-none flex-wrap items-center gap-x-5 gap-y-1.5 p-0 text-compact text-muted tabular-nums">
84 {repo.origin?.kind === "fork" && repo.origin.repo && (
85 <MetaItem icon={<GitForkIcon size={14} />}>
86 Forked from{" "}
87 <Link href={`/${repo.origin.repo}`} className="text-accent-text">
88 {repo.origin.repo}
89 </Link>
90 </MetaItem>
91 )}
92 {repo.origin?.kind === "import" && (
93 <MetaItem icon={<CubeIcon size={14} />}>
94 Imported from <span className="font-mono text-xs">{repo.origin.url.replace(/^https:\/\//, "")}</span>
95 </MetaItem>
96 )}
97 <MetaItem icon={<CalendarIcon size={14} />}>Created {formatDate(repo.createdAt)}</MetaItem>
98 {agents.length === 0 && <MetaItem icon={<span className="size-3 rounded-full ring-[1.5px] ring-current ring-inset" />}>No agents online</MetaItem>}
99 </ul>
100 </header>
101 );
102}
103
104export function AgentsOnline({ repo, agents }: { repo: string; agents: readonly OnlineAgent[] }) {
105 return (
106 <LayerCard>
107 <LayerCard.Secondary>
108 Agents online <span className="font-normal text-faint tabular-nums">{agents.length}</span>
109 </LayerCard.Secondary>
110 {agents.length === 0 ? (
111 <p className="m-0 px-4 py-3 text-compact text-muted">No agents are connected right now.</p>
112 ) : (
113 <ul className="m-0 list-none p-0 text-compact">
114 {agents.map((agent) => (
115 <li key={agent.session} className="flex items-center gap-2.5 px-4 py-2.5 shadow-[inset_0_-1px_0_var(--hairline)] last:shadow-none">
116 <AgentAvatar name={agent.runtime} />
117 <div className="flex min-w-0 flex-col">
118 <span className="truncate">
119 <span className="font-medium">{agent.runtime}</span> <span className="text-muted">for {agent.onBehalfOf.name}</span>
120 </span>
121 <span className="truncate text-muted">
122 {agent.branch ? <span className="font-mono text-xs">{agent.branch}</span> : "idle"}
123 </span>
124 </div>
125 </li>
126 ))}
127 </ul>
128 )}
129 </LayerCard>
130 );
131}
132
133function refLabel(ref: string): string {
134 return isCommitHash(ref) ? ref.slice(0, 7) : ref;
135}
136
137export function BranchPicker({ repo, refs, current }: { repo: string; refs: RefList; current: string }) {
138 return (
139 <DropdownMenu>
140 <DropdownMenu.Trigger
141 render={
142 <Button variant="secondary" icon={<GitBranchIcon />}>
143 <span className="max-w-48 truncate font-mono text-sm">{refLabel(current)}</span>
144 <CaretDownIcon size={12} />
145 </Button>
146 }
147 />
148 <DropdownMenu.Content align="start" className="max-h-96 min-w-64 overflow-y-auto">
149 <DropdownMenu.Label>Branches</DropdownMenu.Label>
150 {refs.branches.map((name) => (
151 <DropdownMenu.LinkItem key={`b:${name}`} href={repoLink(repo, "tree", name)} icon={<GitBranchIcon />}>
152 <span className={`flex flex-1 items-center justify-between gap-3 font-mono text-sm ${name === current ? "font-semibold" : ""}`}>
153 {name}
154 {name === refs.defaultRef && <Badge variant="secondary">default</Badge>}
155 </span>
156 </DropdownMenu.LinkItem>
157 ))}
158 {refs.tags.length > 0 && (
159 <>
160 <DropdownMenu.Separator />
161 <DropdownMenu.Label>Tags</DropdownMenu.Label>
162 {refs.tags.map((name) => (
163 <DropdownMenu.LinkItem key={`t:${name}`} href={repoLink(repo, "tree", name)} icon={<TagIcon />}>
164 <span className={`font-mono text-sm ${name === current ? "font-semibold" : ""}`}>{name}</span>
165 </DropdownMenu.LinkItem>
166 ))}
167 </>
168 )}
169 <DropdownMenu.Separator />
170 <DropdownMenu.LinkItem href={`/${repo}/branches`}>View all branches</DropdownMenu.LinkItem>
171 </DropdownMenu.Content>
172 </DropdownMenu>
173 );
174}
175
176export function PathBreadcrumbs({ repo, current, path }: { repo: string; current: string; path: readonly string[] }) {
177 return (
178 <Breadcrumbs className="min-w-0">
179 <Breadcrumbs.Link href={repoLink(repo, "tree", current)}>{repo}</Breadcrumbs.Link>
180 {path.map((segment, i) => (
181 <span key={i} className="contents">
182 <Breadcrumbs.Separator />
183 {i === path.length - 1 ? (
184 <Breadcrumbs.Current>{segment}</Breadcrumbs.Current>
185 ) : (
186 <Breadcrumbs.Link href={repoLink(repo, "tree", current, path.slice(0, i + 1))}>{segment}</Breadcrumbs.Link>
187 )}
188 </span>
189 ))}
190 </Breadcrumbs>
191 );
192}
193
194export function CloneMenu({ url }: { url: string }) {
195 return (
196 <Popover>
197 <Popover.Trigger
198 render={
199 <Button variant="primary">
200 Code <CaretDownIcon size={12} />
201 </Button>
202 }
203 />
204 <Popover.Content align="end" className="w-[420px] max-w-[calc(100vw-32px)]">
205 <Popover.Title>Clone with HTTPS</Popover.Title>
206 <div className="my-3">
207 <ClipboardText text={url} size="base" />
208 </div>
209 <Popover.Description>
210 Use an{" "}
211 <Link href="/settings/tokens" className="text-accent-text">
212 access token
213 </Link>{" "}
214 as the password.
215 </Popover.Description>
216 </Popover.Content>
217 </Popover>
218 );
219}
220
221const CHECK_DOT: Record<CommitCheck["state"], string> = { succeeded: "bg-success", running: "bg-needs", failed: "bg-danger" };
222
223function CheckDot({ check }: { check: CommitCheck }) {
224 return (
225 <Tooltip content={check.label} render={<Link href={check.href} aria-label={check.label} />} className="hit-area relative inline-flex size-2 shrink-0 rounded-full">
226 <span className={`size-2 rounded-full ${CHECK_DOT[check.state]}`} />
227 </Tooltip>
228 );
229}
230
231export function CommitBar({ repo, current, commit, now, check }: { repo: string; current: string; commit: CommitSummary; now: number; check?: CommitCheck | null }) {
232 const agent = commit.pushedBy?.actor.kind === "agent";
233 return (
234 <div className="flex w-full min-w-0 items-center justify-between gap-4 font-normal">
235 <div className="flex min-w-0 items-center gap-2.5">
236 {agent ? <AgentAvatar name={commit.author} /> : <Avatar name={commit.author} />}
237 <span className="shrink-0 font-semibold text-ink">{commit.author}</span>
238 <Link href={repoLink(repo, "tree", commit.hash)} className="truncate text-muted hover:text-accent-text">
239 {commit.title}
240 </Link>
241 </div>
242 <div className="flex shrink-0 items-center gap-4 text-compact text-muted">
243 {check && <CheckDot check={check} />}
244 <span className="hidden sm:inline">
245 <Link href={repoLink(repo, "tree", commit.hash)} className="font-mono text-xs hover:text-accent-text">
246 {commit.hash.slice(0, 7)}
247 </Link>
248 <span aria-hidden="true"> · </span>
249 <Ago at={commit.committedAt} now={now} />
250 </span>
251 <Link href={repoLink(repo, "commits", current)} className="flex items-center gap-1.5 font-medium text-ink hover:text-accent-text">
252 <ClockCounterClockwiseIcon size={14} /> History
253 </Link>
254 </div>
255 </div>
256 );
257}
258
259function EntryIcon({ type }: { type: TreeEntry["type"] }) {
260 if (type === "tree") return <FolderIcon size={16} weight="fill" className="shrink-0 text-faint" />;
261 if (type === "gitlink") return <CubeIcon size={16} className="shrink-0 text-muted" />;
262 return <FileIcon size={16} className="shrink-0 text-faint" />;
263}
264
265const ROW = "grid grid-cols-[minmax(0,1fr)_auto] items-center gap-4 px-4 py-2 text-compact shadow-[inset_0_-1px_0_var(--hairline)] last:shadow-none sm:grid-cols-[minmax(0,2fr)_minmax(0,3fr)_7rem] hover:bg-hover transition-colors duration-150";
266
267export function FileList({ repo, current, path, tree, now, last, check }: { repo: string; current: string; path: readonly string[]; tree: Tree; now: number; last?: LastChanges; check?: CommitCheck | null }) {
268 return (
269 <LayerCard>
270 <LayerCard.Secondary className="py-2">
271 <CommitBar repo={repo} current={current} commit={tree.commit} now={now} check={check} />
272 </LayerCard.Secondary>
273 <ul className="m-0 list-none p-0">
274 {path.length > 0 && (
275 <li className={ROW}>
276 <Link href={repoLink(repo, "tree", current, path.slice(0, -1))} className="flex items-center gap-3 text-muted hover:text-accent-text">
277 <FolderIcon size={16} className="shrink-0" /> ..
278 </Link>
279 </li>
280 )}
281 {tree.entries.map((entry) => {
282 const change = last?.changes[entry.name];
283 return (
284 <li key={entry.name} className={ROW}>
285 {entry.type === "gitlink" ? (
286 <span className="flex min-w-0 items-center gap-3">
287 <EntryIcon type={entry.type} /> <span className="truncate">{entry.name}</span>
288 </span>
289 ) : (
290 <Link href={repoLink(repo, entry.type === "tree" ? "tree" : "blob", current, [...path, entry.name])} className="flex min-w-0 items-center gap-3 text-ink hover:text-accent-text">
291 <EntryIcon type={entry.type} /> <span className="truncate">{entry.name}</span>
292 </Link>
293 )}
294 <span className="hidden min-w-0 truncate text-muted sm:block">
295 {change ? (
296 <Link href={repoLink(repo, "tree", change.hash)} className="arrive hover:text-accent-text">
297 {change.title}
298 </Link>
299 ) : last ? null : (
300 <span className="shimmer inline-block h-2.5 w-40 rounded-full align-middle" />
301 )}
302 </span>
303 <span className="text-right whitespace-nowrap text-muted">{change ? <span className="arrive"><Ago at={change.committedAt} now={last.asOf} /></span> : null}</span>
304 </li>
305 );
306 })}
307 </ul>
308 </LayerCard>
309 );
310}
311
312export function Readme({ readme }: { readme: NonNullable<Tree["readme"]> }) {
313 return (
314 <LayerCard>
315 <LayerCard.Secondary className="flex items-center gap-2">
316 <BookOpenIcon size={16} /> {readme.name}
317 </LayerCard.Secondary>
318 <LayerCard.Primary className="px-6 py-5 sm:px-8 sm:py-6">
319 <article className="markdown" dangerouslySetInnerHTML={{ __html: readme.html }} />
320 </LayerCard.Primary>
321 </LayerCard>
322 );
323}
324
325export function TreeView({
326 repo,
327 cloneUrl,
328 refs,
329 current,
330 path,
331 tree,
332 now,
333 last,
334 check,
335}: {
336 repo: string;
337 cloneUrl: string;
338 refs: RefList;
339 current: string;
340 path: readonly string[];
341 tree: Tree;
342 now: number;
343 last?: LastChanges;
344 check?: CommitCheck | null;
345}) {
346 const plural = (count: number, one: string, many: string) => `${count} ${count === 1 ? one : many}`;
347 return (
348 <div className="flex min-w-0 flex-col gap-4">
349 <div className="flex flex-wrap items-center justify-between gap-3">
350 <div className="flex min-w-0 items-center gap-4">
351 <BranchPicker repo={repo} refs={refs} current={current} />
352 {path.length > 0 ? (
353 <PathBreadcrumbs repo={repo} current={current} path={path} />
354 ) : (
355 <div className="hidden items-center gap-4 text-compact text-muted tabular-nums md:flex">
356 <Link href={`/${repo}/branches`} className="flex items-center gap-1.5 rounded-chip hover:text-ink">
357 <GitBranchIcon size={16} /> <span className="font-semibold text-ink">{refs.branches.length}</span> {plural(refs.branches.length, "branch", "branches").replace(/^\d+ /, "")}
358 </Link>
359 <span className="flex items-center gap-1.5">
360 <TagIcon size={16} /> <span className="font-semibold text-ink">{refs.tags.length}</span> {plural(refs.tags.length, "tag", "tags").replace(/^\d+ /, "")}
361 </span>
362 </div>
363 )}
364 </div>
365 <CloneMenu url={cloneUrl} />
366 </div>
367 <FileList repo={repo} current={current} path={path} tree={tree} now={now} last={last} check={check} />
368 {tree.readme && <Readme readme={tree.readme} />}
369 </div>
370 );
371}
372
373export function QuickSetup({ repo, cloneUrl }: { repo: string; cloneUrl: string }) {
374 return (
375 <LayerCard>
376 <LayerCard.Secondary>Quick setup</LayerCard.Secondary>
377 <LayerCard.Primary className="gap-5 p-5">
378 <ClipboardText text={cloneUrl} size="base" />
379 <section className="flex flex-col gap-2">
380 <h3 className="m-0 text-base font-semibold">Create a new repository on the command line</h3>
381 <pre className="m-0 overflow-x-auto rounded-control bg-sunken px-4 py-3 font-mono text-compact leading-relaxed shadow-[inset_0_0_0_1px_var(--hairline)]">{`echo "# ${repo}" >> README.md
382git init -b main
383git add README.md
384git commit -m "first commit"
385git remote add origin ${cloneUrl}
386git push -u origin main`}</pre>
387 </section>
388 <section className="flex flex-col gap-2">
389 <h3 className="m-0 text-base font-semibold">Push an existing repository from the command line</h3>
390 <pre className="m-0 overflow-x-auto rounded-control bg-sunken px-4 py-3 font-mono text-compact leading-relaxed shadow-[inset_0_0_0_1px_var(--hairline)]">{`git remote add origin ${cloneUrl}
391git push -u origin main`}</pre>
392 </section>
393 <p className="m-0 text-sm text-muted">
394 Use an{" "}
395 <Link href="/settings/tokens" className="text-accent-text">
396 access token
397 </Link>{" "}
398 as the password when Git asks.
399 </p>
400 </LayerCard.Primary>
401 </LayerCard>
402 );
403}
404
405export function Preparing({ repo }: { repo: string }) {
406 return (
407 <Empty
408 icon={<HourglassIcon size={40} />}
409 title="This repository is being prepared"
410 description="Copying history can take a moment for large repositories."
411 contents={
412 <LinkButton variant="secondary" href={`/${repo}`} icon={<ArrowClockwiseIcon />}>
413 Refresh
414 </LinkButton>
415 }
416 />
417 );
418}