Skip to content

Commit 2a9efb2

Browse files
committed
fix(web): keep pending pull request actions scoped to each request
- Add browser coverage for pending merges across thread navigation
1 parent 05c0ed7 commit 2a9efb2

2 files changed

Lines changed: 91 additions & 7 deletions

File tree

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import { EnvironmentId, ProjectId, type PullRequestRef } from "@t3tools/contracts";
2+
import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients";
3+
import { expect, it, vi } from "vite-plus/test";
4+
import { userEvent } from "vite-plus/test/browser";
5+
6+
import { act, render } from "../../../test/render";
7+
import { usePullRequestActionRunner } from "./usePullRequestActions";
8+
9+
const state = vi.hoisted(() => ({ runAction: vi.fn() }));
10+
vi.mock(import("~/state/use-atom-command"), () => ({
11+
useAtomCommand: (() => state.runAction) as never,
12+
}));
13+
14+
function deferred<T>() {
15+
let resolve!: (value: T) => void;
16+
const promise = new Promise<T>((done) => {
17+
resolve = done;
18+
});
19+
return { promise, resolve };
20+
}
21+
22+
function MergeButton({ reference }: { reference: PullRequestRef }) {
23+
const { actionPending, perform } = usePullRequestActionRunner({
24+
environmentId: EnvironmentId.make("environment"),
25+
reference,
26+
terminology: sourceControlClients.get(undefined).changeRequest,
27+
});
28+
return (
29+
<button type="button" disabled={actionPending} onClick={() => void perform("merge")}>
30+
{actionPending ? "Merging..." : "Merge"}
31+
</button>
32+
);
33+
}
34+
35+
it("keeps pending merges with their pull request when navigating between threads", async () => {
36+
const first: PullRequestRef = {
37+
projectId: ProjectId.make("project"),
38+
repository: "owner/repo",
39+
number: 1,
40+
};
41+
const second = { ...first, number: 2 };
42+
const firstResult = deferred<{ _tag: "Success"; value: void }>();
43+
const secondResult = deferred<{ _tag: "Success"; value: void }>();
44+
state.runAction
45+
.mockReset()
46+
.mockReturnValueOnce(firstResult.promise)
47+
.mockReturnValueOnce(secondResult.promise);
48+
await using screen = await render(<MergeButton reference={first} />);
49+
const button = screen.getByRole("button");
50+
51+
await userEvent.click(button);
52+
await expect.element(button).toHaveTextContent("Merging...");
53+
await screen.rerender(<MergeButton reference={second} />);
54+
await expect.element(button).toHaveTextContent("Merge");
55+
await expect.element(button).toBeEnabled();
56+
await userEvent.click(button);
57+
expect(state.runAction).toHaveBeenCalledTimes(2);
58+
59+
await screen.rerender(<MergeButton reference={first} />);
60+
await expect.element(button).toHaveTextContent("Merging...");
61+
await expect.element(button).toBeDisabled();
62+
await act(async () => {
63+
firstResult.resolve({ _tag: "Success", value: undefined });
64+
await firstResult.promise;
65+
});
66+
await expect.element(button).toHaveTextContent("Merge");
67+
await screen.rerender(<MergeButton reference={second} />);
68+
await expect.element(button).toHaveTextContent("Merging...");
69+
await act(async () => {
70+
secondResult.resolve({ _tag: "Success", value: undefined });
71+
await secondResult.promise;
72+
});
73+
await expect.element(button).toHaveTextContent("Merge");
74+
});

‎apps/web/src/components/pullRequest/usePullRequestActions.ts‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,23 @@ export function usePullRequestActionRunner({
140140
const runAction = useAtomCommand(pullRequestEnvironment.runAction, {
141141
reportFailure: false,
142142
});
143-
const [actionPending, setActionPending] = useState(false);
144-
const pendingRef = useRef(false);
143+
const pendingRef = useRef(new Set<string>());
144+
const [pendingKeys, setPendingKeys] = useState<ReadonlySet<string>>(() => new Set());
145+
const actionKey =
146+
reference === null
147+
? null
148+
: JSON.stringify([
149+
environmentId,
150+
reference.host ?? null,
151+
reference.repository,
152+
reference.number,
153+
]);
154+
const actionPending = actionKey !== null && pendingKeys.has(actionKey);
145155

146156
const perform = async (action: PullRequestAction, method?: PullRequestMergeMethod) => {
147-
if (pendingRef.current || reference === null) return;
148-
pendingRef.current = true;
149-
setActionPending(true);
157+
if (actionKey === null || reference === null || pendingRef.current.has(actionKey)) return;
158+
pendingRef.current.add(actionKey);
159+
setPendingKeys(new Set(pendingRef.current));
150160
const result = await runAction({
151161
environmentId,
152162
input: {
@@ -155,8 +165,8 @@ export function usePullRequestActionRunner({
155165
...(method ? { mergeMethod: method } : resolveMergeMethod ? { resolveMergeMethod } : {}),
156166
},
157167
});
158-
pendingRef.current = false;
159-
setActionPending(false);
168+
pendingRef.current.delete(actionKey);
169+
setPendingKeys(new Set(pendingRef.current));
160170
if (result._tag === "Failure") {
161171
toastManager.add({
162172
type: "error",

0 commit comments

Comments
 (0)