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

PullRequestRow.tsx

239 lines9.0 KB

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