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