The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 23, 06:45 UTC
perf(web): the thread action menu hook compiles without manual memoization (#20701)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Oct 16, 2026 at 21:48 UTC
c0cdb02a4b8f96df7e077a8e7dadc6da833f3568
1 file changed
+197
-232
apps/web/src/hooks/useThreadActionMenu.ts
+197
-232
@@ -13,7 +13,6 @@ import {
13
type ThreadId,
14
} from "@t3tools/contracts";
15
import { useRouter } from "@tanstack/react-router";
16
-import { useCallback, useMemo } from "react";
16
17
import { resolveSnoozePresets } from "../components/Sidebar.snooze";
18
import {
@@ -68,15 +67,11 @@ export function useThreadActionMenu(input: {
67
const projects = useProjects();
68
const primaryEnvironmentId = usePrimaryEnvironmentId();
69
const projectGroupingSettings = useClientSettings(selectProjectGroupingSettings);
71
- const logicalProjectKeyByPhysicalKey = useMemo(
72
- () =>
73
- buildPhysicalToLogicalProjectKeyMap({
74
- projects,
75
- settings: projectGroupingSettings,
76
- primaryEnvironmentId,
77
- }),
78
- [primaryEnvironmentId, projectGroupingSettings, projects],
79
- );
70
+ const logicalProjectKeyByPhysicalKey = buildPhysicalToLogicalProjectKeyMap({
71
+ projects,
72
+ settings: projectGroupingSettings,
73
+ primaryEnvironmentId,
74
+ });
75
const {
76
settleThread,
77
unsettleThread,
@@ -116,247 +111,217 @@ export function useThreadActionMenu(input: {
111
onError: (error) => failureToast("Failed to copy thread ID", error),
112
});
113
119
- const openMenu = useCallback(
120
- (position: { x: number; y: number }) => {
121
- if (threadRef === null) return;
122
- void (async () => {
123
- const api = readLocalApi();
124
- if (!api) return;
125
- const thread = readThreadShell(threadRef);
126
- if (!thread) return;
127
- const now = new Date();
128
- const supports = {
129
- settlement: readEnvironmentSupportsSettlement(threadRef.environmentId),
130
- autoSettleOptOut: readEnvironmentSupportsAutoSettleOptOut(threadRef.environmentId),
131
- snooze: readEnvironmentSupportsSnooze(threadRef.environmentId),
132
- pinning: readEnvironmentSupportsPinning(threadRef.environmentId),
133
- titleRegeneration: readEnvironmentSupportsTitleRegeneration(threadRef.environmentId),
134
- };
135
- const isRegeneratingTitle = thread.titleRegeneration != null;
136
- const snoozePresets = resolveSnoozePresets(now, timestampFormat);
137
- const items = buildThreadActionMenuItems({
138
- canOperate: readEnvironmentScope(threadRef.environmentId, AuthOrchestrationOperateScope),
139
- branch: thread.branch ?? null,
140
- projectFilter: null,
141
- isPinned: thread.pinnedAt != null,
142
- isSettled: supports.settlement && thread.settledOverride === "settled",
143
- autoSettleEnabled: thread.autoSettleDisabledAt == null,
144
- isSnoozed: supports.snooze && effectiveSnoozed(thread, { now: now.toISOString() }),
145
- canSnoozeNow: canSnooze(thread, { now: now.toISOString() }),
146
- isRegeneratingTitle,
147
- isRunning: !threadRuntimeCanArchive(thread.runtime),
148
- supports,
149
- snoozePresets,
150
- });
151
- const clicked = await settlePromise(() => api.contextMenu.show(items, position));
152
- if (clicked._tag === "Failure" || clicked.value === null) return;
153
- const action: ThreadActionMenuId = clicked.value;
154
- if (
155
- threadActionRequiresOperate(action) &&
156
- !readEnvironmentScope(threadRef.environmentId, AuthOrchestrationOperateScope)
157
- ) {
158
- failureToast(
159
- "Thread action unavailable",
160
- new Error("This connection cannot change threads."),
114
+ const openMenu = (position: { x: number; y: number }) => {
115
+ if (threadRef === null) return;
116
+ void (async () => {
117
+ const api = readLocalApi();
118
+ if (!api) return;
119
+ const thread = readThreadShell(threadRef);
120
+ if (!thread) return;
121
+ const now = new Date();
122
+ const supports = {
123
+ settlement: readEnvironmentSupportsSettlement(threadRef.environmentId),
124
+ autoSettleOptOut: readEnvironmentSupportsAutoSettleOptOut(threadRef.environmentId),
125
+ snooze: readEnvironmentSupportsSnooze(threadRef.environmentId),
126
+ pinning: readEnvironmentSupportsPinning(threadRef.environmentId),
127
+ titleRegeneration: readEnvironmentSupportsTitleRegeneration(threadRef.environmentId),
128
+ };
129
+ const isRegeneratingTitle = thread.titleRegeneration != null;
130
+ const snoozePresets = resolveSnoozePresets(now, timestampFormat);
131
+ const items = buildThreadActionMenuItems({
132
+ canOperate: readEnvironmentScope(threadRef.environmentId, AuthOrchestrationOperateScope),
133
+ branch: thread.branch ?? null,
134
+ projectFilter: null,
135
+ isPinned: thread.pinnedAt != null,
136
+ isSettled: supports.settlement && thread.settledOverride === "settled",
137
+ autoSettleEnabled: thread.autoSettleDisabledAt == null,
138
+ isSnoozed: supports.snooze && effectiveSnoozed(thread, { now: now.toISOString() }),
139
+ canSnoozeNow: canSnooze(thread, { now: now.toISOString() }),
140
+ isRegeneratingTitle,
141
+ isRunning: !threadRuntimeCanArchive(thread.runtime),
142
+ supports,
143
+ snoozePresets,
144
+ });
145
+ const clicked = await settlePromise(() => api.contextMenu.show(items, position));
146
+ if (clicked._tag === "Failure" || clicked.value === null) return;
147
+ const action: ThreadActionMenuId = clicked.value;
148
+ if (
149
+ threadActionRequiresOperate(action) &&
150
+ !readEnvironmentScope(threadRef.environmentId, AuthOrchestrationOperateScope)
151
+ ) {
152
+ failureToast(
153
+ "Thread action unavailable",
154
+ new Error("This connection cannot change threads."),
155
+ );
156
+ return;
157
+ }
158
+ if (action.startsWith("snooze:")) {
159
+ const preset =
160
+ action === "snooze:custom"
161
+ ? await requestCustomSnooze()
162
+ : snoozePresets.find((candidate) => `snooze:${candidate.id}` === action);
163
+ if (!preset) return;
164
+ const result = await snoozeThread(threadRef, preset.snoozedUntil);
165
+ if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
166
+ failureToast("Failed to snooze thread", squashAtomCommandFailure(result));
167
+ }
168
+ return;
169
+ }
170
+ const reportFailure = async (
171
+ title: string,
172
+ run: () => Promise<AtomCommandResult<unknown, unknown>>,
173
+ ) => {
174
+ const result = await run();
175
+ if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
176
+ failureToast(title, squashAtomCommandFailure(result));
177
+ }
178
+ };
179
+ switch (action) {
180
+ case "project-settings": {
181
+ const project = projects.find(
182
+ (candidate) =>
183
+ candidate.environmentId === thread.environmentId && candidate.id === thread.projectId,
184
);
185
+ if (!project) return;
186
+ const projectKey =
187
+ logicalProjectKeyByPhysicalKey.get(derivePhysicalProjectKey(project)) ??
188
+ deriveLogicalProjectKeyFromSettings(project, projectGroupingSettings);
189
+ void router.navigate({
190
+ to: "/projects/$projectKey",
191
+ params: { projectKey },
192
+ });
193
return;
194
}
164
- if (action.startsWith("snooze:")) {
165
- const preset =
166
- action === "snooze:custom"
167
- ? await requestCustomSnooze()
168
- : snoozePresets.find((candidate) => `snooze:${candidate.id}` === action);
169
- if (!preset) return;
170
- const result = await snoozeThread(threadRef, preset.snoozedUntil);
171
- if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
172
- failureToast("Failed to snooze thread", squashAtomCommandFailure(result));
195
+ case "new-thread-on-branch": {
196
+ const result = await settlePromise(() =>
197
+ handleNewThread(scopeProjectRef(threadRef.environmentId, thread.projectId), {
198
+ branch: thread.branch,
199
+ worktreePath: thread.worktreePath,
200
+ envMode: thread.worktreePath ? "worktree" : "local",
201
+ startFromOrigin: false,
202
+ }),
203
+ );
204
+ if (result._tag === "Failure") {
205
+ failureToast("Could not create thread", squashAtomCommandFailure(result));
206
}
207
return;
208
}
176
- const reportFailure = async (
177
- title: string,
178
- run: () => Promise<AtomCommandResult<unknown, unknown>>,
179
- ) => {
180
- const result = await run();
181
- if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
182
- failureToast(title, squashAtomCommandFailure(result));
183
- }
184
- };
185
- switch (action) {
186
- case "project-settings": {
187
- const project = projects.find(
188
- (candidate) =>
189
- candidate.environmentId === thread.environmentId &&
190
- candidate.id === thread.projectId,
191
- );
192
- if (!project) return;
193
- const projectKey =
194
- logicalProjectKeyByPhysicalKey.get(derivePhysicalProjectKey(project)) ??
195
- deriveLogicalProjectKeyFromSettings(project, projectGroupingSettings);
196
- void router.navigate({
197
- to: "/projects/$projectKey",
198
- params: { projectKey },
199
- });
200
- return;
201
- }
202
- case "new-thread-on-branch": {
203
- const result = await settlePromise(() =>
204
- handleNewThread(scopeProjectRef(threadRef.environmentId, thread.projectId), {
205
- branch: thread.branch,
206
- worktreePath: thread.worktreePath,
207
- envMode: thread.worktreePath ? "worktree" : "local",
208
- startFromOrigin: false,
209
+ case "settle":
210
+ await reportFailure("Failed to settle thread", () => settleThread(threadRef));
211
+ return;
212
+ case "unsettle":
213
+ await reportFailure("Failed to un-settle thread", () => unsettleThread(threadRef));
214
+ return;
215
+ case "unsnooze":
216
+ await reportFailure("Failed to wake thread", () => unsnoozeThread(threadRef));
217
+ return;
218
+ case "pin":
219
+ await reportFailure("Failed to pin thread", () => pinThread(threadRef));
220
+ return;
221
+ case "unpin": {
222
+ await reportFailure("Failed to unpin thread", () => confirmAndUnpinThread(threadRef));
223
+ return;
224
+ }
225
+ case "auto-settle:enabled":
226
+ case "auto-settle:disabled":
227
+ await reportFailure("Failed to update auto-settle", () =>
228
+ setThreadAutoSettle(threadRef, action === "auto-settle:enabled"),
229
+ );
230
+ return;
231
+ case "rename":
232
+ onStartRename();
233
+ return;
234
+ case "regenerate-title":
235
+ if (isRegeneratingTitle) return;
236
+ await reportFailure("Failed to regenerate thread title", () =>
237
+ updateThreadMetadata({
238
+ environmentId: threadRef.environmentId,
239
+ input: { threadId: threadRef.threadId, regenerateTitle: true },
240
+ }),
241
+ );
242
+ return;
243
+ case "mark-unread":
244
+ markThreadUnread(threadRef);
245
+ return;
246
+ case "copy-path": {
247
+ const workspacePath = thread.worktreePath ?? projectCwd;
248
+ if (!workspacePath) {
249
+ toastManager.add(
250
+ stackedThreadToast({
251
+ type: "error",
252
+ title: "Path unavailable",
253
+ description: "This thread does not have a workspace path to copy.",
254
}),
255
);
211
- if (result._tag === "Failure") {
212
- failureToast("Could not create thread", squashAtomCommandFailure(result));
213
- }
256
return;
257
}
216
- case "settle":
217
- await reportFailure("Failed to settle thread", () => settleThread(threadRef));
218
- return;
219
- case "unsettle":
220
- await reportFailure("Failed to un-settle thread", () => unsettleThread(threadRef));
221
- return;
222
- case "unsnooze":
223
- await reportFailure("Failed to wake thread", () => unsnoozeThread(threadRef));
224
- return;
225
- case "pin":
226
- await reportFailure("Failed to pin thread", () => pinThread(threadRef));
227
- return;
228
- case "unpin": {
229
- await reportFailure("Failed to unpin thread", () => confirmAndUnpinThread(threadRef));
230
- return;
258
+ copyPathToClipboard(workspacePath, { path: workspacePath });
259
+ return;
260
+ }
261
+ case "copy-branch":
262
+ if (thread.branch) {
263
+ copyBranchToClipboard(thread.branch, { branch: thread.branch });
264
}
232
- case "auto-settle:enabled":
233
- case "auto-settle:disabled":
234
- await reportFailure("Failed to update auto-settle", () =>
235
- setThreadAutoSettle(threadRef, action === "auto-settle:enabled"),
265
+ return;
266
+ case "copy-thread-id":
267
+ copyThreadIdToClipboard(thread.id, { threadId: thread.id });
268
+ return;
269
+ case "archive": {
270
+ if (confirmThreadArchive) {
271
+ const confirmed = await settlePromise(() =>
272
+ api.dialogs.confirm(`Archive thread "${thread.title}"?`),
273
);
237
- return;
238
- case "rename":
239
- onStartRename();
240
- return;
241
- case "regenerate-title":
242
- if (isRegeneratingTitle) return;
243
- await reportFailure("Failed to regenerate thread title", () =>
244
- updateThreadMetadata({
245
- environmentId: threadRef.environmentId,
246
- input: { threadId: threadRef.threadId, regenerateTitle: true },
247
- }),
274
+ if (confirmed._tag === "Failure" || !confirmed.value) return;
275
+ }
276
+ let didArchive = false;
277
+ const result = await archiveThread(threadRef, {
278
+ onArchived: () => {
279
+ didArchive = true;
280
+ },
281
+ });
282
+ if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
283
+ failureToast(
284
+ didArchive ? "Thread archived, but navigation failed" : "Failed to archive thread",
285
+ squashAtomCommandFailure(result),
286
);
249
- return;
250
- case "mark-unread":
251
- markThreadUnread(threadRef);
252
- return;
253
- case "copy-path": {
254
- const workspacePath = thread.worktreePath ?? projectCwd;
255
- if (!workspacePath) {
256
- toastManager.add(
257
- stackedThreadToast({
258
- type: "error",
259
- title: "Path unavailable",
260
- description: "This thread does not have a workspace path to copy.",
261
- }),
262
- );
263
- return;
264
- }
265
- copyPathToClipboard(workspacePath, { path: workspacePath });
266
- return;
287
}
268
- case "copy-branch":
269
- if (thread.branch) {
270
- copyBranchToClipboard(thread.branch, { branch: thread.branch });
271
- }
272
- return;
273
- case "copy-thread-id":
274
- copyThreadIdToClipboard(thread.id, { threadId: thread.id });
275
- return;
276
- case "archive": {
277
- if (confirmThreadArchive) {
278
- const confirmed = await settlePromise(() =>
279
- api.dialogs.confirm(`Archive thread "${thread.title}"?`),
280
- );
281
- if (confirmed._tag === "Failure" || !confirmed.value) return;
282
- }
283
- let didArchive = false;
284
- const result = await archiveThread(threadRef, {
285
- onArchived: () => {
286
- didArchive = true;
287
- },
288
- });
289
- if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
290
- failureToast(
291
- didArchive ? "Thread archived, but navigation failed" : "Failed to archive thread",
292
- squashAtomCommandFailure(result),
293
- );
294
- }
295
- return;
288
+ return;
289
+ }
290
+ case "delete": {
291
+ if (confirmThreadDelete) {
292
+ const confirmed = await settlePromise(() =>
293
+ api.dialogs.confirm(
294
+ [
295
+ `Delete thread "${thread.title}"?`,
296
+ "This permanently clears conversation history for this thread.",
297
+ ].join("\n"),
298
+ { variant: "destructive" },
299
+ ),
300
+ );
301
+ if (confirmed._tag === "Failure" || !confirmed.value) return;
302
}
297
- case "delete": {
298
- if (confirmThreadDelete) {
299
- const confirmed = await settlePromise(() =>
300
- api.dialogs.confirm(
301
- [
302
- `Delete thread "${thread.title}"?`,
303
- "This permanently clears conversation history for this thread.",
304
- ].join("\n"),
305
- { variant: "destructive" },
306
- ),
307
- );
308
- if (confirmed._tag === "Failure" || !confirmed.value) return;
309
- }
310
- const deleted = await deleteThread(threadRef);
311
- if (
312
- deleted._tag === "Failure" &&
313
- !isAtomCommandInterrupted(deleted) &&
314
- // A failure with the thread already gone is worktree cleanup
315
- // failing after a successful delete — deleteThread has toasted
316
- // that itself, and "Failed to delete thread" would be a lie.
317
- readThreadShell(threadRef) !== null
318
- ) {
319
- failureToast("Failed to delete thread", squashAtomCommandFailure(deleted));
320
- }
321
- return;
303
+ const deleted = await deleteThread(threadRef);
304
+ if (
305
+ deleted._tag === "Failure" &&
306
+ !isAtomCommandInterrupted(deleted) &&
307
+ // A failure with the thread already gone is worktree cleanup
308
+ // failing after a successful delete — deleteThread has toasted
309
+ // that itself, and "Failed to delete thread" would be a lie.
310
+ readThreadShell(threadRef) !== null
311
+ ) {
312
+ failureToast("Failed to delete thread", squashAtomCommandFailure(deleted));
313
}
323
- default:
324
- return;
314
+ return;
315
}
326
- })();
327
- },
328
- [
329
- archiveThread,
330
- confirmThreadArchive,
331
- confirmThreadDelete,
332
- confirmAndUnpinThread,
333
- copyBranchToClipboard,
334
- copyPathToClipboard,
335
- copyThreadIdToClipboard,
336
- deleteThread,
337
- handleNewThread,
338
- logicalProjectKeyByPhysicalKey,
339
- markThreadUnread,
340
- onStartRename,
341
- pinThread,
342
- projectCwd,
343
- projectGroupingSettings,
344
- projects,
345
- router,
346
- setThreadAutoSettle,
347
- settleThread,
348
- snoozeThread,
349
- threadRef,
350
- timestampFormat,
351
- unsettleThread,
352
- unsnoozeThread,
353
- updateThreadMetadata,
354
- ],
355
- );
316
+ default:
317
+ return;
318
+ }
319
+ })();
320
+ };
321
357
- const closeMenu = useCallback(() => {
322
+ const closeMenu = () => {
323
void readLocalApi()?.contextMenu.close();
359
- }, []);
324
+ };
325
326
return { openMenu, closeMenu };
327
}