Skip to content

Commit e66da23

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 a917c0a commit e66da23

3 files changed

Lines changed: 128 additions & 112 deletions

File tree

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

Lines changed: 16 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";
@@ -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(),
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
@@ -359,6 +359,7 @@ export default defineConfig({
359359
"apps/web/src/components/chat/usePendingThreadRequests.ts",
360360
"apps/web/src/components/chat/useQueuedRunEdit.ts",
361361
"apps/web/src/components/chat/useUsageLimitsPanel.ts",
362+
"apps/web/src/components/pullRequest/useThreadLinkedPullRequest.ts",
362363
"apps/web/src/components/terminal/**",
363364
],
364365
rules: {

0 commit comments

Comments
 (0)