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

ThreadDetailsPrRow.tsx

415 lines15.5 KB

1/**
2 * The thread details panel's pull request row: what the branch's pull request is, and the one
3 * thing worth doing to it right now.
4 *
5 * The row itself opens the pull request in the right panel, exactly as it always has. Around
6 * that, the host's richer answer collapses into a single trailing slot, ranked by what unblocks
7 * the merge next: "Resolve" on conflicts, "Ready" on a draft, "Fix" under failing checks, and
8 * "Merge" only once the branch is clean and its checks pass. While checks run the slot reports
9 * that instead — a merge offered mid-run would race the very runs that gate it. Everything else
10 * the host knows (title, state, checks tally, diff size) lives in the row's tooltip, so a hover
11 * answers what previously took opening the panel.
12 *
13 * Until the detail arrives — or where pull requests are not supported at all — the row renders
14 * from the `vcs.status` summary alone, which is the plain row this panel showed before.
15 */
16import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell";
17import type { EnvironmentId, ProjectId, PullRequestRef } from "@t3tools/contracts";
18import { ArrowUpRightIcon, FileDiffIcon, GitBranchIcon, TriangleAlertIcon } from "lucide-react";
19import { useState, type MouseEvent as ReactMouseEvent } from "react";
20​
21import { cn } from "~/lib/utils";
22import { useServerConfigs } from "~/state/entities";
23import { pullRequestEnvironment } from "~/state/pullRequests";
24import { useEnvironmentQuery } from "~/state/query";
25​
26import {
27 buildFixFindingsHandoff,
28 buildResolveConflictsPrompt,
29 classifyPullRequestChecks,
30 describePullRequestChecks,
31 isPullRequestConflicting,
32 resolveSelectedMergeMethod,
33 allowedPullRequestMergeMethods,
34 resolveThreadPanelPullRequestAction,
35} from "../pullRequest/pullRequestDetail.logic";
36import {
37 PullRequestCheckStatusIcon,
38 PullRequestDiffStat,
39 resolvePullRequestState,
40} from "../pullRequest/pullRequestPresentation";
41import {
42 usePullRequestActionRunner,
43 usePullRequestHandoffs,
44} from "../pullRequest/usePullRequestActions";
45import {
46 ChangeRequestStatusIcon,
47 type PrStatusIndicator,
48 type ThreadPr,
49} from "../ThreadStatusIndicators";
50import {
51 AlertDialog,
52 AlertDialogClose,
53 AlertDialogDescription,
54 AlertDialogFooter,
55 AlertDialogHeader,
56 AlertDialogPopup,
57 AlertDialogTitle,
58} from "../ui/alert-dialog";
59import { Button } from "../ui/button";
60import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
61import {
62 THREAD_DETAILS_PANEL_ICON_CLASS,
63 THREAD_DETAILS_PANEL_LINK_SPLIT_ACTION_CLASS,
64 THREAD_DETAILS_PANEL_LINK_SPLIT_GROUP_CLASS,
65 THREAD_DETAILS_PANEL_LINK_SPLIT_PRIMARY_CLASS,
66 THREAD_DETAILS_PANEL_ROW_CLASS,
67 THREAD_DETAILS_PANEL_SPLIT_SEPARATOR_CLASS,
68} from "./threadDetailsPanelStyles";
69​
70export function ThreadDetailsPrRow({
71 environmentId,
72 pr,
73 status,
74 project,
75 label,
76 openAriaLabel,
77 onOpen,
78 onActed,
79}: {
80 environmentId: EnvironmentId;
81 pr: NonNullable<ThreadPr>;
82 status: PrStatusIndicator;
83 /** The thread's project, which is what the pull request is read through on the host. */
84 project: EnvironmentProject | null;
85 label: string;
86 openAriaLabel: string;
87 onOpen: (event: ReactMouseEvent<HTMLElement>) => void;
88 /** An action changed the pull request on the host, so the vcs status behind the row is stale. */
89 onActed?: () => void;
90}) {
91 const serverConfigs = useServerConfigs();
92 const supportsPullRequests =
93 serverConfigs.get(environmentId)?.environment.capabilities.pullRequests === true;
94 // The identity's own spelling, the way the detail panel is addressed everywhere else.
95 const identity = project?.repositoryIdentity;
96 const repository =
97 identity?.displayName ??
98 (identity?.owner && identity.name ? `${identity.owner}/${identity.name}` : null);
99 const reference: PullRequestRef | null =
100 supportsPullRequests && project !== null && repository !== null
101 ? { projectId: project.id as ProjectId, repository, number: pr.number }
102 : null;
103 const detailQuery = useEnvironmentQuery(
104 reference === null ? null : pullRequestEnvironment.detail({ environmentId, input: reference }),
105 );
106 const detail = detailQuery.data ?? null;
107​
108 const { actionPending, perform } = usePullRequestActionRunner({
109 environmentId,
110 reference,
111 onSuccess: () => {
112 detailQuery.refresh();
113 onActed?.();
114 },
115 });
116 const { handoff, startHandoff } = usePullRequestHandoffs({ environmentId, detail });
117 const [confirmingMerge, setConfirmingMerge] = useState(false);
118​
119 const rowAction = resolveThreadPanelPullRequestAction(detail);
120 const conflicting = isPullRequestConflicting(detail);
121 const checksState = detail === null ? "none" : classifyPullRequestChecks(detail.checks);
122 const checksRunning = detail?.state === "open" && rowAction === null && checksState === "pending";
123 const selectedMergeMethod = resolveSelectedMergeMethod(
124 allowedPullRequestMergeMethods(detail),
125 "merge",
126 );
127​
128 const startResolveConflicts = () => {
129 if (detail === null) return;
130 void startHandoff("conflicts", {
131 prompt: buildResolveConflictsPrompt({
132 number: detail.number,
133 url: detail.url,
134 headBranch: detail.headBranch,
135 baseBranch: detail.baseBranch,
136 }),
137 });
138 };
139​
140 const startFixChecks = () => {
141 if (detail === null) return;
142 // The compact row fetches no conversation, so the handoff carries the failing checks alone;
143 // review threads keep arriving through the full panel's richer version of this action.
144 void startHandoff(
145 "findings",
146 buildFixFindingsHandoff({
147 number: detail.number,
148 title: detail.title,
149 url: detail.url,
150 headBranch: detail.headBranch,
151 baseBranch: detail.baseBranch,
152 reviewThreads: [],
153 comments: [],
154 checks: detail.checks,
155 commentsTruncated: false,
156 }),
157 );
158 };
159​
160 // Once the host has answered, the glyph knows about drafts and conflicts, which the vcs
161 // summary does not. Draft outranks conflicts in it, same as the detail panel.
162 const statePresentation =
163 detail === null
164 ? null
165 : resolvePullRequestState({
166 state: detail.state,
167 isDraft: detail.isDraft,
168 mergeability: detail.mergeability,
169 baseBranch: detail.baseBranch,
170 });
171 const icon = statePresentation ? (
172 <statePresentation.Icon
173 aria-hidden
174 className={cn("-mx-0.5 size-4 shrink-0", statePresentation.toneClassName)}
175 />
176 ) : (
177 <ChangeRequestStatusIcon className={cn(THREAD_DETAILS_PANEL_ICON_CLASS, status.colorClass)} />
178 );
179​
180 // Everything the host reported, at a glance. The row stays one line; the tooltip is where the
181 // rest of the answer lives — styled like the sidebar's thread tooltip, title above icon-led
182 // detail rows, so the two read as one family.
183 const rowTooltip =
184 detail === null || statePresentation === null ? (
185 <TooltipPopup side="top">{status.tooltip}</TooltipPopup>
186 ) : (
187 <TooltipPopup
188 side="top"
189 align="start"
190 sideOffset={4}
191 variant="glass"
192 className="max-w-80 text-left whitespace-normal [&_[data-slot=tooltip-viewport]]:p-0"
193 >
194 <div className="flex min-w-0 max-w-80 flex-col gap-2 p-[var(--floating-content-inset)]">
195 <div className="flex min-w-0 items-baseline gap-1.5 text-xs leading-none">
196 <span className="min-w-0 truncate font-medium text-foreground">{detail.title}</span>
197 <span className="shrink-0 text-muted-foreground">#{detail.number}</span>
198 </div>
199 <div className="grid gap-1.5 pl-0.5 text-xs text-muted-foreground">
200 <div className="flex min-w-0 items-center gap-2">
201 <statePresentation.Icon
202 aria-hidden
203 className={cn("size-3 shrink-0", statePresentation.toneClassName)}
204 />
205 <div className="min-w-0 truncate text-foreground/75">{statePresentation.label}</div>
206 </div>
207 <div className="flex min-w-0 items-center gap-2">
208 <GitBranchIcon className="size-3 shrink-0 stroke-muted-foreground" />
209 <div className="min-w-0 truncate text-foreground/75">
210 {detail.baseBranch} ← {detail.headBranch}
211 </div>
212 </div>
213 {detail.state === "open" && checksState !== "none" ? (
214 <div className="flex min-w-0 items-center gap-2">
215 <PullRequestCheckStatusIcon
216 status={
217 checksState === "failing"
218 ? "failure"
219 : checksState === "pending"
220 ? "pending"
221 : "success"
222 }
223 />
224 <div className="min-w-0 truncate text-foreground/75">
225 {describePullRequestChecks(detail.checks)}
226 </div>
227 </div>
228 ) : null}
229 {detail.isDraft && conflicting ? (
230 <div className="flex min-w-0 items-start gap-2 text-destructive">
231 <TriangleAlertIcon aria-hidden className="mt-0.5 size-3 shrink-0 stroke-current" />
232 <div className="min-w-0 flex-1 wrap-break-word leading-5">
233 Merge conflicts with {detail.baseBranch}
234 </div>
235 </div>
236 ) : null}
237 <div className="flex min-w-0 items-center gap-2">
238 <FileDiffIcon className="size-3 shrink-0 stroke-muted-foreground" />
239 <div className="min-w-0 flex items-baseline gap-1 truncate text-foreground/75">
240 {detail.changedFiles.toLocaleString()}{" "}
241 {detail.changedFiles === 1 ? "file" : "files"}
242 <PullRequestDiffStat additions={detail.additions} deletions={detail.deletions} />
243 </div>
244 </div>
245 </div>
246 </div>
247 </TooltipPopup>
248 );
249​
250 const trailingAction =
251 rowAction === "resolve"
252 ? {
253 label: "Resolve",
254 pendingLabel: "Preparing...",
255 pending: handoff === "conflicts",
256 destructive: true,
257 suffix: <ArrowUpRightIcon aria-hidden className="size-3 shrink-0" />,
258 tooltip: "Check the branch out and resolve the conflicts in a new thread",
259 onClick: startResolveConflicts,
260 }
261 : rowAction === "ready"
262 ? {
263 label: "Ready",
264 pendingLabel: "Marking...",
265 pending: actionPending,
266 destructive: false,
267 suffix: null,
268 tooltip: "Mark this pull request as ready for review",
269 onClick: () => void perform("ready"),
270 }
271 : rowAction === "fix"
272 ? {
273 label: "Fix",
274 pendingLabel: "Preparing...",
275 pending: handoff === "findings",
276 destructive: true,
277 suffix: <ArrowUpRightIcon aria-hidden className="size-3 shrink-0" />,
278 tooltip: "Fix the failing checks in a new thread",
279 onClick: startFixChecks,
280 }
281 : rowAction === "merge"
282 ? {
283 label: "Merge",
284 pendingLabel: "Merging...",
285 pending: actionPending,
286 destructive: false,
287 suffix: null,
288 tooltip: `Merge this pull request (${selectedMergeMethod})`,
289 onClick: () => setConfirmingMerge(true),
290 }
291 : null;
292​
293 const rowContent = (
294 <>
295 {icon}
296 <span className="min-w-0 flex-1 truncate text-left">{label}</span>
297 </>
298 );
299​
300 return (
301 <>
302 {trailingAction || checksRunning ? (
303 <div className={THREAD_DETAILS_PANEL_LINK_SPLIT_GROUP_CLASS}>
304 <Tooltip>
305 <TooltipTrigger
306 render={
307 <Button
308 type="button"
309 variant="ghost"
310 size="sm"
311 className={THREAD_DETAILS_PANEL_LINK_SPLIT_PRIMARY_CLASS}
312 aria-label={openAriaLabel}
313 onClick={onOpen}
314 />
315 }
316 >
317 {rowContent}
318 </TooltipTrigger>
319 {rowTooltip}
320 </Tooltip>
321 <span aria-hidden="true" className={THREAD_DETAILS_PANEL_SPLIT_SEPARATOR_CLASS} />
322 {trailingAction ? (
323 <Tooltip>
324 <TooltipTrigger
325 render={
326 <Button
327 type="button"
328 variant="ghost"
329 size="sm"
330 className={cn(
331 THREAD_DETAILS_PANEL_LINK_SPLIT_ACTION_CLASS,
332 trailingAction.destructive &&
333 "text-destructive hover:text-destructive data-pressed:text-destructive",
334 )}
335 disabled={actionPending || handoff !== null}
336 onClick={trailingAction.onClick}
337 />
338 }
339 >
340 {trailingAction.pending ? trailingAction.pendingLabel : trailingAction.label}
341 {trailingAction.suffix}
342 </TooltipTrigger>
343 <TooltipPopup side="top">{trailingAction.tooltip}</TooltipPopup>
344 </Tooltip>
345 ) : (
346 // Checks are still running: the slot reports that instead of offering a merge that
347 // would race them. Not a button — there is nothing to press until they finish.
348 <Tooltip>
349 <TooltipTrigger
350 render={
351 <span className="flex h-9 shrink-0 cursor-default items-center gap-1.5 px-2.5 text-[13px] font-medium text-muted-foreground" />
352 }
353 >
354 <PullRequestCheckStatusIcon status="pending" />
355 <span className="tabular-nums">
356 {detail === null
357 ? null
358 : `${detail.checks.filter((check) => check.status === "pending").length}/${detail.checks.length}`}
359 </span>
360 </TooltipTrigger>
361 <TooltipPopup side="top">
362 {detail ? describePullRequestChecks(detail.checks) : "Checks are running"}
363 </TooltipPopup>
364 </Tooltip>
365 )}
366 </div>
367 ) : (
368 <Tooltip>
369 <TooltipTrigger
370 render={
371 <Button
372 type="button"
373 variant="ghost"
374 size="sm"
375 className={THREAD_DETAILS_PANEL_ROW_CLASS}
376 aria-label={openAriaLabel}
377 onClick={onOpen}
378 />
379 }
380 >
381 {rowContent}
382 </TooltipTrigger>
383 {rowTooltip}
384 </Tooltip>
385 )}
386 {rowAction === "merge" ? (
387 <AlertDialog open={confirmingMerge} onOpenChange={(open) => setConfirmingMerge(open)}>
388 <AlertDialogPopup>
389 <AlertDialogHeader>
390 <AlertDialogTitle>Merge pull request?</AlertDialogTitle>
391 <AlertDialogDescription>
392 This merges #{pr.number} using {selectedMergeMethod}.
393 </AlertDialogDescription>
394 </AlertDialogHeader>
395 <AlertDialogFooter>
396 <AlertDialogClose render={<Button variant="outline" size="sm" />}>
397 Cancel
398 </AlertDialogClose>
399 <Button
400 size="sm"
401 disabled={actionPending}
402 onClick={() => {
403 setConfirmingMerge(false);
404 void perform("merge", selectedMergeMethod);
405 }}
406 >
407 Merge
408 </Button>
409 </AlertDialogFooter>
410 </AlertDialogPopup>
411 </AlertDialog>
412 ) : null}
413 </>
414 );