Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

PullRequestRow.tsx

277 lines10.2 KB

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 );
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 closing = false,
95 sweeping = false,
96 onCloseSweepStart,
97}: {
98 entry: EnvironmentPullRequestEntry;
99 selected: boolean;
100 showProjectTitle: boolean;
101 /** Only when the list spans more than one host, where the repository alone is ambiguous. */
102 showProvider: boolean;
103 /** Names the server this row was read from, where the list spans more than one. */
104 environmentLabel?: string;
105 /**
106 * A search found this, but in something the row does not show — a description, a comment, a
107 * commit message. Saying so is the difference between a result and an apparently random row.
108 */
109 matchedElsewhere?: boolean;
110 /** Used by the list's shared visibility observer to defer optional line-count reads. */
111 statsKey?: string;
112 statsRef?: RefCallback<HTMLDivElement>;
113 onSelect: (entry: PullRequestRowTarget) => void;
114 speedMode: boolean;
115 onActed: (result: PullRequestSpeedActionResult) => void;
116 closing?: boolean;
117 sweeping?: boolean;
118 onCloseSweepStart?: (entry: EnvironmentPullRequestEntry, event: PointerEvent) => void;
119}) {
120 const { Icon, providerName } = getSourceControlPresentationForKind(entry.provider);
121 return (
122 <div
123 ref={statsRef}
124 data-pull-request-stats-key={statsKey}
125 data-pull-request-key={statsKey}
126 className={cn(
127 // Keep the action buttons inside the same skipped row, but outside its selection button.
128 "flex items-center rounded-md [content-visibility:auto] [contain-intrinsic-block-size:56.5px]",
129 selected ? "bg-accent" : "hover:bg-accent/60",
130 )}
131 >
132 <button
133 type="button"
134 aria-current={selected ? "true" : undefined}
135 onClick={() => onSelect(entry)}
136 className={cn(
137 PULL_REQUEST_ROW_CLASS,
138 PAGE_ROW_CLASS,
139 "min-w-0 flex-1 cursor-pointer transition-colors focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring",
140 )}
141 >
142 <PullRequestRowGlyph
143 state={entry.state}
144 isDraft={entry.isDraft}
145 mergeability={entry.mergeability}
146 baseBranch={entry.baseBranch}
147 // On the title line rather than between the lines, as main aligns it.
148 className="mt-0.75 self-start"
149 />
150 <PullRequestRowLines
151 number={
152 // The number carries the link, here as much as on the detail: a right-click on it
153 // copies the pull request's own address rather than opening the editing menu.
154 <span
155 className={PULL_REQUEST_ROW_NUMBER_CLASS}
156 onContextMenu={(event) => {
157 event.preventDefault();
158 event.stopPropagation();
159 void showPullRequestLinkContextMenu({
160 url: entry.url,
161 openLabel: openOnHostLabel(entry.provider),
162 position: { x: event.clientX, y: event.clientY },
163 });
164 }}
165 >
166 #{entry.number}
167 </span>
168 }
169 title={entry.title}
170 signals={
171 <>
172 {entry.checksState === undefined ? null : (
173 <PullRequestChecksPopover
174 checksState={entry.checksState}
175 environmentId={entry.environmentId}
176 reference={{
177 projectId: entry.projectId,
178 repository: entry.repository,
179 number: entry.number,
180 }}
181 />
182 )}
183 {/* Only a verdict the host actually reports: an approval, a request for changes,
184 or a review the branch rules still require. No glyph on the common case of a
185 pull request nobody has reviewed, so a row only wears a person when the person
186 has said something. */}
187 {entry.reviewDecision === undefined ? null : (
188 <PullRequestReviewDecisionGlyph decision={entry.reviewDecision} />
189 )}
190 </>
191 }
192 status={
193 <>
194 {entry.stack ? (
195 <PullRequestStackPopover
196 environmentId={entry.environmentId}
197 reference={{
198 projectId: entry.projectId,
199 host: entry.host,
200 repository: entry.repository,
201 number: entry.number,
202 }}
203 membership={entry.stack}
204 onSelect={(target) =>
205 onSelect({ ...target, host: entry.host, environmentId: entry.environmentId })
206 }
207 />
208 ) : null}
209 <PullRequestDiffStat
210 additions={entry.additions}
211 deletions={entry.deletions}
212 className="font-mono"
213 />
214 </>
215 }
216 metaClassName="@container/pr-row-meta"
217 meta={
218 <>
219 {matchedElsewhere ? (
220 <Tooltip>
221 <TooltipTrigger
222 render={
223 <span className="flex min-w-6 items-center gap-1 overflow-hidden rounded-full border border-border/60 px-1 text-3xs" />
224 }
225 >
226 <span className="sr-only">matched in the description</span>
227 <SearchIcon aria-hidden className="size-3 shrink-0" />
228 <span aria-hidden className="hidden truncate @xs/pr-row-meta:block">
229 matched in the description
230 </span>
231 </TooltipTrigger>
232 <TooltipPopup side="top">Matched in the description</TooltipPopup>
233 </Tooltip>
234 ) : null}
235 {showProvider ? (
236 <Tooltip>
237 <TooltipTrigger render={<span className="inline-flex shrink-0" />}>
238 <Icon aria-label={providerName} className="size-3" />
239 </TooltipTrigger>
240 <TooltipPopup>{providerName}</TooltipPopup>
241 </Tooltip>
242 ) : null}
243 <PullRequestRowAuthor
244 actor={entry.author}
245 className="min-w-3.5 max-w-40"
246 labelClassName="sr-only @xs/pr-row-meta:not-sr-only @xs/pr-row-meta:truncate"
247 />
248 {showProjectTitle ? <span className="truncate">{entry.repository}</span> : null}
249 {environmentLabel ? (
250 <span className="min-w-0 max-w-32 truncate">{environmentLabel}</span>
251 ) : null}
252 {entry.labels.length > 0 ? <PullRequestRowLabels labels={entry.labels} /> : null}
253 </>
254 }
255 updatedAt={entry.updatedAt}
256 />
257 </button>
258 {entry.state !== "merged" && entry.provider === "github" ? (
259 <PullRequestSpeedActions
260 entry={entry}
261 visible={speedMode}
262 onActed={onActed}
263 closing={closing}
264 sweeping={sweeping}
265 {...(onCloseSweepStart ? { onCloseSweepStart } : {})}
266 />
267 ) : null}
268 </div>
269 );
271​
272/**
273 * Memoized: the list re-renders on every keystroke of a search and every status poll, and a
274 * row whose entry, selection and match state are unchanged has nothing new to say. Effective
275 * because the route hands it a stable `onSelect`.
276 */
277export const PullRequestRow = memo(PullRequestRowImpl);