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