Skip to content

Commit a1dc160

Browse files
TonybynMp4claude
andcommitted
fix(web): keep Resolve disabled when a thread is collapsed and reopened mid-request
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 7e8420f commit a1dc160

2 files changed

Lines changed: 34 additions & 7 deletions

File tree

‎apps/web/src/components/pullRequest/PullRequestResolveThreadButton.browser.test.tsx‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,13 @@ it("stays disabled when the detail is rebuilt before refreshed activity arrives"
5555
await screen.rerender(button(detailWith([{ ...thread, isResolved: true }])));
5656
await expect.element(screen.getByRole("button", { name: "Unresolve" })).toBeEnabled();
5757
});
58+
59+
it("stays disabled when its thread is collapsed and reopened before refreshed activity arrives", async () => {
60+
const threads = [{ ...thread, id: "reopened" }];
61+
await using screen = await render(button(detailWith(threads)));
62+
await screen.getByRole("button", { name: "Resolve" }).click();
63+
64+
await screen.rerender(<></>);
65+
await screen.rerender(button(detailWith(threads)));
66+
await expect.element(screen.getByRole("button", { name: "Resolve" })).toBeDisabled();
67+
});

‎apps/web/src/components/pullRequest/PullRequestResolveThreadButton.tsx‎

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,30 @@ import type {
55
PullRequestReviewThread,
66
} from "@t3tools/contracts";
77
import { canResolvePullRequestThread } from "@t3tools/client-runtime/pull-request-editing";
8-
import { useState } from "react";
8+
import { useSyncExternalStore } from "react";
99

1010
import { pullRequestEnvironment } from "~/state/pullRequests";
1111
import { useAtomCommand } from "~/state/use-atom-command";
1212

1313
import { Button } from "../ui/button";
1414
import { toastManager } from "../ui/toast";
1515

16+
type Sent = PullRequestDetailView["reviewThreads"] | "pending";
17+
18+
// Outlives the button, which unmounts when its thread collapses, so reopening it mid-request
19+
// cannot send a second one.
20+
const sentByThread = new Map<string, Sent>();
21+
const sentListeners = new Set<() => void>();
22+
const subscribeSent = (listener: () => void) => {
23+
sentListeners.add(listener);
24+
return () => sentListeners.delete(listener);
25+
};
26+
const setSentFor = (key: string, sent: Sent | null) => {
27+
if (sent === null) sentByThread.delete(key);
28+
else sentByThread.set(key, sent);
29+
for (const listener of sentListeners) listener();
30+
};
31+
1632
/**
1733
* Resolves or unresolves a thread from the conversation rather than from its line in the Code tab,
1834
* which a thread on the change request as a whole does not have. Callers put it on the thread's
@@ -33,8 +49,9 @@ export function PullRequestResolveThreadButton({
3349
}) {
3450
// Held until refreshed activity arrives: `detail` is rebuilt whenever the core detail reloads,
3551
// which can land before activity does and would re-enable the button on the old label.
36-
const [sent, setSent] = useState<PullRequestDetailView["reviewThreads"] | "pending" | null>(null);
37-
if (sent !== null && sent !== "pending" && sent !== detail.reviewThreads) setSent(null);
52+
const key = `${environmentId}:${thread.id}`;
53+
const sent = useSyncExternalStore(subscribeSent, () => sentByThread.get(key));
54+
const waiting = sent === "pending" || sent === detail.reviewThreads;
3855
const setThreadResolution = useAtomCommand(pullRequestEnvironment.setThreadResolution, {
3956
reportFailure: false,
4057
});
@@ -43,17 +60,17 @@ export function PullRequestResolveThreadButton({
4360
}
4461

4562
const toggle = async () => {
46-
setSent("pending");
63+
setSentFor(key, "pending");
4764
const result = await setThreadResolution({
4865
environmentId,
4966
input: { ...reference, threadId: thread.id, resolved: !thread.isResolved },
5067
});
5168
if (result._tag === "Failure") {
52-
setSent(null);
69+
setSentFor(key, null);
5370
toastManager.add({ type: "error", title: "The conversation could not be updated" });
5471
return;
5572
}
56-
setSent(detail.reviewThreads);
73+
setSentFor(key, detail.reviewThreads);
5774
onRefresh();
5875
};
5976

@@ -62,7 +79,7 @@ export function PullRequestResolveThreadButton({
6279
size="xs"
6380
variant="ghost"
6481
className="shrink-0"
65-
disabled={sent !== null}
82+
disabled={waiting}
6683
onClick={() => void toggle()}
6784
>
6885
{thread.isResolved ? "Unresolve" : "Resolve"}

0 commit comments

Comments
 (0)