@@ -5,14 +5,30 @@ import type {
55 PullRequestReviewThread ,
66} from "@t3tools/contracts" ;
77import { canResolvePullRequestThread } from "@t3tools/client-runtime/pull-request-editing" ;
8- import { useState } from "react" ;
8+ import { useSyncExternalStore } from "react" ;
99
1010import { pullRequestEnvironment } from "~/state/pullRequests" ;
1111import { useAtomCommand } from "~/state/use-atom-command" ;
1212
1313import { Button } from "../ui/button" ;
1414import { 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