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