Open History to see the last commit
1import { SearchIcon, UserCheckIcon } from "lucide-react";
2import { PullRequestStackPopover } from "./PullRequestStackPopover";
3import { memo, type RefCallback } from "react";
4
5import { cn } from "~/lib/utils";
6import { getSourceControlPresentationForKind } from "~/sourceControlPresentation";
7import { formatRelativeTimeLabel } from "~/timestampFormat";
8
9import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
10import { PullRequestChecksPopover } from "./PullRequestChecksPopover";
11import { pullRequestLabelColor, type EnvironmentPullRequestEntry } from "./pullRequestList.logic";
12import { openOnHostLabel, showPullRequestLinkContextMenu } from "./pullRequestLinkContextMenu";
13import {
14 PullRequestActorLabel,
15 PullRequestDiffStat,
16 PullRequestMetaLine,
17 PullRequestStateGlyph,
18} from "./pullRequestPresentation";
19
20/**
21 * Each slot past the first only appears once the meta line is wide enough to hold it, so a
22 * narrow row shows one label and a "+N" while a wide one spreads out up to three. The "+N"
23 * rides on whichever pill is the last visible one, and is hidden as soon as the next slot shows.
24 */
25const LABEL_SLOTS = [
26 { pill: "", overflow: "@xl/pr-row-meta:hidden" },
27 { pill: "hidden @xl/pr-row-meta:inline-flex", overflow: "@3xl/pr-row-meta:hidden" },
28 { pill: "hidden @3xl/pr-row-meta:inline-flex", overflow: "" },
29] as const;
30
31function PullRequestRowLabels({ labels }: { labels: EnvironmentPullRequestEntry["labels"] }) {
32 if (labels.length === 0) return null;
33 return (
34 <span className="flex min-w-0 items-center gap-1">
35 {LABEL_SLOTS.map((slot, index) => {
36 const label = labels[index];
37 if (!label) return null;
38 const dot = pullRequestLabelColor(label.color);
39 const remaining = labels.length - index - 1;
40 return (
41 <span
42 key={label.name}
43 className={cn(
44 "inline-flex max-w-40 min-w-0 items-center gap-1 rounded-full border border-border/70 bg-muted/40 py-0 pl-1 pr-1.5 text-[10px] leading-3.5 text-muted-foreground",
45 slot.pill,
46 )}
47 >
48 <span
49 aria-hidden
50 className="size-2 shrink-0 rounded-full bg-muted-foreground"
51 {...(dot ? { style: { backgroundColor: dot } } : {})}
52 />
53 <span className="truncate">{label.name}</span>
54 {remaining > 0 ? (
55 <span className={cn("shrink-0", slot.overflow)}>+{remaining}</span>
56 ) : null}
57 </span>
58 );
59 })}
60 </span>
61 );
62}
63
64export type PullRequestRowTarget = Pick<
65 EnvironmentPullRequestEntry,
66 "environmentId" | "projectId" | "host" | "repository" | "number"
67>;
68
69function PullRequestRowImpl({
70 entry,
71 selected,
72 showProjectTitle,
73 showProvider,
74 environmentLabel,
75 matchedElsewhere,
76 statsKey,
77 statsRef,
78 onSelect,
79}: {
80 entry: EnvironmentPullRequestEntry;
81 selected: boolean;
82 showProjectTitle: boolean;
83 /** Only when the list spans more than one host, where the repository alone is ambiguous. */
84 showProvider: boolean;
85 /** Names the server this row was read from, where the list spans more than one. */
86 environmentLabel?: string;
87 /**
88 * A search found this, but in something the row does not show — a description, a comment, a
89 * commit message. Saying so is the difference between a result and an apparently random row.
90 */
91 matchedElsewhere?: boolean;
92 /** Used by the list's shared visibility observer to defer optional line-count reads. */
93 statsKey?: string;
94 statsRef?: RefCallback<HTMLButtonElement>;
95 onSelect: (entry: PullRequestRowTarget) => void;
96}) {
97 const { Icon, providerName } = getSourceControlPresentationForKind(entry.provider);
98 return (
99 <button
100 ref={statsRef}
101 data-pull-request-stats-key={statsKey}
102 type="button"
103 aria-current={selected ? "true" : undefined}
104 onClick={() => onSelect(entry)}
105 className={cn(
106 "@container/pr-row grid w-full grid-cols-[auto_minmax(0,1fr)] items-center gap-3 rounded-lg px-3 py-3 text-left transition-colors focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring",
107 // Offscreen rows are skipped for style, layout and paint: a long list costs what the
108 // viewport shows, not what the pages have loaded. The intrinsic size keeps the
109 // scrollbar honest while a row is skipped.
110 "[contain-intrinsic-block-size:66px] [content-visibility:auto]",
111 selected ? "bg-accent" : "hover:bg-accent/60",
112 )}
113 >
114 <PullRequestStateGlyph
115 state={entry.state}
116 isDraft={entry.isDraft}
117 mergeability={entry.mergeability}
118 baseBranch={entry.baseBranch}
119 />
120 <span className="grid min-w-0 grid-cols-[minmax(0,1fr)_auto] items-center gap-x-3 gap-y-1.5">
121 <span className="col-start-1 row-start-1 block truncate text-sm font-medium text-foreground">
122 {entry.title}
123 </span>
124 <span className="col-start-2 row-start-1 flex items-center justify-self-end gap-2 text-xs">
125 {entry.stack ? (
126 <PullRequestStackPopover
127 environmentId={entry.environmentId}
128 reference={{
129 projectId: entry.projectId,
130 host: entry.host,
131 repository: entry.repository,
132 number: entry.number,
133 }}
134 membership={entry.stack}
135 onSelect={(target) =>
136 onSelect({ ...target, host: entry.host, environmentId: entry.environmentId })
137 }
138 />
139 ) : null}
140 <PullRequestDiffStat
141 additions={entry.additions}
142 deletions={entry.deletions}
143 className="shrink-0 whitespace-nowrap text-[11px]"
144 />
145 </span>
146 <PullRequestMetaLine className="@container/pr-row-meta col-start-1 row-start-2 overflow-hidden text-xs text-muted-foreground/70">
147 {matchedElsewhere ? (
148 <Tooltip>
149 <TooltipTrigger
150 render={
151 <span className="flex min-w-6 items-center gap-1 overflow-hidden rounded-full border border-border/60 px-1 text-[10px]" />
152 }
153 >
154 <span className="sr-only">matched in the description</span>
155 <SearchIcon aria-hidden className="size-3 shrink-0" />
156 <span aria-hidden className="hidden truncate @xs/pr-row-meta:block">
157 matched in the description
158 </span>
159 </TooltipTrigger>
160 <TooltipPopup side="top">Matched in the description</TooltipPopup>
161 </Tooltip>
162 ) : null}
163 <span className="flex shrink-0 items-center gap-1">
164 {showProvider ? (
165 <Tooltip>
166 <TooltipTrigger render={<span className="inline-flex shrink-0" />}>
167 <Icon aria-label={providerName} className="size-3" />
168 </TooltipTrigger>
169 <TooltipPopup>{providerName}</TooltipPopup>
170 </Tooltip>
171 ) : null}
172 {/* The number carries the link, here as much as on the detail: a right-click on it
173 copies the pull request's own address rather than opening the editing menu. */}
174 <span
175 onContextMenu={(event) => {
176 event.preventDefault();
177 event.stopPropagation();
178 void showPullRequestLinkContextMenu({
179 url: entry.url,
180 openLabel: openOnHostLabel(entry.provider),
181 position: { x: event.clientX, y: event.clientY },
182 });
183 }}
184 >
185 #{entry.number}
186 </span>
187 </span>
188 {showProjectTitle ? <span className="truncate">{entry.repository}</span> : null}
189 {environmentLabel ? (
190 <span className="min-w-0 max-w-32 truncate">{environmentLabel}</span>
191 ) : null}
192 <PullRequestActorLabel
193 actor={entry.author}
194 className="min-w-4 max-w-40"
195 labelClassName="sr-only @xs/pr-row-meta:not-sr-only @xs/pr-row-meta:truncate"
196 />
197 {entry.labels.length > 0 ? <PullRequestRowLabels labels={entry.labels} /> : null}
198 {/* Only a verdict somebody has actually given: "review required" is the absence of
199 one, and saying so on every unreviewed row would say nothing. */}
200 {entry.reviewDecision === "approved" ? (
201 <Tooltip>
202 <TooltipTrigger render={<span className="inline-flex shrink-0" />}>
203 <UserCheckIcon
204 aria-hidden
205 className="size-3.5 text-emerald-600/90 dark:text-emerald-400/80"
206 />
207 <span className="sr-only">Approved</span>
208 </TooltipTrigger>
209 <TooltipPopup>Approved</TooltipPopup>
210 </Tooltip>
211 ) : entry.reviewDecision === "changes-requested" ? (
212 <span className="min-w-0 truncate text-amber-600/90 dark:text-amber-400/80">
213 Changes requested
214 </span>
215 ) : null}
216 {entry.checksState === undefined ? null : (
217 <PullRequestChecksPopover
218 checksState={entry.checksState}
219 environmentId={entry.environmentId}
220 reference={{
221 projectId: entry.projectId,
222 repository: entry.repository,
223 number: entry.number,
224 }}
225 />
226 )}
227 </PullRequestMetaLine>
228 <span className="col-start-2 row-start-2 flex items-center justify-self-end gap-3 whitespace-nowrap text-[11px] text-muted-foreground/70 tabular-nums">
229 <span className="hidden @sm/pr-row:inline">
230 {formatRelativeTimeLabel(entry.updatedAt)}
231 </span>
232 </span>
233 </span>
234 </button>
235 );
236}
237
238/**
239 * Memoized: the list re-renders on every keystroke of a search and every status poll, and a
240 * row whose entry, selection and match state are unchanged has nothing new to say. Effective
241 * because the route hands it a stable `onSelect`.
242 */
243export const PullRequestRow = memo(PullRequestRowImpl);