g1t/apps/web/app/components/branches.tsx
| 1 | import { ArrowDown, ArrowUp, GitBranch, GitPullRequest, Globe } from "lucide-react"; |
| 2 | import { Link } from "react-router"; |
| 3 | |
| 4 | import type { Pull } from "@g1t/contracts"; |
| 5 | |
| 6 | import type { Drift } from "../lib/branches"; |
| 7 | import { CheckBadge } from "./checks"; |
| 8 | import { host } from "./deploy"; |
| 9 | import { Avatar, TimeAgo } from "./ui"; |
| 10 | import { Hint } from "./ui/hint"; |
| 11 | |
| 12 | function drift({ ahead, behind }: Drift, main: string): string { |
| 13 | return `${ahead} ${ahead === 1 ? "commit" : "commits"} ahead of ${main}, ${behind} behind`; |
| 14 | } |
| 15 | |
| 16 | export type ActiveBranch = { |
| 17 | name: string; |
| 18 | commit: { hash: string; message: string; author: string; at: string } | null; |
| 19 | /** Null when the two histories were not read far enough to meet. */ |
| 20 | drift: Drift | null; |
| 21 | pull: { number: number; title: string; checkStatus: Pull["checkStatus"]; draft: boolean } | null; |
| 22 | preview: string | null; |
| 23 | }; |
| 24 | |
| 25 | /** Branches other than the default, newest first, with how far each has moved and what is open on it. */ |
| 26 | export function ActiveBranches({ branches, base, main }: { branches: ActiveBranch[]; base: string; main: string }) { |
| 27 | return ( |
| 28 | <ul className="divide-y divide-line overflow-hidden rounded-xl border border-line bg-surface"> |
| 29 | {branches.map((branch) => ( |
| 30 | <li key={branch.name} className="flex flex-wrap items-start gap-x-3 gap-y-1.5 px-4 py-3"> |
| 31 | <GitBranch size={15} className="mt-0.5 shrink-0 text-faint" /> |
| 32 | <span className="min-w-0 grow basis-48"> |
| 33 | <Link |
| 34 | to={`${base}/tree/${encodeURIComponent(branch.name)}`} |
| 35 | className="block truncate font-mono text-[0.8125rem] font-medium hover:text-accent" |
| 36 | > |
| 37 | {branch.name} |
| 38 | </Link> |
| 39 | {branch.commit && ( |
| 40 | <span className="mt-0.5 flex min-w-0 items-center gap-1.5 text-xs text-muted"> |
| 41 | <Avatar name={branch.commit.author} size={13} /> |
| 42 | <span className="shrink-0">{branch.commit.author}</span> |
| 43 | <span className="text-faint">·</span> |
| 44 | <Hint label={branch.commit.message}> |
| 45 | <Link to={`${base}/commit/${branch.commit.hash}`} className="min-w-0 truncate hover:text-fg"> |
| 46 | {branch.commit.message} |
| 47 | </Link> |
| 48 | </Hint> |
| 49 | <span className="shrink-0 text-faint"> |
| 50 | · <TimeAgo at={branch.commit.at} /> |
| 51 | </span> |
| 52 | </span> |
| 53 | )} |
| 54 | </span> |
| 55 | <span className="ml-6.5 flex shrink-0 flex-wrap items-center gap-2 text-xs sm:ml-0"> |
| 56 | {branch.drift && ( |
| 57 | <Hint label={drift(branch.drift, main)}> |
| 58 | <span className="inline-flex items-center gap-1.5 rounded-md border border-line px-1.5 py-0.5 font-mono tabular-nums text-muted"> |
| 59 | <span className="sr-only">{drift(branch.drift, main)}</span> |
| 60 | <span className="inline-flex items-center gap-0.5" aria-hidden> |
| 61 | <ArrowUp size={11} className={branch.drift.ahead > 0 ? "text-success" : "text-faint"} /> |
| 62 | {branch.drift.ahead} |
| 63 | </span> |
| 64 | <span className="inline-flex items-center gap-0.5" aria-hidden> |
| 65 | <ArrowDown size={11} className={branch.drift.behind > 0 ? "text-warn" : "text-faint"} /> |
| 66 | {branch.drift.behind} |
| 67 | </span> |
| 68 | </span> |
| 69 | </Hint> |
| 70 | )} |
| 71 | {branch.pull ? ( |
| 72 | <Hint label={branch.pull.title}> |
| 73 | <Link |
| 74 | to={`${base}/pull/${branch.pull.number}`} |
| 75 | className="inline-flex items-center gap-1.5 rounded-md border border-line px-1.5 py-0.5 text-muted hover:border-line-strong hover:text-fg" |
| 76 | > |
| 77 | <GitPullRequest size={12} className={branch.pull.draft ? "text-faint" : "text-success"} />#{branch.pull.number} |
| 78 | <span className="sr-only">{branch.pull.title}</span> |
| 79 | <CheckBadge status={branch.pull.checkStatus} /> |
| 80 | </Link> |
| 81 | </Hint> |
| 82 | ) : branch.drift?.ahead === 0 ? ( |
| 83 | // Nothing here that the default branch lacks: no pull request to open. |
| 84 | <Hint label={`Every commit on ${branch.name} is already on ${main}.`}> |
| 85 | <span className="px-1.5 py-0.5 text-faint">Nothing to merge</span> |
| 86 | </Hint> |
| 87 | ) : ( |
| 88 | <Link |
| 89 | to={`${base}/pulls/new?branch=${encodeURIComponent(branch.name)}`} |
| 90 | className="rounded-md px-1.5 py-0.5 text-faint hover:text-fg" |
| 91 | > |
| 92 | Open a pull request |
| 93 | </Link> |
| 94 | )} |
| 95 | {branch.preview && ( |
| 96 | <Hint label={host(branch.preview)}> |
| 97 | <a |
| 98 | href={branch.preview} |
| 99 | className="inline-flex max-w-48 items-center gap-1 truncate rounded-md border border-line px-1.5 py-0.5 text-muted hover:border-line-strong hover:text-fg" |
| 100 | > |
| 101 | <Globe size={12} className="shrink-0 text-accent" /> |
| 102 | Preview |
| 103 | </a> |
| 104 | </Hint> |
| 105 | )} |
| 106 | </span> |
| 107 | </li> |
| 108 | ))} |
| 109 | </ul> |
| 110 | ); |
| 111 | } |