Skip to content

Commit 604da87

Browse files
steventhannaclaude
andcommitted
feat(web): thread details show PR stacks as a rail and merge them whole
The thread details card flattened every linked pull request into one list, so stacked PRs looked unrelated, and its Merge button merged a single stacked layer. The detail panel already blocked that; the card skipped the guard. The card now groups each stack into a rail (tip on top, base branch at the bottom), folds long stacks around the current layer, and names the layer that blocks the merge. Rows other than the current one render from the stored snapshot instead of polling the host per row. usePullRequestStackGate holds the single-merge guard for both the panel and the card, and usePullRequestStackAction holds the stack merge flow that PullRequestStackMenu and the card share. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent f0fb0e8 commit 604da87

10 files changed

Lines changed: 850 additions & 239 deletions

‎apps/web/src/components/chat/ThreadDetailsPrRow.browser.test.tsx‎

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,32 @@ const state = vi.hoisted(() => ({
2222
checkoutError: null as Error | null,
2323
addToast: vi.fn(),
2424
updateToast: vi.fn(),
25+
serverConfigs: new Map() as Map<unknown, unknown>,
26+
stack: null as null | {
27+
readonly id: string;
28+
readonly number: number;
29+
readonly url: string;
30+
readonly base: string;
31+
readonly layers: ReadonlyArray<{
32+
number: number;
33+
headBranch: string;
34+
headSha: string;
35+
state: "open" | "merged";
36+
}>;
37+
},
2538
}));
2639

