Open History to see the last commit
1import { SearchIcon } from "lucide-react";
2import { PullRequestStackPopover } from "./PullRequestStackPopover";
3import {
4 PullRequestSpeedActions,
5 type PullRequestSpeedActionResult,
6} from "./PullRequestSpeedActions";
7import { memo, type RefCallback } from "react";
8
9import { cn } from "~/lib/utils";
10import { getSourceControlPresentationForKind } from "~/sourceControlPresentation";
11
12import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
13import { PullRequestChecksPopover } from "./PullRequestChecksPopover";
14import type { EnvironmentPullRequestEntry } from "./pullRequestList.logic";
15import { openOnHostLabel, showPullRequestLinkContextMenu } from "./pullRequestLinkContextMenu";
16import {
17 PULL_REQUEST_ROW_CLASS,
18 PULL_REQUEST_ROW_NUMBER_CLASS,
19 PullRequestRowAuthor,
20 PullRequestRowGlyph,
21 PullRequestRowLines,
22} from "./PullRequestListRow";
23import {
24 PullRequestDiffStat,
25 PullRequestLabelChip,
26 PullRequestReviewDecisionGlyph,
27} from "./pullRequestPresentation";
28
29/**
30 * Each slot past the first only appears once the meta line is wide enough to hold it, so a
31 * narrow row shows one label and a "+N" while a wide one spreads out up to three. The "+N"
32 * rides on whichever pill is the last visible one, and is hidden as soon as the next slot shows.
33 */
34// Each slot's pill appears at a wider row, and its overflow count yields to the next slot.
35const LABEL_SLOTS = [
36 { overflow: "@xl/pr-row-meta:hidden" },
37 { overflow: "@3xl/pr-row-meta:hidden" },
38 { overflow: "" },
39] as const;
40
41function PullRequestRowLabels({ labels }: { labels: EnvironmentPullRequestEntry["labels"] }) {
42 if (labels.length === 0) return null;
43 return (
44 <span className="flex min-w-0 items-center gap-1">
45 {LABEL_SLOTS.map((slot, index) => {
46 const label = labels[index];
47 if (!label) return null;
48 const remaining = labels.length - index - 1;
49 return (
50 <PullRequestLabelChip
51 key={label.name}
52 label={label}
53 className={
54 index === 0
55 ? ""
56 : index === 1
57 ? "hidden @xl/pr-row-meta:inline-flex"
58 : "hidden @3xl/pr-row-meta:inline-flex"
59 }
60 >
61 {remaining > 0 ? (
62 <span className={cn("shrink-0", slot.overflow)}>+{remaining}</span>
63 ) : null}
64 </PullRequestLabelChip>
65 );
66 })}
67 </span>
68 );
69}
70
71/**
72 * The page row keeps a little more room around the shared lines than the panel, which sits in
73 * a narrow column. Its outer wrapper reserves the full row height when offscreen.
74 */
75const PAGE_ROW_CLASS = "px-3 py-2.5";
76
77export type PullRequestRowTarget = Pick<
78 EnvironmentPullRequestEntry,
79 "environmentId" | "projectId" | "host" | "repository" | "number"
80>;
81
82function PullRequestRowImpl({
83 entry,
84 selected,
85 showProjectTitle,
86 showProvider,
87 environmentLabel,
88 matchedElsewhere,
89 statsKey,
90 statsRef,
91 onSelect,
92 speedMode,
93 onActed,
94}: {
95 entry: EnvironmentPullRequestEntry;
96 selected: boolean;
97 showProjectTitle: boolean;
98 /** Only when the list spans more than one host, where the repository alone is ambiguous. */
99 showProvider: boolean;
100 /** Names the server this row was read from, where the list spans more than one. */
101 environmentLabel?: string;
102 /**
103 * A search found this, but in something the row does not show — a description, a comment, a
104 * commit message. Saying so is the difference between a result and an apparently random row.
105 */
106 matchedElsewhere?: boolean;
107 /** Used by the list's shared visibility observer to defer optional line-count reads. */
108 statsKey?: string;
109 statsRef?: RefCallback<HTMLDivElement>;
110 onSelect: (entry: PullRequestRowTarget) => void;
111 speedMode: boolean;
112 onActed: (result: PullRequestSpeedActionResult) => void;
113}) {
114 const { Icon, providerName } = getSourceControlPresentationForKind(entry.provider);
115 return (
116 <div
117 ref={statsRef}
118 data-pull-request-stats-key={statsKey}
119 className={cn(
120 // Keep the action buttons inside the same skipped row, but outside its selection button.
121 "flex items-center rounded-md [content-visibility:auto] [contain-intrinsic-block-size:56.5px]",
122 selected ? "bg-accent" : "hover:bg-accent/60",
123 )}
124 >
125 <button
126 type="button"
127 aria-current={selected ? "true" : undefined}
128 onClick={() => onSelect(entry)}
129 className={cn(
130 PULL_REQUEST_ROW_CLASS,
131 PAGE_ROW_CLASS,
132 "min-w-0 flex-1 cursor-pointer transition-colors focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring",
133 )}
134 >
135 <PullRequestRowGlyph
136 state={entry.state}
137 isDraft={entry.isDraft}
138 mergeability={entry.mergeability}
139 baseBranch={entry.baseBranch}
140 // On the title line rather than between the lines, as main aligns it.
141 className="mt-0.75 self-start"
142 />
143 <PullRequestRowLines
144 number={
145 // The number carries the link, here as much as on the detail: a right-click on it
146 // copies the pull request's own address rather than opening the editing menu.
147 <span
148 className={PULL_REQUEST_ROW_NUMBER_CLASS}
149 onContextMenu={(event) => {
150 event.preventDefault();
151 event.stopPropagation();
152 void showPullRequestLinkContextMenu({
153 url: entry.url,
154 openLabel: openOnHostLabel(entry.provider),
155 position: { x: event.clientX, y: event.clientY },
156 });
157 }}
158 >
159 #{entry.number}
160 </span>
161 }
162 title={entry.title}
163 signals={
164 <>
165 {entry.checksState === undefined ? null : (
166 <PullRequestChecksPopover
167 checksState={entry.checksState}
168 environmentId={entry.environmentId}
169 reference={{
170 projectId: entry.projectId,
171 repository: entry.repository,
172 number: entry.number,
173 }}
174 />
175 )}
176 {/* Only a verdict the host actually reports: an approval, a request for changes,
177 or a review the branch rules still require. No glyph on the common case of a
178 pull request nobody has reviewed, so a row only wears a person when the person
179 has said something. */}
180 {entry.reviewDecision === undefined ? null : (
181 <PullRequestReviewDecisionGlyph decision={entry.reviewDecision} />
182 )}
183 </>
184 }
185 status={
186 <>
187 {entry.stack ? (
188 <PullRequestStackPopover
189 environmentId={entry.environmentId}
190 reference={{
191 projectId: entry.projectId,
192 host: entry.host,
193 repository: entry.repository,
194 number: entry.number,
195 }}
196 membership={entry.stack}
197 onSelect={(target) =>
198 onSelect({ ...target, host: entry.host, environmentId: entry.environmentId })
199 }
200 />
201 ) : null}
202 <PullRequestDiffStat
203 additions={entry.additions}
204 deletions={entry.deletions}
205 className="font-mono"
206 />
207 </>
208 }
209 metaClassName="@container/pr-row-meta"
210 meta={
211 <>
212 {matchedElsewhere ? (
213 <Tooltip>
214 <TooltipTrigger
215 render={
216 <span className="flex min-w-6 items-center gap-1 overflow-hidden rounded-full border border-border/60 px-1 text-3xs" />
217 }
218 >
219 <span className="sr-only">matched in the description</span>
220 <SearchIcon aria-hidden className="size-3 shrink-0" />
221 <span aria-hidden className="hidden truncate @xs/pr-row-meta:block">
222 matched in the description
223 </span>
224 </TooltipTrigger>
225 <TooltipPopup side="top">Matched in the description</TooltipPopup>
226 </Tooltip>
227 ) : null}
228 {showProvider ? (
229 <Tooltip>
230 <TooltipTrigger render={<span className="inline-flex shrink-0" />}>
231 <Icon aria-label={providerName} className="size-3" />
232 </TooltipTrigger>
233 <TooltipPopup>{providerName}</TooltipPopup>
234 </Tooltip>
235 ) : null}
236 <PullRequestRowAuthor
237 actor={entry.author}
238 className="min-w-3.5 max-w-40"
239 labelClassName="sr-only @xs/pr-row-meta:not-sr-only @xs/pr-row-meta:truncate"
240 />
241 {showProjectTitle ? <span className="truncate">{entry.repository}</span> : null}
242 {environmentLabel ? (
243 <span className="min-w-0 max-w-32 truncate">{environmentLabel}</span>
244 ) : null}
245 {entry.labels.length > 0 ? <PullRequestRowLabels labels={entry.labels} /> : null}
246 </>
247 }
248 updatedAt={entry.updatedAt}
249 />
250 </button>
251 {entry.state !== "merged" && entry.provider === "github" ? (
252 <PullRequestSpeedActions entry={entry} visible={speedMode} onActed={onActed} />
253 ) : null}
254 </div>
255 );
256}
257
258/**
259 * Memoized: the list re-renders on every keystroke of a search and every status poll, and a
260 * row whose entry, selection and match state are unchanged has nothing new to say. Effective
261 * because the route hands it a stable `onSelect`.
262 */
263export const PullRequestRow = memo(PullRequestRowImpl);