Skip to content

Commit 398f288

Browse files
refactor(web): move the thread's linked pull request out of ChatView
useThreadLinkedPullRequest resolves which change request a thread is linked to, including replacing a merged or closed one with the branch's newer open one, and owns the effect that relinks it on the server and retargets an open panel. It compiles and joins the compile-required list. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent d11a8b8 commit 398f288

3 files changed

Lines changed: 130 additions & 112 deletions

File tree

‎apps/web/src/components/ChatView.tsx‎

Lines changed: 18 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ import {
462462
ThreadErrorBanner,
463463
} from "./chat/ThreadErrorBanner";
464464
import { QueuedRunsControl, type QueuedRunsControlHandle } from "./chat/QueuedRunsControl";
465-
import { useLinkedThreadPullRequest } from "./ThreadStatusIndicators";
465+
import { useThreadLinkedPullRequest } from "./pullRequest/useThreadLinkedPullRequest";
466466
import type { ComposerBannerStackItem } from "./chat/ComposerBannerStack";
467467
import { ThreadStatusLine } from "./chat/ThreadStatusLine";
468468
import { 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(),
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import {
2+
type EnvironmentId,
3+
type ProjectId,
4+
type ScopedThreadRef,
5+
type ThreadLinkedPullRequest,
6+
type VcsStatusResult,
7+
} from "@t3tools/contracts";
8+
import { scopedThreadKey } from "@t3tools/client-runtime/environment";
9+
import {
10+
isAtomCommandInterrupted,
11+
squashAtomCommandFailure,
12+
} from "@t3tools/client-runtime/state/runtime";
13+
import { useEffect, useRef } from "react";
14+
import { selectActiveRightPanelSurface, useRightPanelStore } from "../../rightPanelStore";
15+
import { threadEnvironment } from "../../state/threads";
16+
import { useOrchestrationCommand } from "../../state/use-orchestration-command";
17+
import { useLinkedThreadPullRequest } from "../ThreadStatusIndicators";
18+
import { stackedThreadToast, toastManager } from "../ui/toast";
19+
20+
/**
21+
* The change request a thread is linked to. When the linked one is merged or
22+
* closed and the thread's branch has a newer open one, that one replaces it:
23+
* the thread is relinked on the server, and an open panel showing the old one
24+
* follows.
25+
*/
26+
export function useThreadLinkedPullRequest(input: {
27+
// Null for drafts, which have nothing to relink on the server.
28+
readonly serverThreadRef: ScopedThreadRef | null;
29+
readonly environmentId: EnvironmentId | null;
30+
readonly persisted: ThreadLinkedPullRequest | null;
31+
readonly projectId: ProjectId | null;
32+
readonly projectRepository: string | null;
33+
readonly threadBranch: string | null;
34+
readonly gitStatus: VcsStatusResult | null;
35+
}) {
36+
const { serverThreadRef, persisted, projectId, projectRepository, threadBranch, gitStatus } =
37+
input;
38+
const updateThreadMetadata = useOrchestrationCommand(threadEnvironment.updateMetadata, {
39+
reportFailure: false,
40+
});
41+
const persistedStatus = useLinkedThreadPullRequest(input.environmentId, persisted);
42+
const detected = gitStatus?.pr;
43+
const replacement: ThreadLinkedPullRequest | null =
44+
persisted === null ||
45+
(persistedStatus?.pr.state !== "merged" && persistedStatus?.pr.state !== "closed") ||
46+
gitStatus?.refName !== threadBranch ||
47+
detected?.state !== "open" ||
48+
detected.headRef !== threadBranch ||
49+
projectId === null ||
50+
projectRepository === null ||
51+
(persisted.projectId === projectId &&
52+
persisted.repository.toLowerCase() === projectRepository.toLowerCase() &&
53+
persisted.number === detected.number)
54+
? null
55+
: { projectId, repository: projectRepository, number: detected.number, url: detected.url };
56+
57+
const relinkKeysRef = useRef(new Map<string, string>());
58+
const relinkWriteRef = useRef(Promise.resolve());
59+
useEffect(() => {
60+
if (serverThreadRef === null) return;
61+
const threadKey = scopedThreadKey(serverThreadRef);
62+
if (replacement === null) {
63+
relinkKeysRef.current.delete(threadKey);
64+
return;
65+
}
66+
const relinkKey = `${replacement.projectId}:${replacement.repository}#${replacement.number}`;
67+
if (relinkKeysRef.current.get(threadKey) === relinkKey) return;
68+
relinkKeysRef.current.set(threadKey, relinkKey);
69+
const openSurface = selectActiveRightPanelSurface(
70+
useRightPanelStore.getState().byThreadKey,
71+
serverThreadRef,
72+
);
73+
if (
74+
openSurface?.kind === "pull-request" &&
75+
persisted !== null &&
76+
openSurface.projectId === persisted.projectId &&
77+
openSurface.repository.toLowerCase() === persisted.repository.toLowerCase() &&
78+
openSurface.number === persisted.number
79+
) {
80+
useRightPanelStore.getState().openPullRequest(serverThreadRef, replacement);
81+
}
82+
83+
relinkWriteRef.current = relinkWriteRef.current.then(async () => {
84+
if (relinkKeysRef.current.get(threadKey) !== relinkKey) return;
85+
const result = await updateThreadMetadata({
86+
environmentId: serverThreadRef.environmentId,
87+
input: { threadId: serverThreadRef.threadId, linkedPullRequest: replacement },
88+
});
89+
if (relinkKeysRef.current.get(threadKey) !== relinkKey) return;
90+
if (result._tag !== "Failure") return;
91+
relinkKeysRef.current.delete(threadKey);
92+
if (isAtomCommandInterrupted(result)) return;
93+
const error = squashAtomCommandFailure(result);
94+
toastManager.add(
95+
stackedThreadToast({
96+
type: "error",
97+
title: "Unable to update the thread pull request",
98+
description: error instanceof Error ? error.message : "An error occurred.",
99+
}),
100+
);
101+
});
102+
}, [persisted, replacement, serverThreadRef, updateThreadMetadata]);
103+
104+
const linked = replacement ?? persisted;
105+
return {
106+
linked,
107+
linkedKey: linked ? JSON.stringify([linked.projectId, linked.repository, linked.number]) : null,
108+
// Detail is shown for the persisted link; a replacement only exists once it is merged.
109+
hasDetail: persisted !== null,
110+
};
111+
}

‎vite.config.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ export default defineConfig({
330330
"apps/web/src/components/chat/usePendingThreadRequests.ts",
331331
"apps/web/src/components/chat/useQueuedRunEdit.ts",
332332
"apps/web/src/components/chat/useUsageLimitsPanel.ts",
333+
"apps/web/src/components/pullRequest/useThreadLinkedPullRequest.ts",
333334
"apps/web/src/components/terminal/**",
334335
],
335336
rules: {

0 commit comments

Comments
 (0)