2740
vi.mock(import("~/state/entities"), async (importOriginal) => ({
2841
...(await importOriginal()),
29-
useServerConfigs: (() => new Map()) as never,
42+
useServerConfigs: (() => state.serverConfigs) as never,
43+
}));
44+
vi.mock(import("~/state/usePullRequestStack"), async (importOriginal) => ({
45+
...(await importOriginal()),
46+
usePullRequestStackGate: (() => ({
47+
stack: { data: state.stack, isFresh: true, error: null },
48+
supportsStackActions: state.stack !== null,
49+
canMergeSinglePullRequest: state.stack === null,
50+
})) as never,
3051
}));
3152
const queryData = () => ({
3253
projectId: ProjectId.make("project"),
@@ -112,6 +133,52 @@ afterEach(() => {
112133
vi.unstubAllGlobals();
113134
state.status = "success";
114135
state.extraStatus = null;
136+
state.serverConfigs = new Map();
137+
state.stack = null;
138+
});
139+
140+
it("offers to merge a stacked pull request only with its whole stack", () => {
141+
const environmentId = EnvironmentId.make("environment");
142+
state.serverConfigs = new Map([
143+
[environmentId, { environment: { capabilities: { pullRequests: true } } }],
144+
]);
145+
state.stack = {
146+
id: "stack",
147+
number: 7,
148+
url: "https://github.com/owner/repo/stacks/7",
149+
base: "main",
150+
layers: [
151+
{ number: 1, headBranch: "feature", headSha: "a1", state: "open" },
152+
{ number: 2, headBranch: "feature-2", headSha: "b2", state: "open" },
153+
],
154+
};
155+
act(() =>
156+
root.render(
157+
<ThreadDetailsPrRow
158+
environmentId={environmentId}
159+
pr={null}
160+
number={1}
161+
reference={{ host: "github.com", repository: "owner/repo", number: 1 }}
162+
status={null}
163+
project={{ id: ProjectId.make("project") } as never}
164+
label="Test PR"
165+
openAriaLabel="Open PR"
166+
onOpen={vi.fn()}
167+
/>,
168+
),
169+
);
170+
const labels = [...container.querySelectorAll("button")].map((button) =>
171+
button.textContent?.trim(),
172+
);
173+
expect(labels).toContain("Merge stack");
174+
expect(labels).not.toContain("Merge");
175+
176+
act(() =>
177+
[...container.querySelectorAll("button")]
178+
.find((button) => button.textContent?.trim() === "Merge stack")!
179+
.click(),
180+
);
181+
expect(document.body.textContent).toContain("Merge 2 pull requests?");
115182
});
116183

117184
it("requires a new merge click after passing checks become pending and pass again", () => {

‎apps/web/src/components/chat/ThreadDetailsPrRow.tsx‎

Lines changed: 96 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@ import { ThreadDetailsControl } from "./ThreadDetailsControl";
22
import { PullRequestGlyph } from "../pullRequest/pullRequestIcons";
33
import { selectEnvironmentQueryData } from "@t3tools/client-runtime/state/runtime";
44
import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell";
5-
import type { EnvironmentId, ProjectId, PullRequestRef } from "@t3tools/contracts";
5+
import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients";
6+
import type {
7+
EnvironmentId,
8+
ProjectId,
9+
PullRequestMergeMethod,
10+
PullRequestRef,
11+
PullRequestStack,
12+
} from "@t3tools/contracts";
613
import { sourceControlRepositorySelector } from "@t3tools/shared/sourceControl";
714
import {
815
ArrowUpRightIcon,
@@ -25,6 +32,7 @@ import {
2532
useEnvironmentQueryRefresh,
2633
useEnvironmentQuerySelector,
2734
} from "~/state/query";
35+
import { usePullRequestStackGate } from "~/state/usePullRequestStack";
2836

2937
import {
3038
buildFixFindingsHandoff,
@@ -37,6 +45,7 @@ import {
3745
resolveThreadPanelPullRequestAction,
3846
} from "../pullRequest/pullRequestDetail.logic";
3947
import { PullRequestChecksPopover } from "../pullRequest/PullRequestChecksPopover";
48+
import { usePullRequestStackAction } from "../pullRequest/usePullRequestStackAction";
4049
import {
4150
pullRequestChecksState,
4251
PullRequestCheckStatusIcon,
@@ -167,7 +176,27 @@ export function ThreadDetailsPrRow({
167176
const { handoff, startHandoff } = usePullRequestHandoffs({ environmentId, detail });
168177
const [confirmingMerge, setConfirmingMerge] = useState(false);
169178

170-
const rowAction = resolveThreadPanelPullRequestAction(detail);
179+
const stackGate = usePullRequestStackGate(environmentId, detail, reference);
180+
const panelAction = resolveThreadPanelPullRequestAction(detail);
181+
// A stacked layer merges with its stack, never alone; while the stack is unknown, offer neither.
182+
// The card merges the whole stack, the way `gh stack` does, so it targets the top unmerged layer.
183+
const stackTop = stackGate.stack.data?.layers.findLast((layer) => layer.state !== "merged");
184+
const stackMerge =
185+
panelAction === "merge" &&
186+
!stackGate.canMergeSinglePullRequest &&
187+
stackGate.stack.isFresh &&
188+
stackGate.stack.data !== null &&
189+
stackTop !== undefined &&
190+
reference !== null &&
191+
detail !== null
192+
? {
193+
stack: stackGate.stack.data,
194+
reference: { ...reference, number: stackTop.number },
195+
hostLabel: sourceControlClients.get(detail.provider).label,
196+
}
197+
: null;
198+
const rowAction =
199+
panelAction === "merge" && !stackGate.canMergeSinglePullRequest ? null : panelAction;
171200
if (confirmingMerge && rowAction !== "merge") {
172201
setConfirmingMerge(false);
173202
}
@@ -405,7 +434,7 @@ export function ThreadDetailsPrRow({
405434

406435
return (
407436
<>
408-
{detail !== null && (showChecks || trailingAction !== null || watching) ? (
437+
{detail !== null && (showChecks || trailingAction !== null || stackMerge || watching) ? (
409438
<div className="pb-2">
410439
{openRow(detail.title, "row")}
411440
<div className="-mt-1 flex h-6 min-w-0 items-center gap-0.5 ps-9 pe-1 text-xs text-muted-foreground">
@@ -441,6 +470,18 @@ export function ThreadDetailsPrRow({
441470
<TooltipPopup side="top">{trailingAction.tooltip}</TooltipPopup>
442471
</Tooltip>
443472
) : null}
473+
{stackMerge ? (
474+
<ThreadDetailsStackMergeControl
475+
environmentId={environmentId}
476+
{...stackMerge}
477+
mergeMethod={selectedMergeMethod}
478+
onActed={() => {
479+
detailQuery.refresh();
480+
checksQuery.refresh();
481+
onActed?.();
482+
}}
483+
/>
484+
) : null}
444485
</div>
445486
</div>
446487
) : watching ? (
@@ -482,3 +523,55 @@ export function ThreadDetailsPrRow({
482523
</>
483524
);
484525
}
526+
527+
function ThreadDetailsStackMergeControl({
528+
environmentId,
529+
stack,
530+
reference,
531+
hostLabel,
532+
mergeMethod,
533+
onActed,
534+
}: {
535+
environmentId: EnvironmentId;
536+
stack: PullRequestStack;
537+
reference: PullRequestRef;
538+
hostLabel: string;
539+
mergeMethod: PullRequestMergeMethod;
540+
onActed: () => void;
541+
}) {
542+
const action = usePullRequestStackAction({
543+
hostLabel,
544+
stack,
545+
reference,
546+
environmentId,
547+
canMerge: true,
548+
canRebase: false,
549+
mergeMethod,
550+
onActed,
551+
});
552+
return (
553+
<>
554+
<Tooltip>
555+
<TooltipTrigger
556+
render={
557+
<ThreadDetailsControl
558+
type="button"
559+
variant="ghost"
560+
size="sm"
561+
part="meta"
562+
tone="primary"
563+
disabled={action.mergeDisabled}
564+
onClick={() => action.request("merge")}
565+
/>
566+
}
567+
>
568+
{action.pending ? "Merging..." : "Merge stack"}
569+
</TooltipTrigger>
570+
<TooltipPopup side="top">
571+
Merge all {action.mergeLayers.length} pull requests in this stack into {stack.base}
572+
</TooltipPopup>
573+
</Tooltip>
574+
{action.dialog}
575+
</>
576+
);
577+
}

‎apps/web/src/components/chat/ThreadDetailsPrRows.browser.test.tsx‎

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ vi.mock(import("./ThreadDetailsPrRow"), async (importOriginal) => {
1919
) : (
2020
<div data-row={String(props.number)}>
2121
{props.onStopWatching ? (
22-
<button onClick={props.onStopWatching}>Stop watching #{props.number}</button>
22+
<button aria-label={`Stop watching #${props.number}`} onClick={props.onStopWatching} />
2323
) : null}
2424
</div>
2525
),
@@ -173,18 +173,30 @@ function toggle() {
173173
act(() => toggleButton()!.click());
174174
}
175175

176-
it("shows only the current pull request until the rest are asked for", () => {
176+
const linkedRows = () =>
177+
[...container.querySelectorAll('[aria-label^="https://github.com/pingdotgg/t3code/pull/"]')].map(
178+
(node) => node.getAttribute("aria-label")!.split("/").at(-1),
179+
);
180+
181+
it("shows the current stack as a rail and hides other pull requests until asked for", () => {
177182
render([other, bottom, top], top);
183+
expect(container.textContent).toContain("Stack of 2");
178184
expect(rows()).toEqual(["2"]);
179-
expect(toggleLabel()).toBe("Show 2 more");
185+
expect(linkedRows()).toEqual(["1"]);
186+
expect(toggleLabel()).toBe("Show 1 more");
180187

181188
toggle();
182-
expect(rows()).toEqual(["2", "1", "3"]);
189+
expect(linkedRows()).toEqual(["1", "3"]);
183190
expect(toggleLabel()).toBe("Show less");
184191

185192
toggle();
186-
expect(rows()).toEqual(["2"]);
187-
expect(toggleLabel()).toBe("Show 2 more");
193+
expect(linkedRows()).toEqual(["1"]);
194+
});
195+
196+
it("names the layer that keeps the stack from merging", () => {
197+
const failing = { ...bottom, snapshot: { ...bottom.snapshot!, checksState: "failing" as const } };
198+
render([failing, top], top);
199+
expect(container.textContent).toContain("#1 checks failing, so the stack cannot merge");
188200
});
189201

190202
it("keeps the single row untouched when the thread links one pull request", () => {
@@ -211,9 +223,11 @@ it("lets only watched pull requests stop their watch", () => {
211223
render([other, watched, top], top);
212224
toggle();
213225
const stopButtons = [...container.querySelectorAll("button")].filter((button) =>
214-
button.textContent?.startsWith("Stop watching"),
226+
button.getAttribute("aria-label")?.startsWith("Stop watching"),
215227
);
216-
expect(stopButtons.map((button) => button.textContent)).toEqual(["Stop watching #1"]);
228+
expect(stopButtons.map((button) => button.getAttribute("aria-label"))).toEqual([
229+
"Stop watching #1",
230+
]);
217231

218232
act(() => stopButtons[0]!.click());
219233
expect(watchCommand).toHaveBeenCalledWith({

0 commit comments

Comments
 (0)