Open History to see the last commit
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
178 state={pr.state}
179 className={cn(THREAD_DETAILS_PANEL_ICON_CLASS, status.colorClass)}
180 />
181 );
182
183 // Everything the host reported, at a glance. The row stays one line; the tooltip is where the
184 // rest of the answer lives — styled like the sidebar's thread tooltip, title above icon-led
185 // detail rows, so the two read as one family.
186 const rowTooltip =
187 detail === null || statePresentation === null ? (
188 <TooltipPopup side="top">{status.tooltip}</TooltipPopup>
189 ) : (
190 <TooltipPopup
191 side="top"
192 align="start"
193 sideOffset={4}
194 variant="glass"
195 className="max-w-80 text-left whitespace-normal [&_[data-slot=tooltip-viewport]]:p-0"
196 >
197 <div className="flex min-w-0 max-w-80 flex-col gap-2 p-[var(--floating-content-inset)]">
198 <div className="flex min-w-0 items-baseline gap-1.5 text-xs leading-none">
199 <span className="min-w-0 truncate font-medium text-foreground">{detail.title}</span>
200 <span className="shrink-0 text-muted-foreground">#{detail.number}</span>
201 </div>
202 <div className="grid gap-1.5 pl-0.5 text-xs text-muted-foreground">
203 <div className="flex min-w-0 items-center gap-2">
204 <statePresentation.Icon
205 aria-hidden
206 className={cn("size-3 shrink-0", statePresentation.toneClassName)}
207 />
208 <div className="min-w-0 truncate text-foreground/75">{statePresentation.label}</div>
209 </div>
210 <div className="flex min-w-0 items-center gap-2">
211 <GitBranchIcon className="size-3 shrink-0 stroke-muted-foreground" />
212 <div className="min-w-0 truncate text-foreground/75">
213 {detail.baseBranch} ← {detail.headBranch}
214 </div>
215 </div>
216 {detail.state === "open" && checksState !== "none" ? (
217 <div className="flex min-w-0 items-center gap-2">
218 <PullRequestCheckStatusIcon
219 status={
220 checksState === "failing"
221 ? "failure"
222 : checksState === "pending"
223 ? "pending"
224 : "success"
225 }
226 />
227 <div className="min-w-0 truncate text-foreground/75">
228 {describePullRequestChecks(detail.checks)}
229 </div>
230 </div>
231 ) : null}
232 {detail.isDraft && conflicting ? (
233 <div className="flex min-w-0 items-start gap-2 text-destructive">
234 <TriangleAlertIcon aria-hidden className="mt-0.5 size-3 shrink-0 stroke-current" />
235 <div className="min-w-0 flex-1 wrap-break-word leading-5">
236 Merge conflicts with {detail.baseBranch}
237 </div>
238 </div>
239 ) : null}
240 <div className="flex min-w-0 items-center gap-2">
241 <FileDiffIcon className="size-3 shrink-0 stroke-muted-foreground" />
242 <div className="min-w-0 flex items-baseline gap-1 truncate text-foreground/75">
243 {detail.changedFiles.toLocaleString()}{" "}
244 {detail.changedFiles === 1 ? "file" : "files"}
245 <PullRequestDiffStat additions={detail.additions} deletions={detail.deletions} />
246 </div>
247 </div>
248 </div>
249 </div>
250 </TooltipPopup>
251 );
252
253 const trailingAction =
254 rowAction === "resolve"
255 ? {
256 label: "Resolve",
257 pendingLabel: "Preparing...",
258 pending: handoff === "conflicts",
259 destructive: true,
260 suffix: <ArrowUpRightIcon aria-hidden className="size-3 shrink-0" />,
261 tooltip: "Check the branch out and resolve the conflicts in a new thread",
262 onClick: startResolveConflicts,
263 }
264 : rowAction === "ready"
265 ? {
266 label: "Ready",
267 pendingLabel: "Marking...",
268 pending: actionPending,
269 destructive: false,
270 suffix: null,
271 tooltip: "Mark this pull request as ready for review",
272 onClick: () => void perform("ready"),
273 }
274 : rowAction === "fix"
275 ? {
276 label: "Fix",
277 pendingLabel: "Preparing...",
278 pending: handoff === "findings",
279 destructive: true,
280 suffix: <ArrowUpRightIcon aria-hidden className="size-3 shrink-0" />,
281 tooltip: "Fix the failing checks in a new thread",
282 onClick: startFixChecks,
283 }
284 : rowAction === "merge"
285 ? {
286 label: "Merge",
287 pendingLabel: "Merging...",
288 pending: actionPending,
289 destructive: false,
290 suffix: null,
291 tooltip: `Merge this pull request (${selectedMergeMethod})`,
292 onClick: () => setConfirmingMerge(true),
293 }
294 : null;
295
296 const rowContent = (
297 <>
298 {icon}
299 <span className="min-w-0 flex-1 truncate text-left">{label}</span>
300 </>
301 );
302
303 return (
304 <>
305 {trailingAction || checksRunning ? (
306 <div className={THREAD_DETAILS_PANEL_LINK_SPLIT_GROUP_CLASS}>
307 <Tooltip>
308 <TooltipTrigger
309 render={
310 <Button
311 type="button"
312 variant="ghost"
313 size="sm"
314 className={THREAD_DETAILS_PANEL_LINK_SPLIT_PRIMARY_CLASS}
315 aria-label={openAriaLabel}
316 onClick={onOpen}
317 />
318 }
319 >
320 {rowContent}
321 </TooltipTrigger>
322 {rowTooltip}
323 </Tooltip>
324 <span aria-hidden="true" className={THREAD_DETAILS_PANEL_SPLIT_SEPARATOR_CLASS} />
325 {trailingAction ? (
326 <Tooltip>
327 <TooltipTrigger
328 render={
329 <Button
330 type="button"
331 variant="ghost"
332 size="sm"
333 className={cn(
334 THREAD_DETAILS_PANEL_LINK_SPLIT_ACTION_CLASS,
335 trailingAction.destructive &&
336 "text-destructive hover:text-destructive data-pressed:text-destructive",
337 )}
338 disabled={actionPending || handoff !== null}
339 onClick={trailingAction.onClick}
340 />
341 }
342 >
343 {trailingAction.pending ? trailingAction.pendingLabel : trailingAction.label}
344 {trailingAction.suffix}
345 </TooltipTrigger>
346 <TooltipPopup side="top">{trailingAction.tooltip}</TooltipPopup>
347 </Tooltip>
348 ) : (
349 // Checks are still running: the slot reports that instead of offering a merge that
350 // would race them. Not a button — there is nothing to press until they finish.
351 <Tooltip>
352 <TooltipTrigger
353 render={
354 <span className="flex h-9 shrink-0 cursor-default items-center gap-1.5 px-2.5 text-[13px] font-medium text-muted-foreground" />
355 }
356 >
357 <PullRequestCheckStatusIcon status="pending" />
358 <span className="tabular-nums">
359 {detail === null
360 ? null
361 : `${detail.checks.filter((check) => check.status === "pending").length}/${detail.checks.length}`}
362 </span>
363 </TooltipTrigger>
364 <TooltipPopup side="top">
365 {detail ? describePullRequestChecks(detail.checks) : "Checks are running"}
366 </TooltipPopup>
367 </Tooltip>
368 )}
369 </div>
370 ) : (
371 <Tooltip>
372 <TooltipTrigger
373 render={
374 <Button
375 type="button"
376 variant="ghost"
377 size="sm"
378 className={THREAD_DETAILS_PANEL_ROW_CLASS}
379 aria-label={openAriaLabel}
380 onClick={onOpen}
381 />
382 }
383 >
384 {rowContent}
385 </TooltipTrigger>
386 {rowTooltip}
387 </Tooltip>
388 )}
389 {rowAction === "merge" ? (
390 <AlertDialog open={confirmingMerge} onOpenChange={(open) => setConfirmingMerge(open)}>
391 <AlertDialogPopup>
392 <AlertDialogHeader>
393 <AlertDialogTitle>Merge pull request?</AlertDialogTitle>
394 <AlertDialogDescription>
395 This merges #{pr.number} using {selectedMergeMethod}.
396 </AlertDialogDescription>
397 </AlertDialogHeader>
398 <AlertDialogFooter>
399 <AlertDialogClose render={<Button variant="outline" size="sm" />}>
400 Cancel
401 </AlertDialogClose>
402 <Button
403 size="sm"
404 disabled={actionPending}
405 onClick={() => {
406 setConfirmingMerge(false);
407 void perform("merge", selectedMergeMethod);
408 }}
409 >
410 Merge
411 </Button>
412 </AlertDialogFooter>
413 </AlertDialogPopup>
414 </AlertDialog>
415 ) : null}
416 </>
417 );
418}