The best way to control Claude Code, Codex and more from any computer or phone
refactor(web): ChatView's pull request checkout lives in usePullRequestDraftThread (#23070)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Oct 21, 2026 at 13:40 UTC
114f77fd056346babc1fa9571d947147bed3f72b
2 files changed
+151
-92
apps/web/src/components/ChatView.tsx
+11
-92
@@ -1,3 +1,4 @@
1
+import { usePullRequestDraftThread } from "./chat/usePullRequestDraftThread";
2
import { useComposerSelection } from "./chat/useComposerSelection";
3
import { useProjectCloneBanner } from "./chat/useProjectCloneBanner";
4
import { useAttachmentPreviewHandoff } from "./chat/useAttachmentPreviewHandoff";
@@ -183,7 +184,7 @@ import { cn } from "~/lib/utils";
184
import { COLLAPSED_SIDEBAR_TITLEBAR_INSET_CLASS } from "~/workspaceTitlebar";
185
import { stackedThreadToast, toastManager } from "./ui/toast";
186
import { projectScriptIdFromCommand } from "~/projectScripts";
186
-import { newDraftId, newMessageId, newThreadId } from "~/lib/utils";
187
+import { newMessageId } from "~/lib/utils";
188
import { registerFaviconProjectForThread } from "~/browserFaviconStore";
189
import {
190
applyProviderInstanceSettings,
@@ -207,7 +208,6 @@ import {
208
deriveLogicalProjectKeyFromSettings,
209
selectProjectGroupingSettings,
210
} from "../logicalProject";
210
-import { buildDraftThreadRouteParams } from "../threadRoutes";
211
import {
212
isSameSidebarThreadRef,
213
useSidebarPendingFileDropStore,
@@ -259,7 +259,6 @@ import {
259
BranchRestoreConfirmHost,
260
DeviceSetupDialogHost,
261
openBranchRestoreConfirm,
262
- openPullRequestThreadDialog,
262
PullRequestThreadDialogHost,
263
RevertConfirmHost,
264
} from "./chat/ChatOverlayHosts";
@@ -635,13 +634,6 @@ export default function ChatView(props: ChatViewProps) {
634
});
635
const setComposerDraftPrompt = useComposerDraftStore((store) => store.setPrompt);
636
const setDraftThreadContext = useComposerDraftStore((store) => store.setDraftThreadContext);
638
- const getDraftSessionByLogicalProjectKey = useComposerDraftStore(
639
- (store) => store.getDraftSessionByLogicalProjectKey,
640
- );
641
- const getDraftSession = useComposerDraftStore((store) => store.getDraftSession);
642
- const setLogicalProjectDraftThreadId = useComposerDraftStore(
643
- (store) => store.setLogicalProjectDraftThreadId,
644
- );
637
const composerImagesRef = useRef<ComposerImageAttachment[]>([]);
638
const composerFilesRef = useRef<ComposerFileAttachment[]>([]);
639
const composerTerminalContextsRef = useRef<TerminalContextDraft[]>([]);
@@ -1198,91 +1190,18 @@ export default function ChatView(props: ChatViewProps) {
1190
activeEnvironment: activeEnvironmentOption,
1191
canPickEnvironment: hasMultipleEnvironments,
1192
});
1201
- const openPullRequestDialog = (reference?: string) => {
1202
- if (!canCheckoutPullRequestIntoThread) {
1203
- return;
1204
- }
1205
- if (activeThreadKey === null) return;
1206
- openPullRequestThreadDialog(activeThreadKey, reference);
1207
- };
1208
- const openPullRequestDialogStable = useStableCallback(openPullRequestDialog);
1193
1210
- const openOrReuseProjectDraftThread = async (input: {
1211
- branch: string;
1212
- worktreePath: string | null;
1213
- envMode: DraftThreadEnvMode;
1214
- }) => {
1215
- if (!activeProject) {
1216
- throw new Error("No active project is available for this pull request.");
1217
- }
1218
- const activeProjectRef = scopeProjectRef(activeProject.environmentId, activeProject.id);
1219
- const logicalProjectKey = deriveLogicalProjectKeyFromSettings(
1220
- activeProject,
1194
+ const { handlePreparedPullRequestThreadStable, openPullRequestDialogStable } =
1195
+ usePullRequestDraftThread({
1196
+ routeKind,
1197
+ draftId,
1198
+ settings,
1199
projectGroupingSettings,
1222
- );
1223
- const storedDraftSession = getDraftSessionByLogicalProjectKey(logicalProjectKey);
1224
- if (storedDraftSession) {
1225
- setDraftThreadContext(storedDraftSession.draftId, input);
1226
- setLogicalProjectDraftThreadId(
1227
- logicalProjectKey,
1228
- activeProjectRef,
1229
- storedDraftSession.draftId,
1230
- {
1231
- threadId: storedDraftSession.threadId,
1232
- ...input,
1233
- },
1234
- );
1235
- if (routeKind !== "draft" || draftId !== storedDraftSession.draftId) {
1236
- await navigate({
1237
- to: "/draft/$draftId",
1238
- params: buildDraftThreadRouteParams(storedDraftSession.draftId),
1239
- });
1240
- }
1241
- return storedDraftSession.threadId;
1242
- }
1243
-
1244
- const activeDraftSession = routeKind === "draft" && draftId ? getDraftSession(draftId) : null;
1245
- if (!isServerThread && activeDraftSession?.logicalProjectKey === logicalProjectKey && draftId) {
1246
- setDraftThreadContext(draftId, input);
1247
- setLogicalProjectDraftThreadId(logicalProjectKey, activeProjectRef, draftId, {
1248
- threadId: activeDraftSession.threadId,
1249
- createdAt: activeDraftSession.createdAt,
1250
- runtimeMode: activeDraftSession.runtimeMode,
1251
- interactionMode: activeDraftSession.interactionMode,
1252
- ...input,
1253
- });
1254
- return activeDraftSession.threadId;
1255
- }
1256
-
1257
- const nextDraftId = newDraftId();
1258
- const nextThreadId = newThreadId();
1259
- setLogicalProjectDraftThreadId(logicalProjectKey, activeProjectRef, nextDraftId, {
1260
- threadId: nextThreadId,
1261
- createdAt: new Date().toISOString(),
1262
- runtimeMode: resolveProjectSettings(settings, activeProject.id, activeProject).settings
1263
- .defaultRuntimeMode,
1264
- interactionMode: DEFAULT_INTERACTION_MODE,
1265
- ...input,
1266
- });
1267
- await navigate({
1268
- to: "/draft/$draftId",
1269
- params: buildDraftThreadRouteParams(nextDraftId),
1270
- });
1271
- return nextThreadId;
1272
- };
1273
-
1274
- const handlePreparedPullRequestThread = async (input: {
1275
- branch: string;
1276
- worktreePath: string | null;
1277
- }) => {
1278
- await openOrReuseProjectDraftThread({
1279
- branch: input.branch,
1280
- worktreePath: input.worktreePath,
1281
- envMode: input.worktreePath ? "worktree" : "local",
1200
+ activeProject,
1201
+ activeThreadKey,
1202
+ isServerThread,
1203
+ canCheckoutPullRequestIntoThread,
1204
});
1283
- };
1284
- const handlePreparedPullRequestThreadStable = useStableCallback(handlePreparedPullRequestThread);
1285
-
1205
const serverConfig = activeThread
1206
? (activeEnvironment?.serverConfig ?? null)
1207
: (primaryEnvironment?.serverConfig ?? null);
apps/web/src/components/chat/usePullRequestDraftThread.ts
new
+140
@@ -0,0 +1,140 @@
1
+import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell";
2
+import type { UnifiedSettings } from "@t3tools/contracts";
3
+import { useStableCallback } from "../../hooks/useStableCallback";
4
+import { scopeProjectRef } from "@t3tools/client-runtime/environment";
5
+import { resolveProjectSettings } from "@t3tools/shared/projectSettings";
6
+import { useNavigate } from "@tanstack/react-router";
7
+import { DEFAULT_INTERACTION_MODE } from "../../types";
8
+import { newDraftId, newThreadId } from "~/lib/utils";
9
+import {
10
+ deriveLogicalProjectKeyFromSettings,
11
+ selectProjectGroupingSettings,
12
+} from "../../logicalProject";
13
+import { buildDraftThreadRouteParams } from "../../threadRoutes";
14
+import { type DraftThreadEnvMode, useComposerDraftStore, DraftId } from "../../composerDraftStore";
15
+import { openPullRequestThreadDialog } from "./ChatOverlayHosts";
16
+interface PullRequestDraftThreadInput {
17
+ readonly routeKind: "server" | "draft";
18
+ readonly draftId: DraftId | null;
19
+ readonly settings: UnifiedSettings;
20
+ readonly projectGroupingSettings: ReturnType<typeof selectProjectGroupingSettings>;
21
+ readonly activeProject: EnvironmentProject | null;
22
+ readonly activeThreadKey: string | null;
23
+ readonly isServerThread: boolean;
24
+ readonly canCheckoutPullRequestIntoThread: boolean;
25
+}
26
+
27
+/**
28
+ * Checking out a pull request into a thread: opens the dialog, then puts the prepared branch or
29
+ * worktree on the project's draft thread, reusing the one already open when there is one.
30
+ */
31
+export function usePullRequestDraftThread(input: PullRequestDraftThreadInput) {
32
+ const {
33
+ routeKind,
34
+ draftId,
35
+ settings,
36
+ projectGroupingSettings,
37
+ activeProject,
38
+ activeThreadKey,
39
+ isServerThread,
40
+ canCheckoutPullRequestIntoThread,
41
+ } = input;
42
+ const navigate = useNavigate();
43
+ const setDraftThreadContext = useComposerDraftStore((store) => store.setDraftThreadContext);
44
+ const getDraftSessionByLogicalProjectKey = useComposerDraftStore(
45
+ (store) => store.getDraftSessionByLogicalProjectKey,
46
+ );
47
+ const getDraftSession = useComposerDraftStore((store) => store.getDraftSession);
48
+ const setLogicalProjectDraftThreadId = useComposerDraftStore(
49
+ (store) => store.setLogicalProjectDraftThreadId,
50
+ );
51
+
52
+ const openPullRequestDialog = (reference?: string) => {
53
+ if (!canCheckoutPullRequestIntoThread) {
54
+ return;
55
+ }
56
+ if (activeThreadKey === null) return;
57
+ openPullRequestThreadDialog(activeThreadKey, reference);
58
+ };
59
+ const openPullRequestDialogStable = useStableCallback(openPullRequestDialog);
60
+
61
+ const openOrReuseProjectDraftThread = async (input: {
62
+ branch: string;
63
+ worktreePath: string | null;
64
+ envMode: DraftThreadEnvMode;
65
+ }) => {
66
+ if (!activeProject) {
67
+ throw new Error("No active project is available for this pull request.");
68
+ }
69
+ const activeProjectRef = scopeProjectRef(activeProject.environmentId, activeProject.id);
70
+ const logicalProjectKey = deriveLogicalProjectKeyFromSettings(
71
+ activeProject,
72
+ projectGroupingSettings,
73
+ );
74
+ const storedDraftSession = getDraftSessionByLogicalProjectKey(logicalProjectKey);
75
+ if (storedDraftSession) {
76
+ setDraftThreadContext(storedDraftSession.draftId, input);
77
+ setLogicalProjectDraftThreadId(
78
+ logicalProjectKey,
79
+ activeProjectRef,
80
+ storedDraftSession.draftId,
81
+ {
82
+ threadId: storedDraftSession.threadId,
83
+ ...input,
84
+ },
85
+ );
86
+ if (routeKind !== "draft" || draftId !== storedDraftSession.draftId) {
87
+ await navigate({
88
+ to: "/draft/$draftId",
89
+ params: buildDraftThreadRouteParams(storedDraftSession.draftId),
90
+ });
91
+ }
92
+ return storedDraftSession.threadId;
93
+ }
94
+
95
+ const activeDraftSession = routeKind === "draft" && draftId ? getDraftSession(draftId) : null;
96
+ if (!isServerThread && activeDraftSession?.logicalProjectKey === logicalProjectKey && draftId) {
97
+ setDraftThreadContext(draftId, input);
98
+ setLogicalProjectDraftThreadId(logicalProjectKey, activeProjectRef, draftId, {
99
+ threadId: activeDraftSession.threadId,
100
+ createdAt: activeDraftSession.createdAt,
101
+ runtimeMode: activeDraftSession.runtimeMode,
102
+ interactionMode: activeDraftSession.interactionMode,
103
+ ...input,
104
+ });
105
+ return activeDraftSession.threadId;
106
+ }
107
+
108
+ const nextDraftId = newDraftId();
109
+ const nextThreadId = newThreadId();
110
+ setLogicalProjectDraftThreadId(logicalProjectKey, activeProjectRef, nextDraftId, {
111
+ threadId: nextThreadId,
112
+ createdAt: new Date().toISOString(),
113
+ runtimeMode: resolveProjectSettings(settings, activeProject.id, activeProject).settings
114
+ .defaultRuntimeMode,
115
+ interactionMode: DEFAULT_INTERACTION_MODE,
116
+ ...input,
117
+ });
118
+ await navigate({
119
+ to: "/draft/$draftId",
120
+ params: buildDraftThreadRouteParams(nextDraftId),
121
+ });
122
+ return nextThreadId;
123
+ };
124
+
125
+ const handlePreparedPullRequestThread = async (input: {
126
+ branch: string;
127
+ worktreePath: string | null;
128
+ }) => {
129
+ await openOrReuseProjectDraftThread({
130
+ branch: input.branch,
131
+ worktreePath: input.worktreePath,
132
+ envMode: input.worktreePath ? "worktree" : "local",
133
+ });
134
+ };
135
+ const handlePreparedPullRequestThreadStable = useStableCallback(handlePreparedPullRequestThread);
136
+ return {
137
+ handlePreparedPullRequestThreadStable,
138
+ openPullRequestDialogStable,
139
+ };
140
+}