Skip to content
225 linesCodeBlameRaw
1import { ArrowLeft, Bot, GitBranch, Milestone as MilestoneIcon, Plus, Tag } from "lucide-react";
2import { Link, useLocation } from "react-router";
3
4import type { Route } from "./+types/pulls";
5import { page } from "../../lib/meta";
6import { openedBy } from "../../lib/opened-by";
7import { ButtonLink, EmptyState, TimeAgo } from "../../components/ui";
8import { CheckBadge } from "../../components/checks";
9import { ChangeSize, PullIcon, StateTabs } from "../../components/work";
10import { AgentBadge, useActiveRuns } from "../../components/agents";
11import { FilterMenu, LabelChip, Swatch } from "../../components/labels";
12import { colorsOf, listFilters, withFilter } from "../../lib/labels";
13import { accessTo, repoFor } from "../../lib/access.server";
14import { agents, work } from "../../lib/services.server";
15import { getViewer, unwrap } from "../../lib/session.server";
16
17export function meta({ params, ...args }: Route.MetaArgs) {
18 return page(args, { title: `Pull requests · ${params.owner}/${params.repo} · g1t` });
19}
20
21export async function loader({ params, context, request }: Route.LoaderArgs) {
22 const path = { namespace: params.owner, name: params.repo };
23 const viewer = getViewer(context);
24 // `?label=bug&milestone=3&base=release`, or the same written in `q`.
25 const { state, label, milestone, base } = listFilters(new URL(request.url).searchParams);
26 // Which an agent is working on, beside the list, so its badges come with it.
27 const [pulls, active, { can }, labels, milestones, repo] = await Promise.all([
28 work.listPulls(path, viewer, state, {
29 label: label || undefined,
30 milestone: milestone ?? undefined,
31 base: base || undefined,
32 }),
33 agents.listRuns(viewer, { repo: path, active: true, limit: 100 }).catch(() => null),
34 accessTo(context, params),
35 work.listLabels(path, viewer),
36 work.listMilestones(path, viewer, "open"),
37 // The layout looks it up in this request too.
38 repoFor(context, params),
39 ]);
40 return {
41 pulls: unwrap(pulls),
42 state,
43 label,
44 milestone,
45 base,
46 labels: labels.ok ? labels.value : [],
47 milestones: milestones.ok ? milestones.value : [],
48 defaultBranch: repo.ok ? repo.value.defaultBranch : null,
49 // As the project's agents.json has them; left out, the list fetches them.
50 active: active?.ok ? { runs: active.value, member: can.run } : undefined,
51 } as const;
52}
53
54export default function Pulls({ loaderData, params }: Route.ComponentProps) {
55 const { pulls, state, active, labels, milestones, label, milestone, defaultBranch } = loaderData;
56 const base = `/${params.owner}/${params.repo}`;
57 const list = `${base}/pulls`;
58 // Which pull requests an agent is working on this minute, and at what.
59 const working = useActiveRuns(params.owner, params.repo, active);
60 const current = new URLSearchParams(useLocation().search);
61 const colors = colorsOf(labels);
62 const milestoneTitle = milestones.find((m) => m.number === milestone)?.title ?? (milestone ? `#${milestone}` : null);
63 const filtered = [label, milestone, loaderData.base].some(Boolean);
64 const kept = new URLSearchParams(current);
65 kept.delete("state");
66 kept.delete("q");
67 return (
68 <div>
69 <StateTabs
70 to={list}
71 state={state}
72 query={kept.toString()}
73 action={
74 <ButtonLink to={`${base}/pulls/new`}>
75 <Plus size={15} />
76 New pull request
77 </ButtonLink>
78 }
79 />
80 <div className="mt-3 flex flex-wrap items-center gap-2">
81 <FilterMenu
82 label="Label"
83 active={label || undefined}
84 clearTo={label ? withFilter(list, current, "label", null) : undefined}
85 searchPlaceholder="Filter labels"
86 emptyText="No label matches."
87 options={labels.map((each) => ({
88 key: each.name,
89 to: withFilter(list, current, "label", each.name),
90 keywords: `${each.name} ${each.description}`,
91 selected: each.name === label,
92 label: (
93 <>
94 <Swatch color={each.color} />
95 <span className="truncate">{each.name}</span>
96 </>
97 ),
98 }))}
99 />
100 <FilterMenu
101 label="Milestone"
102 active={milestoneTitle ?? undefined}
103 clearTo={milestone ? withFilter(list, current, "milestone", null) : undefined}
104 searchPlaceholder="Filter milestones"
105 emptyText="No open milestone matches."
106 options={milestones.map((each) => ({
107 key: String(each.number),
108 to: withFilter(list, current, "milestone", String(each.number)),
109 keywords: `${each.title} ${each.number}`,
110 selected: each.number === milestone,
111 label: <span className="truncate">{each.title}</span>,
112 }))}
113 />
114 {loaderData.base && (
115 <Link
116 to={withFilter(list, current, "base", null)}
117 className="inline-flex h-8 items-center gap-1.5 rounded-md border border-accent/40 bg-accent/5 px-2.5 text-sm"
118 title="Show pull requests into every branch"
119 >
120 Into <span className="font-mono">{loaderData.base}</span>
121 </Link>
122 )}
123 {filtered && (
124 <Link to={state === "closed" ? `${list}?state=closed` : list} className="text-xs text-muted hover:text-fg">
125 Clear filters
126 </Link>
127 )}
128 <span className="ml-auto flex items-center gap-1">
129 <Link
130 to={`${base}/labels`}
131 className="inline-flex h-8 items-center gap-1.5 rounded-md px-2.5 text-sm text-muted transition-colors hover:bg-surface hover:text-fg"
132 >
133 <Tag size={14} />
134 Labels
135 </Link>
136 <Link
137 to={`${base}/milestones`}
138 className="inline-flex h-8 items-center gap-1.5 rounded-md px-2.5 text-sm text-muted transition-colors hover:bg-surface hover:text-fg"
139 >
140 <MilestoneIcon size={14} />
141 Milestones
142 </Link>
143 </span>
144 </div>
145 <div className="mt-4">
146 {pulls.length === 0 ? (
147 <EmptyState
148 title={
149 filtered
150 ? `No ${state} pull requests match these filters`
151 : state === "open"
152 ? "No open pull requests"
153 : "No closed pull requests"
154 }
155 >
156 A pull request proposes a change. Assign agents to an issue and
157 each opens one in its own fork, or push a branch and open one
158 yourself.
159 </EmptyState>
160 ) : (
161 <ul className="divide-y divide-line overflow-hidden rounded-xl border border-line">
162 {pulls.map((pull) => (
163 <li key={pull.id}>
164 <Link
165 prefetch="intent"
166 to={`${base}/pull/${pull.number}`}
167 className="flex items-start gap-3 px-4 py-3 transition-colors hover:bg-surface"
168 >
169 <span className="mt-0.5">
170 <PullIcon status={pull.status} />
171 </span>
172 <span className="min-w-0 grow">
173 <span className="flex flex-wrap items-center gap-x-2 gap-y-1">
174 <span className="min-w-0 truncate font-medium">{pull.title}</span>
175 {pull.status === "draft" && (
176 <span className="shrink-0 rounded-full border border-line px-1.5 py-px text-[0.6875rem] text-faint">
177 draft
178 </span>
179 )}
180 {(pull.labels ?? []).map((name) => (
181 <LabelChip key={name} name={name} color={colors[name]} />
182 ))}
183 <AgentBadge run={working.get(pull.number)} />
184 </span>
185 <span className="mt-0.5 block text-xs text-faint">
186 #{pull.number} opened <TimeAgo at={pull.createdAt} /> by{" "}
187 {openedBy(pull).name}
188 {pull.requestedBy && <> for {pull.requestedBy.username}</>}
189 {pull.issue != null && <> · for #{pull.issue}</>}
190 {pull.supersededBy != null && <> · superseded by #{pull.supersededBy}</>}
191 {pull.milestone && (
192 <>
193 {" "}
194 · <MilestoneIcon size={11} className="inline" /> {pull.milestone.title}
195 </>
196 )}
197 {/* Into a branch other than the default one: said. */}
198 {pull.base && defaultBranch && pull.base !== defaultBranch && (
199 <>
200 {" "}
201 · <ArrowLeft size={11} className="inline" /> into{" "}
202 <span className="font-mono">{pull.base}</span>
203 </>
204 )}
205 </span>
206 </span>
207 <span className="mt-0.5 hidden sm:block">
208 <ChangeSize files={pull.files} />
209 </span>
210 <span className="mt-0.5">
211 <CheckBadge status={pull.checkStatus} />
212 </span>
213 <span className="mt-0.5 flex shrink-0 items-center gap-1 font-mono text-xs text-muted">
214 {pull.branch ? <GitBranch size={13} /> : <Bot size={13} />}
215 {pull.branch ?? pull.agent}
216 </span>
217 </Link>
218 </li>
219 ))}
220 </ul>
221 )}
222 </div>
223 </div>
224 );
225}