@@ -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" ;
@@ -4244,120 +4244,26 @@ export default function ChatView(props: ChatViewProps) {
42444244 useRightPanelStore . getState ( ) . openFile ( activeThreadRef , relativePath ) ;
42454245 } ;
42464246 const openFileSurfaceStable = useStableCallback ( openFileSurface ) ;
4247- const persistedLinkedThreadPullRequest = isServerThread
4248- ? ( activeThreadShell ?. linkedPullRequest ?? activeThread ?. linkedPullRequest ?? null )
4249- : ( activeThread ?. linkedPullRequest ?? null ) ;
4247+ // The thread's own change request, placed against the project it belongs to. Without a
4248+ // project there is nothing to resolve it against, so the caller falls back to the browser.
42504249 const activeProjectRepository = sourceControlRepositorySelector (
42514250 activeProject ?. repositoryIdentity ,
42524251 ) ;
4253- const persistedLinkedThreadPullRequestStatus = useLinkedThreadPullRequest (
4254- activeThreadRef ?. environmentId ?? null ,
4255- persistedLinkedThreadPullRequest ,
4256- ) ;
4257- const replacementLinkedThreadPullRequest = useMemo ( ( ) => {
4258- const detected = gitStatusQuery . data ?. pr ;
4259- const threadBranch = activeThread ?. branch ;
4260- const projectId = activeProject ?. id ;
4261- if (
4262- persistedLinkedThreadPullRequest === null ||
4263- ( persistedLinkedThreadPullRequestStatus ?. pr . state !== "merged" &&
4264- persistedLinkedThreadPullRequestStatus ?. pr . state !== "closed" ) ||
4265- gitStatusQuery . data ?. refName !== threadBranch ||
4266- detected ?. state !== "open" ||
4267- detected . headRef !== threadBranch ||
4268- projectId === undefined ||
4269- activeProjectRepository === null ||
4270- ( persistedLinkedThreadPullRequest . projectId === projectId &&
4271- persistedLinkedThreadPullRequest . repository . toLowerCase ( ) ===
4272- activeProjectRepository . toLowerCase ( ) &&
4273- persistedLinkedThreadPullRequest . number === detected . number )
4274- ) {
4275- return null ;
4276- }
4277- return {
4278- projectId,
4279- repository : activeProjectRepository ,
4280- number : detected . number ,
4281- url : detected . url ,
4282- } ;
4283- } , [
4284- activeProject ?. id ,
4285- activeProjectRepository ,
4286- activeThread ?. branch ,
4287- gitStatusQuery . data ,
4288- persistedLinkedThreadPullRequest ,
4289- persistedLinkedThreadPullRequestStatus ?. pr . state ,
4290- ] ) ;
4291- const linkedThreadPullRequest =
4292- replacementLinkedThreadPullRequest ?? persistedLinkedThreadPullRequest ;
4293- const linkedThreadPullRequestKey = linkedThreadPullRequest
4294- ? JSON . stringify ( [
4295- linkedThreadPullRequest . projectId ,
4296- linkedThreadPullRequest . repository ,
4297- linkedThreadPullRequest . number ,
4298- ] )
4299- : null ;
4300- const threadRepository = linkedThreadPullRequest ?. repository ?? activeProjectRepository ;
4301- const threadPrRelinkKeysRef = useRef ( new Map < string , string > ( ) ) ;
4302- const threadPrRelinkWriteRef = useRef ( Promise . resolve ( ) ) ;
4303- useEffect ( ( ) => {
4304- if ( ! isServerThread || activeThreadKey === null || activeThreadRef === null ) {
4305- return ;
4306- }
4307- if ( replacementLinkedThreadPullRequest === null ) {
4308- threadPrRelinkKeysRef . current . delete ( activeThreadKey ) ;
4309- return ;
4310- }
4311- const relinkKey = `${ replacementLinkedThreadPullRequest . projectId } :${ replacementLinkedThreadPullRequest . repository } #${ replacementLinkedThreadPullRequest . number } ` ;
4312- if ( threadPrRelinkKeysRef . current . get ( activeThreadKey ) === relinkKey ) return ;
4313- threadPrRelinkKeysRef . current . set ( activeThreadKey , relinkKey ) ;
4314- const openSurface = selectActiveRightPanelSurface (
4315- useRightPanelStore . getState ( ) . byThreadKey ,
4316- activeThreadRef ,
4317- ) ;
4318- if (
4319- openSurface ?. kind === "pull-request" &&
4320- persistedLinkedThreadPullRequest !== null &&
4321- openSurface . projectId === persistedLinkedThreadPullRequest . projectId &&
4322- openSurface . repository . toLowerCase ( ) ===
4323- persistedLinkedThreadPullRequest . repository . toLowerCase ( ) &&
4324- openSurface . number === persistedLinkedThreadPullRequest . number
4325- ) {
4326- useRightPanelStore
4327- . getState ( )
4328- . openPullRequest ( activeThreadRef , replacementLinkedThreadPullRequest ) ;
4329- }
4330-
4331- threadPrRelinkWriteRef . current = threadPrRelinkWriteRef . current . then ( async ( ) => {
4332- if ( threadPrRelinkKeysRef . current . get ( activeThreadKey ) !== relinkKey ) return ;
4333- const result = await updateThreadMetadata ( {
4334- environmentId : activeThreadRef . environmentId ,
4335- input : {
4336- threadId : activeThreadRef . threadId ,
4337- linkedPullRequest : replacementLinkedThreadPullRequest ,
4338- } ,
4339- } ) ;
4340- if ( threadPrRelinkKeysRef . current . get ( activeThreadKey ) !== relinkKey ) return ;
4341- if ( result . _tag !== "Failure" ) return ;
4342- threadPrRelinkKeysRef . current . delete ( activeThreadKey ) ;
4343- if ( isAtomCommandInterrupted ( result ) ) return ;
4344- toastManager . add (
4345- stackedThreadToast ( {
4346- type : "error" ,
4347- title : "Unable to update the thread pull request" ,
4348- description : chatActionErrorMessage ( squashAtomCommandFailure ( result ) ) ,
4349- } ) ,
4350- ) ;
4351- } ) ;
4352- } , [
4353- activeThreadKey ,
4354- activeThreadRef ,
4355- isServerThread ,
4356- persistedLinkedThreadPullRequest ,
4357- replacementLinkedThreadPullRequest ,
4358- updateThreadMetadata ,
4359- ] ) ;
4360- const hasLinkedPullRequestDetail = persistedLinkedThreadPullRequest !== null ;
4252+ const {
4253+ linked : linkedThreadPullRequest ,
4254+ linkedKey : linkedThreadPullRequestKey ,
4255+ hasDetail : hasLinkedPullRequestDetail ,
4256+ } = useThreadLinkedPullRequest ( {
4257+ serverThreadRef : isServerThread ? activeThreadRef : null ,
4258+ environmentId : activeThreadRef ?. environmentId ?? null ,
4259+ persisted : isServerThread
4260+ ? ( activeThreadShell ?. linkedPullRequest ?? activeThread ?. linkedPullRequest ?? null )
4261+ : ( activeThread ?. linkedPullRequest ?? null ) ,
4262+ projectId : activeProject ?. id ?? null ,
4263+ projectRepository : activeProjectRepository ,
4264+ threadBranch : activeThread ?. branch ?? null ,
4265+ gitStatus : gitStatusQuery . data ?? null ,
4266+ } ) ;
43614267 const proactivePullRequestsKey = pullRequestsSurfaceAvailable
43624268 ? JSON . stringify (
43634269 visiblePullRequests . map ( ( link ) => [ link . host , link . repository , link . number ] ) . sort ( ) ,
0 commit comments