148 lines · 6.5 KBRaw
| 1 | import { AgentAvatar, Ago, Badge, Banner, Button, Dialog, Empty, Input, LayerCard } from "@refs/design-system"; |
| 2 | import { GitBranchIcon, GitPullRequestIcon, TrashIcon } from "@refs/design-system/icons"; |
| 3 | import { pullLink, repoLink, type BranchSummary } from "@refs/backend/contract"; |
| 4 | |
| 5 | import { useState } from "react"; |
| 6 | import { Link, useFetcher } from "react-router"; |
| 7 | import type { Route } from "./+types/repo-branches"; |
| 8 | import { failed, formText, found, fullName, nowSeconds, refs } from "../lib/session"; |
| 9 | |
| 10 | const FILTER_FROM = 8; |
| 11 | |
| 12 | export const meta: Route.MetaFunction = ({ params }) => [{ title: `Branches · ${fullName(params)} · refs` }]; |
| 13 | |
| 14 | export async function loader({ params, context }: Route.LoaderArgs) { |
| 15 | const repo = await found(refs(context).repo(fullName(params))); |
| 16 | return { repo: fullName(params), canDelete: repo.summary.can.write, branches: await found(repo.branches()), now: nowSeconds() }; |
| 17 | } |
| 18 | |
| 19 | export async function action({ request, params, context }: Route.ActionArgs) { |
| 20 | const repo = await found(refs(context).repo(fullName(params))); |
| 21 | const outcome = await found(repo.deleteBranch(formText(await request.formData(), "name"))); |
| 22 | return outcome.ok ? { error: null } : failed(outcome.problem, {}); |
| 23 | } |
| 24 | |
| 25 | function DeleteBranch({ name }: { name: string }) { |
| 26 | const fetcher = useFetcher<typeof action>(); |
| 27 | return ( |
| 28 | <Dialog.Root> |
| 29 | <Dialog.Trigger |
| 30 | render={(props) => ( |
| 31 | <Button {...props} variant="ghost" size="sm" shape="square" className="hit-area" aria-label={`Delete ${name}`}> |
| 32 | <TrashIcon size={14} /> |
| 33 | </Button> |
| 34 | )} |
| 35 | /> |
| 36 | <Dialog size="sm" className="p-6"> |
| 37 | <Dialog.Title className="m-0 text-lg font-semibold wrap-anywhere">Delete {name}?</Dialog.Title> |
| 38 | <Dialog.Description className="mt-2 mb-5 text-muted">Commits that are only on this branch are lost. Commits that are on another branch stay.</Dialog.Description> |
| 39 | {fetcher.data?.error && <Banner variant="error" description={fetcher.data.error} className="mb-5" />} |
| 40 | <fetcher.Form method="post" className="flex justify-end gap-2"> |
| 41 | <input type="hidden" name="name" value={name} /> |
| 42 | <Dialog.Close render={(props) => <Button {...props} variant="secondary">Cancel</Button>} /> |
| 43 | <Button type="submit" variant="destructive" loading={fetcher.state !== "idle"}> |
| 44 | Delete branch |
| 45 | </Button> |
| 46 | </fetcher.Form> |
| 47 | </Dialog> |
| 48 | </Dialog.Root> |
| 49 | ); |
| 50 | } |
| 51 | |
| 52 | function BranchRow({ repo, branch, canDelete, now }: { repo: string; branch: BranchSummary; canDelete: boolean; now: number }) { |
| 53 | const free = !branch.isDefault && branch.pull === null && branch.agent === null; |
| 54 | return ( |
| 55 | <li className="flex items-center gap-3 px-4 py-3 shadow-[inset_0_-1px_0_var(--hairline)] last:shadow-none"> |
| 56 | <GitBranchIcon size={16} className="shrink-0 text-muted" aria-hidden="true" /> |
| 57 | <div className="flex min-w-0 flex-1 flex-col gap-0.5"> |
| 58 | <Link to={repoLink(repo, "tree", branch.name)} className="w-fit max-w-full truncate font-mono text-sm font-medium text-ink hover:text-accent-text"> |
| 59 | {branch.name} |
| 60 | </Link> |
| 61 | {branch.commit && ( |
| 62 | <span className="truncate text-compact text-muted"> |
| 63 | {branch.commit.title} · {branch.commit.author} · <Ago at={branch.commit.committedAt} now={now} /> |
| 64 | </span> |
| 65 | )} |
| 66 | </div> |
| 67 | {branch.agent && ( |
| 68 | <span className="hidden shrink-0 items-center gap-1.5 text-compact text-muted sm:flex"> |
| 69 | <AgentAvatar name={branch.agent} /> {branch.agent} works here |
| 70 | </span> |
| 71 | )} |
| 72 | {branch.pull !== null && ( |
| 73 | <Link to={pullLink(repo, branch.pull)} className="flex shrink-0 items-center gap-1.5 text-compact text-muted tabular-nums hover:text-ink"> |
| 74 | <GitPullRequestIcon size={14} aria-hidden="true" /> #{branch.pull} |
| 75 | </Link> |
| 76 | )} |
| 77 | {branch.isDefault && <Badge variant="secondary">default</Badge>} |
| 78 | {canDelete && free && <DeleteBranch name={branch.name} />} |
| 79 | </li> |
| 80 | ); |
| 81 | } |
| 82 | |
| 83 | function Group({ title, children }: { title: string; children: React.ReactNode }) { |
| 84 | return ( |
| 85 | <section className="flex flex-col gap-2"> |
| 86 | <h2 className="m-0 text-compact font-medium text-muted">{title}</h2> |
| 87 | {children} |
| 88 | </section> |
| 89 | ); |
| 90 | } |
| 91 | |
| 92 | export default function RepoBranches({ loaderData }: Route.ComponentProps) { |
| 93 | const { repo, branches, canDelete, now } = loaderData; |
| 94 | const [filter, setFilter] = useState(""); |
| 95 | const main = branches.find((branch) => branch.isDefault); |
| 96 | const others = branches.filter((branch) => !branch.isDefault); |
| 97 | const shown = others.filter((branch) => branch.name.toLowerCase().includes(filter.trim().toLowerCase())); |
| 98 | |
| 99 | if (branches.length === 0) { |
| 100 | return ( |
| 101 | <Empty |
| 102 | icon={<GitBranchIcon size={40} />} |
| 103 | title="No branches yet" |
| 104 | description="This repository has no commits. Push a commit to create the first branch." |
| 105 | contents={ |
| 106 | <Link to={`/${repo}`} className="text-compact text-accent-text hover:underline"> |
| 107 | See how to push |
| 108 | </Link> |
| 109 | } |
| 110 | /> |
| 111 | ); |
| 112 | } |
| 113 | |
| 114 | return ( |
| 115 | <div className="flex flex-col gap-6"> |
| 116 | <h1 className="m-0 text-heading font-semibold">Branches</h1> |
| 117 | {main && ( |
| 118 | <Group title="Default"> |
| 119 | <LayerCard> |
| 120 | <ul className="m-0 list-none p-0"> |
| 121 | <BranchRow repo={repo} branch={main} canDelete={canDelete} now={now} /> |
| 122 | </ul> |
| 123 | </LayerCard> |
| 124 | </Group> |
| 125 | )} |
| 126 | <Group title={others.length ? `Other branches · ${others.length}` : "Other branches"}> |
| 127 | {others.length >= FILTER_FROM && <Input aria-label="Filter branches" placeholder="Filter branches" value={filter} onChange={(event) => setFilter(event.target.value)} />} |
| 128 | {others.length === 0 ? ( |
| 129 | <LayerCard> |
| 130 | <Empty size="sm" title="No other branches" description="A branch that you or an agent pushes shows here. Delete it here after its work is merged." /> |
| 131 | </LayerCard> |
| 132 | ) : shown.length === 0 ? ( |
| 133 | <LayerCard> |
| 134 | <Empty size="sm" title={`No branch matches "${filter.trim()}"`} contents={<Button variant="secondary" onClick={() => setFilter("")}>Clear the filter</Button>} /> |
| 135 | </LayerCard> |
| 136 | ) : ( |
| 137 | <LayerCard> |
| 138 | <ul className="m-0 list-none p-0"> |
| 139 | {shown.map((branch) => ( |
| 140 | <BranchRow key={branch.name} repo={repo} branch={branch} canDelete={canDelete} now={now} /> |
| 141 | ))} |
| 142 | </ul> |
| 143 | </LayerCard> |
| 144 | )} |
| 145 | </Group> |
| 146 | </div> |
| 147 | ); |
| 148 | } |