Skip to content

g1t/apps/web/app/routes/repo/branches.tsx

155 lines7,064 bytesCodeBlame
1import { GitBranch, Search } from "lucide-react";
2import { Form, Link } from "react-router";
3
4import type { Route } from "./+types/branches";
5import { ActiveBranches } from "../../components/branches";
6import { Avatar, EmptyState, TimeAgo, notACredential } from "../../components/ui";
7import { Hint } from "../../components/ui/hint";
8import { readBranches } from "../../lib/branches.server";
9import { commitChecksFor } from "../../lib/commit-checks.server";
10import { CommitChecksBadge } from "../../components/commit-checks";
11import { page } from "../../lib/meta";
12import { deployments, repos, work } from "../../lib/services.server";
13import { getViewer, unwrap } from "../../lib/session.server";
14
15/** The most branches read for one page; past that, search narrows them. */
16const BRANCHES_READ = 60;
17/** A branch whose last commit is older than this is stale. */
18const STALE_DAYS = 90;
19
20export function meta({ params, ...args }: Route.MetaArgs) {
21 return page(args, { title: `Branches · ${params.owner}/${params.repo} · g1t` });
22}
23
24export async function loader({ params, context, request }: Route.LoaderArgs) {
25 const path = { namespace: params.owner, name: params.repo };
26 const viewer = getViewer(context);
27 const query = new URL(request.url).searchParams.get("q")?.trim().toLowerCase() || null;
28 const soft = <T,>(promise: Promise<T>): Promise<T | null> => promise.catch(() => null);
29 const [repo, list, pulls, deploys] = await Promise.all([
30 repos.get(path, viewer),
31 repos.branches(path, viewer),
32 soft(work.listPulls(path, viewer, "open")),
33 // Previews are for people with a role here; everyone else sees none.
34 soft(deployments.list({ workspace: params.owner, slug: params.repo }, viewer)),
35 ]);
36 const found = unwrap(repo);
37 // The merge queue's own branches (g1t-queue/<entry>) are its working
38 // copies, not anyone's branch to look at.
39 const all = unwrap(list).filter((branch) => !branch.name.startsWith("g1t-queue/"));
40 const matching = query ? all.filter((branch) => branch.name === found.defaultBranch || branch.name.toLowerCase().includes(query)) : all;
41 const read = await readBranches(
42 path,
43 viewer,
44 {
45 defaultBranch: found.defaultBranch,
46 branches: matching,
47 pulls: pulls?.ok ? pulls.value : [],
48 previews: deploys?.ok ? deploys.value.live.filter((app) => app.kind === "preview") : [],
49 },
50 BRANCHES_READ,
51 );
52 // Each branch head's checks, in one call, streamed in beside it.
53 const checks = commitChecksFor(path, viewer, [read.head?.hash, ...read.shown.map((branch) => branch.commit?.hash)]);
54 const staleBefore = Date.now() - STALE_DAYS * 86_400_000;
55 const isStale = (at: string | undefined) => at != null && Date.parse(at) < staleBefore;
56 return {
57 main: found.defaultBranch,
58 head: read.head,
59 active: read.shown.filter((branch) => !isStale(branch.commit?.at)),
60 stale: read.shown.filter((branch) => isStale(branch.commit?.at)),
61 // Every branch but the default, and how many matched the search.
62 total: all.length - 1,
63 matched: read.total,
64 unread: Math.max(0, read.total - read.shown.length),
65 query,
66 checks,
67 };
68}
69
70export default function Branches({ loaderData, params }: Route.ComponentProps) {
71 const { main, head, active, stale, total, matched, unread, query, checks } = loaderData;
72 const base = `/${params.owner}/${params.repo}`;
73 return (
74 <div className="space-y-6">
75 <div className="flex flex-wrap items-center justify-between gap-3">
76 <h2 className="text-lg font-semibold tracking-tight">
77 Branches <span className="font-normal text-faint">{total + 1}</span>
78 </h2>
79 <Form method="get" role="search" className="relative w-full sm:w-72">
80 <Search size={14} className="pointer-events-none absolute top-1/2 left-2.5 -translate-y-1/2 text-faint" />
81 <input
82 name="q"
83 defaultValue={query ?? ""}
84 {...notACredential()}
85 placeholder="Search branches"
86 aria-label="Search branches"
87 className="h-8 w-full rounded-md border border-line bg-surface pr-3 pl-8 text-sm outline-none transition-colors placeholder:text-faint hover:border-line-strong focus:border-accent-dim"
88 />
89 </Form>
90 </div>
91
92 <section>
93 <h3 className="mb-2 text-sm font-medium text-muted">Default</h3>
94 <div className="flex flex-wrap items-center gap-x-3 gap-y-1 rounded-xl border border-line bg-surface px-4 py-3">
95 <GitBranch size={15} className="shrink-0 text-faint" />
96 <Link to={`${base}/tree/${encodeURIComponent(main)}`} className="font-mono text-[0.8125rem] font-medium hover:text-accent">
97 {main}
98 </Link>
99 <span className="rounded-full border border-line px-2 py-px text-xs text-muted">default</span>
100 {head && (
101 <span className="flex min-w-0 grow basis-64 items-center gap-1.5 text-xs text-muted">
102 <Avatar name={head.author} size={13} />
103 <span className="shrink-0">{head.author}</span>
104 <span className="text-faint">·</span>
105 <Hint label={head.message}>
106 <Link to={`${base}/commit/${head.hash}`} className="min-w-0 truncate hover:text-fg">
107 {head.message}
108 </Link>
109 </Hint>
110 <CommitChecksBadge checks={checks} sha={head.hash} className="size-5" />
111 <span className="shrink-0 text-faint">
112 · <TimeAgo at={head.at} />
113 </span>
114 </span>
115 )}
116 </div>
117 </section>
118
119 {query && (
120 <p className="text-sm text-muted">
121 {matched} of {total} {total === 1 ? "branch matches" : "branches match"} “{query}”.{" "}
122 <Link to={`${base}/branches`} className="text-fg hover:underline">
123 Clear
124 </Link>
125 </p>
126 )}
127
128 {active.length > 0 && (
129 <section>
130 <h3 className="mb-2 text-sm font-medium text-muted">Active</h3>
131 <ActiveBranches branches={active} base={base} main={main} checks={checks} />
132 </section>
133 )}
134 {stale.length > 0 && (
135 <section>
136 <h3 className="mb-2 text-sm font-medium text-muted">Stale</h3>
137 <p className="-mt-1 mb-2 text-xs text-faint">No commits in the last 90 days.</p>
138 <ActiveBranches branches={stale} base={base} main={main} checks={checks} />
139 </section>
140 )}
141 {active.length === 0 && stale.length === 0 && (
142 <EmptyState title={query ? "No branch matches" : "Only the default branch"}>
143 {query
144 ? "Try another part of the name."
145 : "Branches agents and people push show here, each with its pull request, checks and how far it has moved from the default branch."}
146 </EmptyState>
147 )}
148 {unread > 0 && (
149 <p className="text-xs text-faint">
150 {unread} more {unread === 1 ? "branch is" : "branches are"} not shown. Search to find one by name.
151 </p>
152 )}
153 </div>
154 );
155}