@@ -462,7 +462,7 @@ import {
462462 ThreadErrorBanner ,
463463} from "./chat/ThreadErrorBanner" ;
464464import { QueuedRunsControl , type QueuedRunsControlHandle } from "./chat/QueuedRunsControl" ;
465- import { useLinkedThreadPullRequest } from "./ThreadStatusIndicators " ;
465+ import { useThreadLinkedPullRequest } from "./pullRequest/useThreadLinkedPullRequest " ;
466466import type { ComposerBannerStackItem } from "./chat/ComposerBannerStack" ;
467467import { ThreadStatusLine } from "./chat/ThreadStatusLine" ;
468468import { formatRelativeTimeLabel , formatRelativeTimeUntilLabel } from "../timestampFormat" ;
@@ -4382,120 +4382,24 @@ export default function ChatView(props: ChatViewProps) {
43824382 const openFileSurfaceStable = useStableCallback ( openFileSurface ) ;
43834383 // The thread's own change request, placed against the project it belongs to. Without a
43844384 // project there is nothing to resolve it against, so the caller falls back to the browser.
4385- const persistedLinkedThreadPullRequest = isServerThread
4386- ? ( activeThreadShell ?. linkedPullRequest ?? activeThread ?. linkedPullRequest ?? null )
4387- : ( activeThread ?. linkedPullRequest ?? null ) ;
43884385 const activeProjectRepository = sourceControlRepositorySelector (
43894386 activeProject ?. repositoryIdentity ,
43904387 ) ;
4391- const persistedLinkedThreadPullRequestStatus = useLinkedThreadPullRequest (
4392- activeThreadRef ?. environmentId ?? null ,
4393- persistedLinkedThreadPullRequest ,
4394- ) ;
4395- const replacementLinkedThreadPullRequest = useMemo ( ( ) => {
4396- const detected = gitStatusQuery . data ?. pr ;
4397- const threadBranch = activeThread ?. branch ;
4398- const projectId = activeProject ?. id ;
4399- if (
4400- persistedLinkedThreadPullRequest === null ||
4401- ( persistedLinkedThreadPullRequestStatus ?. pr . state !== "merged" &&
4402- persistedLinkedThreadPullRequestStatus ?. pr . state !== "closed" ) ||
4403- gitStatusQuery . data ?. refName !== threadBranch ||
4404- detected ?. state !== "open" ||
4405- detected . headRef !== threadBranch ||
4406- projectId === undefined ||
4407- activeProjectRepository === null ||
4408- ( persistedLinkedThreadPullRequest . projectId === projectId &&
4409- persistedLinkedThreadPullRequest . repository . toLowerCase ( ) ===
4410- activeProjectRepository . toLowerCase ( ) &&
4411- persistedLinkedThreadPullRequest . number === detected . number )
4412- ) {
4413- return null ;
4414- }
4415- return {
4416- projectId,
4417- repository : activeProjectRepository ,
4418- number : detected . number ,
4419- url : detected . url ,
4420- } ;
4421- } , [
4422- activeProject ?. id ,
4423- activeProjectRepository ,
4424- activeThread ?. branch ,
4425- gitStatusQuery . data ,
4426- persistedLinkedThreadPullRequest ,
4427- persistedLinkedThreadPullRequestStatus ?. pr . state ,
4428- ] ) ;
4429- const linkedThreadPullRequest =
4430- replacementLinkedThreadPullRequest ?? persistedLinkedThreadPullRequest ;
4431- const linkedThreadPullRequestKey = linkedThreadPullRequest
4432- ? JSON . stringify ( [
4433- linkedThreadPullRequest . projectId ,
4434- linkedThreadPullRequest . repository ,
4435- linkedThreadPullRequest . number ,
4436- ] )
4437- : null ;
4438- const threadRepository = linkedThreadPullRequest ?. repository ?? activeProjectRepository ;
4439- const threadPrRelinkKeysRef = useRef ( new Map < string , string > ( ) ) ;
4440- const threadPrRelinkWriteRef = useRef ( Promise . resolve ( ) ) ;
4441- useEffect ( ( ) => {
4442- if ( ! isServerThread || activeThreadKey === null || activeThreadRef === null ) {
4443- return ;
4444- }
4445- if ( replacementLinkedThreadPullRequest === null ) {
4446- threadPrRelinkKeysRef . current . delete ( activeThreadKey ) ;
4447- return ;
4448- }
4449- const relinkKey = `${ replacementLinkedThreadPullRequest . projectId } :${ replacementLinkedThreadPullRequest . repository } #${ replacementLinkedThreadPullRequest . number } ` ;
4450- if ( threadPrRelinkKeysRef . current . get ( activeThreadKey ) === relinkKey ) return ;
4451- threadPrRelinkKeysRef . current . set ( activeThreadKey , relinkKey ) ;
4452- const openSurface = selectActiveRightPanelSurface (
4453- useRightPanelStore . getState ( ) . byThreadKey ,
4454- activeThreadRef ,
4455- ) ;
4456- if (
4457- openSurface ?. kind === "pull-request" &&
4458- persistedLinkedThreadPullRequest !== null &&
4459- openSurface . projectId === persistedLinkedThreadPullRequest . projectId &&
4460- openSurface . repository . toLowerCase ( ) ===
4461- persistedLinkedThreadPullRequest . repository . toLowerCase ( ) &&
4462- openSurface . number === persistedLinkedThreadPullRequest . number
4463- ) {
4464- useRightPanelStore
4465- . getState ( )
4466- . openPullRequest ( activeThreadRef , replacementLinkedThreadPullRequest ) ;
4467- }
4468-
4469- threadPrRelinkWriteRef . current = threadPrRelinkWriteRef . current . then ( async ( ) => {
4470- if ( threadPrRelinkKeysRef . current . get ( activeThreadKey ) !== relinkKey ) return ;
4471- const result = await updateThreadMetadata ( {
4472- environmentId : activeThreadRef . environmentId ,
4473- input : {
4474- threadId : activeThreadRef . threadId ,
4475- linkedPullRequest : replacementLinkedThreadPullRequest ,
4476- } ,
4477- } ) ;
4478- if ( threadPrRelinkKeysRef . current . get ( activeThreadKey ) !== relinkKey ) return ;
4479- if ( result . _tag !== "Failure" ) return ;
4480- threadPrRelinkKeysRef . current . delete ( activeThreadKey ) ;
4481- if ( isAtomCommandInterrupted ( result ) ) return ;
4482- toastManager . add (
4483- stackedThreadToast ( {
4484- type : "error" ,
4485- title : "Unable to update the thread pull request" ,
4486- description : chatActionErrorMessage ( squashAtomCommandFailure ( result ) ) ,
4487- } ) ,
4488- ) ;
4489- } ) ;
4490- } , [
4491- activeThreadKey ,
4492- activeThreadRef ,
4493- isServerThread ,
4494- persistedLinkedThreadPullRequest ,
4495- replacementLinkedThreadPullRequest ,
4496- updateThreadMetadata ,
4497- ] ) ;
4498- const hasLinkedPullRequestDetail = persistedLinkedThreadPullRequest !== null ;
4388+ const {
4389+ linked : linkedThreadPullRequest ,
4390+ linkedKey : linkedThreadPullRequestKey ,
4391+ hasDetail : hasLinkedPullRequestDetail ,
4392+ } = useThreadLinkedPullRequest ( {
4393+ serverThreadRef : isServerThread ? activeThreadRef : null ,
4394+ environmentId : activeThreadRef ?. environmentId ?? null ,
4395+ persisted : isServerThread
4396+ ? ( activeThreadShell ?. linkedPullRequest ?? activeThread ?. linkedPullRequest ?? null )
4397+ : ( activeThread ?. linkedPullRequest ?? null ) ,
4398+ projectId : activeProject ?. id ?? null ,
4399+ projectRepository : activeProjectRepository ,
4400+ threadBranch : activeThread ?. branch ?? null ,
4401+ gitStatus : gitStatusQuery . data ?? null ,
4402+ } ) ;
44994403 const proactivePullRequestsKey = pullRequestsSurfaceAvailable
45004404 ? JSON . stringify (
45014405 visiblePullRequests . map ( ( link ) => [ link . host , link . repository , link . number ] ) . sort ( ) ,
0 commit comments