The best way to control Claude Code, Codex and more from any computer or phone
perf(web): thread actions compile without manual memoization (#20470)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Oct 16, 2026 at 18:46 UTC
7f4142e5b07bd1027d513c88311347101b294b52
2 files changed
+580
-680
apps/web/reactCompiledFiles.ts
+1
@@ -45,4 +45,5 @@ export const REACT_COMPILED_FILES = [
45
"apps/web/src/components/BranchToolbar.tsx",
46
"apps/web/src/components/DiffPanel.tsx",
47
"apps/web/src/components/onboarding/WelcomeWizard.tsx",
48
+ "apps/web/src/hooks/useThreadActions.ts",
49
] as const;
apps/web/src/hooks/useThreadActions.ts
+579
-680
@@ -21,12 +21,10 @@ import * as Cause from "effect/Cause";
21
import * as Schema from "effect/Schema";
22
import { AsyncResult } from "effect/reactivity";
23
import { useRouter } from "@tanstack/react-router";
24
-import { useCallback, useMemo, useRef } from "react";
24
25
import { getFallbackThreadIdAfterDelete, pinOrderKeyBetween } from "../components/Sidebar.logic";
26
import { useComposerDraftStore } from "../composerDraftStore";
27
import { environmentSession, readEnvironmentScope } from "../state/session";
29
-import { terminalEnvironment } from "../state/terminal";
28
import { appAtomRegistry } from "../rpc/atomRegistry";
29
import { environmentServerConfigsAtom } from "../state/server";
30
import { isScratchProject } from "@t3tools/client-runtime/state/projects";
@@ -206,38 +204,32 @@ function useMarkThreadUnread() {
204
reportFailure: false,
205
});
206
const markThreadUnreadLocal = useUiStateStore((state) => state.markThreadUnread);
209
- return useCallback(
210
- (target: ScopedThreadRef) => {
211
- if (readEnvironmentSupportsVisitedTracking(target.environmentId)) {
212
- void markThreadUnreadMutation({
213
- environmentId: target.environmentId,
214
- input: { threadId: target.threadId },
215
- });
216
- return;
217
- }
218
- const thread = readThreadShell(target);
219
- markThreadUnreadLocal(scopedThreadKey(target), thread?.latestRun?.completedAt);
220
- },
221
- [markThreadUnreadLocal, markThreadUnreadMutation],
222
- );
207
+ return (target: ScopedThreadRef) => {
208
+ if (readEnvironmentSupportsVisitedTracking(target.environmentId)) {
209
+ void markThreadUnreadMutation({
210
+ environmentId: target.environmentId,
211
+ input: { threadId: target.threadId },
212
+ });
213
+ return;
214
+ }
215
+ const thread = readThreadShell(target);
216
+ markThreadUnreadLocal(scopedThreadKey(target), thread?.latestRun?.completedAt);
217
+ };
218
}
219
220
export function useAcknowledgeThreadWoke() {
221
const visitThreadMutation = useAtomCommand(threadEnvironment.visit, { reportFailure: false });
222
const markThreadVisited = useUiStateStore((state) => state.markThreadVisited);
228
- return useCallback(
229
- (target: ScopedThreadRef, wokeAt: string) => {
230
- if (readEnvironmentSupportsVisitedTracking(target.environmentId)) {
231
- void visitThreadMutation({
232
- environmentId: target.environmentId,
233
- input: { threadId: target.threadId, visitedAt: wokeAt },
234
- });
235
- return;
236
- }
237
- markThreadVisited(scopedThreadKey(target), wokeAt);
238
- },
239
- [markThreadVisited, visitThreadMutation],
240
- );
223
+ return (target: ScopedThreadRef, wokeAt: string) => {
224
+ if (readEnvironmentSupportsVisitedTracking(target.environmentId)) {
225
+ void visitThreadMutation({
226
+ environmentId: target.environmentId,
227
+ input: { threadId: target.threadId, visitedAt: wokeAt },
228
+ });
229
+ return;
230
+ }
231
+ markThreadVisited(scopedThreadKey(target), wokeAt);
232
+ };
233
}
234
235
function threadOperationFailure(target: ScopedThreadRef) {
@@ -312,14 +304,8 @@ export function useThreadActions() {
304
const markThreadVisited = useUiStateStore((state) => state.markThreadVisited);
305
const router = useRouter();
306
const handleNewThread = useNewThreadHandler();
315
- // Keep a ref so archiveThread can call handleNewThread without appearing in
316
- // its dependency array — handleNewThread is inherently unstable (depends on
317
- // the projects list) and would otherwise cascade new references into every
318
- // sidebar row via archiveThread → attemptArchiveThread.
319
- const handleNewThreadRef = useRef(handleNewThread);
320
- handleNewThreadRef.current = handleNewThread;
321
-
322
- const resolveThreadTarget = useCallback((target: ScopedThreadRef) => {
307
+
308
+ const resolveThreadTarget = (target: ScopedThreadRef) => {
309
const thread = readThreadShell(target);
310
if (!thread) {
311
return null;
@@ -328,686 +314,599 @@ export function useThreadActions() {
314
thread,
315
threadRef: target,
316
};
331
- }, []);
332
- const getCurrentRouteThreadRef = useCallback(() => {
317
+ };
318
+ const getCurrentRouteThreadRef = () => {
319
const currentRouteParams = router.state.matches[router.state.matches.length - 1]?.params ?? {};
320
return resolveThreadRouteRef(currentRouteParams);
335
- }, [router]);
321
+ };
322
+
323
+ const unarchiveThread = async (target: ScopedThreadRef, opts: { navigate?: boolean } = {}) => {
324
+ ThreadUndo.invalidate("archive", scopedThreadKey(target));
325
+ const result = await unarchiveThreadMutation({
326
+ environmentId: target.environmentId,
327
+ input: { threadId: target.threadId },
328
+ });
329
+ if (result._tag === "Failure") {
330
+ return result;
331
+ }
332
+ refreshArchivedThreadsForEnvironment(target.environmentId);
333
+ if (opts.navigate) {
334
+ return settlePromise(() =>
335
+ router.navigate({
336
+ to: "/$environmentId/$threadId",
337
+ params: buildThreadRouteParams(target),
338
+ }),
339
+ );
340
+ }
341
+ return result;
342
+ };
343
+
344
+ const archiveThread = async (target: ScopedThreadRef, opts: { onArchived?: () => void } = {}) => {
345
+ const resolved = resolveThreadTarget(target);
346
+ if (!resolved) return AsyncResult.success(undefined);
347
+ const { thread, threadRef } = resolved;
348
+ if (!threadRuntimeCanArchive(thread.runtime)) {
349
+ return AsyncResult.failure(
350
+ Cause.fail(
351
+ new ThreadArchiveBlockedError({
352
+ environmentId: threadRef.environmentId,
353
+ threadId: threadRef.threadId,
354
+ }),
355
+ ),
356
+ );
357
+ }
358
+
359
+ const currentRouteThreadRef = getCurrentRouteThreadRef();
360
+ const shouldNavigateToDraft =
361
+ currentRouteThreadRef?.threadId === threadRef.threadId &&
362
+ currentRouteThreadRef.environmentId === threadRef.environmentId;
363
+ const action = ThreadUndo.begin("archive", scopedThreadKey(threadRef));
364
+ const archiveResult = await archiveThreadMutation({
365
+ environmentId: threadRef.environmentId,
366
+ input: { threadId: threadRef.threadId },
367
+ });
368
+ if (archiveResult._tag === "Failure") {
369
+ action.finish();
370
+ return archiveResult;
371
+ }
372
+ const wokeAt = threadWokeAt(thread, { now: new Date().toISOString() });
373
+ if (wokeAt !== null) {
374
+ markThreadVisited(scopedThreadKey(threadRef), wokeAt);
375
+ }
376
+ refreshArchivedThreadsForEnvironment(threadRef.environmentId);
377
+ opts.onArchived?.();
378
+ showThreadUndoNotice({
379
+ action: "Archived",
380
+ claim: action,
381
+ undo: () => unarchiveThread(threadRef, { navigate: shouldNavigateToDraft }),
382
+ failureTitle: "Failed to undo archive",
383
+ });
384
+
385
+ if (shouldNavigateToDraft) {
386
+ const navigationResult = await settlePromise(() =>
387
+ handleNewThread(scopeProjectRef(thread.environmentId, thread.projectId)),
388
+ );
389
+ if (navigationResult._tag === "Failure") {
390
+ return navigationResult;
391
+ }
392
+ return archiveResult;
393
+ }
394
337
- const unarchiveThread = useCallback(
338
- async (target: ScopedThreadRef, opts: { navigate?: boolean } = {}) => {
339
- ThreadUndo.invalidate("archive", scopedThreadKey(target));
340
- const result = await unarchiveThreadMutation({
395
+ return archiveResult;
396
+ };
397
+
398
+ const deleteThread = async (
399
+ target: ScopedThreadRef,
400
+ opts: { deletedThreadKeys?: ReadonlySet<string> } = {},
401
+ ) => {
402
+ const permissionFailure = threadOperationFailure(target);
403
+ if (permissionFailure) return permissionFailure;
404
+ const resolved = resolveThreadTarget(target);
405
+ if (!resolved) {
406
+ const result = await deleteThreadMutation({
407
environmentId: target.environmentId,
408
input: { threadId: target.threadId },
409
});
344
- if (result._tag === "Failure") {
345
- return result;
346
- }
347
- refreshArchivedThreadsForEnvironment(target.environmentId);
348
- if (opts.navigate) {
349
- return settlePromise(() =>
350
- router.navigate({
351
- to: "/$environmentId/$threadId",
352
- params: buildThreadRouteParams(target),
353
- }),
354
- );
410
+ if (result._tag === "Success") {
411
+ refreshArchivedThreadsForEnvironment(target.environmentId);
412
+ clearThreadPreviewState(target);
413
}
414
return result;
357
- },
358
- [router, unarchiveThreadMutation],
359
- );
360
-
361
- const archiveThread = useCallback(
362
- async (target: ScopedThreadRef, opts: { onArchived?: () => void } = {}) => {
363
- const resolved = resolveThreadTarget(target);
364
- if (!resolved) return AsyncResult.success(undefined);
365
- const { thread, threadRef } = resolved;
366
- if (!threadRuntimeCanArchive(thread.runtime)) {
367
- return AsyncResult.failure(
368
- Cause.fail(
369
- new ThreadArchiveBlockedError({
370
- environmentId: threadRef.environmentId,
371
- threadId: threadRef.threadId,
415
+ }
416
+ const { thread, threadRef } = resolved;
417
+ const threads = readEnvironmentThreadRefs(threadRef.environmentId).flatMap((ref) => {
418
+ const shell = readThreadShell(ref);
419
+ return shell === null ? [] : [shell];
420
+ });
421
+ const threadProject = readProject({
422
+ environmentId: threadRef.environmentId,
423
+ projectId: thread.projectId,
424
+ });
425
+ const deletedIds =
426
+ opts.deletedThreadKeys && opts.deletedThreadKeys.size > 0
427
+ ? new Set<ThreadId>(
428
+ [...opts.deletedThreadKeys].flatMap((threadKey) => {
429
+ const ref = parseScopedThreadKey(threadKey);
430
+ return ref && ref.environmentId === threadRef.environmentId ? [ref.threadId] : [];
431
}),
373
- ),
374
- );
375
- }
376
-
377
- const currentRouteThreadRef = getCurrentRouteThreadRef();
378
- const shouldNavigateToDraft =
379
- currentRouteThreadRef?.threadId === threadRef.threadId &&
380
- currentRouteThreadRef.environmentId === threadRef.environmentId;
381
- const action = ThreadUndo.begin("archive", scopedThreadKey(threadRef));
382
- const archiveResult = await archiveThreadMutation({
383
- environmentId: threadRef.environmentId,
384
- input: { threadId: threadRef.threadId },
385
- });
386
- if (archiveResult._tag === "Failure") {
387
- action.finish();
388
- return archiveResult;
389
- }
390
- const wokeAt = threadWokeAt(thread, { now: new Date().toISOString() });
391
- if (wokeAt !== null) {
392
- markThreadVisited(scopedThreadKey(threadRef), wokeAt);
393
- }
394
- refreshArchivedThreadsForEnvironment(threadRef.environmentId);
395
- opts.onArchived?.();
396
- showThreadUndoNotice({
397
- action: "Archived",
398
- claim: action,
399
- undo: () => unarchiveThread(threadRef, { navigate: shouldNavigateToDraft }),
400
- failureTitle: "Failed to undo archive",
401
- });
402
-
403
- if (shouldNavigateToDraft) {
404
- const navigationResult = await settlePromise(() =>
405
- handleNewThreadRef.current(scopeProjectRef(thread.environmentId, thread.projectId)),
406
- );
407
- if (navigationResult._tag === "Failure") {
408
- return navigationResult;
409
- }
410
- return archiveResult;
411
- }
412
-
413
- return archiveResult;
414
- },
415
- [
416
- archiveThreadMutation,
417
- getCurrentRouteThreadRef,
418
- markThreadVisited,
419
- resolveThreadTarget,
420
- unarchiveThread,
421
- ],
422
- );
423
-
424
- const deleteThread = useCallback(
425
- async (target: ScopedThreadRef, opts: { deletedThreadKeys?: ReadonlySet<string> } = {}) => {
426
- const permissionFailure = threadOperationFailure(target);
432
+ )
433
+ : undefined;
434
+ const survivingThreads =
435
+ deletedIds && deletedIds.size > 0
436
+ ? threads.filter((entry) => entry.id === threadRef.threadId || !deletedIds.has(entry.id))
437
+ : threads;
438
+ const orphanedWorktreePath = getOrphanedWorktreePathForThread(
439
+ survivingThreads,
440
+ threadRef.threadId,
441
+ );
442
+ const displayWorktreePath = orphanedWorktreePath
443
+ ? formatWorktreePathForDisplay(orphanedWorktreePath)
444
+ : null;
445
+ const environmentConfig = appAtomRegistry
446
+ .get(environmentServerConfigsAtom)
447
+ .get(threadRef.environmentId);
448
+ const localApi = readLocalApi();
449
+ let canDeleteWorktree = false;
450
+ if (
451
+ orphanedWorktreePath !== null &&
452
+ threadProject !== null &&
453
+ !isScratchProject(threadProject, environmentConfig?.scratchWorkspaceRoot) &&
454
+ localApi
455
+ ) {
456
+ const sessionResult = await loadSessionState(threadRef.environmentId);
457
+ const permissionFailure = threadOperationFailure(threadRef);
458
if (permissionFailure) return permissionFailure;
428
- const resolved = resolveThreadTarget(target);
429
- if (!resolved) {
430
- const result = await deleteThreadMutation({
431
- environmentId: target.environmentId,
432
- input: { threadId: target.threadId },
433
- });
434
- if (result._tag === "Success") {
435
- refreshArchivedThreadsForEnvironment(target.environmentId);
436
- clearThreadPreviewState(target);
437
- }
438
- return result;
439
- }
440
- const { thread, threadRef } = resolved;
441
- const threads = readEnvironmentThreadRefs(threadRef.environmentId).flatMap((ref) => {
442
- const shell = readThreadShell(ref);
443
- return shell === null ? [] : [shell];
444
- });
445
- const threadProject = readProject({
446
- environmentId: threadRef.environmentId,
447
- projectId: thread.projectId,
448
- });
449
- const deletedIds =
450
- opts.deletedThreadKeys && opts.deletedThreadKeys.size > 0
451
- ? new Set<ThreadId>(
452
- [...opts.deletedThreadKeys].flatMap((threadKey) => {
453
- const ref = parseScopedThreadKey(threadKey);
454
- return ref && ref.environmentId === threadRef.environmentId ? [ref.threadId] : [];
455
- }),
456
- )
457
- : undefined;
458
- const survivingThreads =
459
- deletedIds && deletedIds.size > 0
460
- ? threads.filter((entry) => entry.id === threadRef.threadId || !deletedIds.has(entry.id))
461
- : threads;
462
- const orphanedWorktreePath = getOrphanedWorktreePathForThread(
463
- survivingThreads,
464
- threadRef.threadId,
459
+ canDeleteWorktree =
460
+ sessionResult._tag === "Success" &&
461
+ sessionGrantsScope(sessionResult.value, AuthSourceControlWriteScope);
462
+ }
463
+ let shouldDeleteWorktree = false;
464
+ const environmentSettings = environmentConfig?.settings;
465
+ const automaticWorktreeCleanup = environmentSettings
466
+ ? resolveWorktreeCleanup(environmentSettings, thread.projectId).worktreeOnDelete
467
+ : false;
468
+ if (canDeleteWorktree && localApi && !automaticWorktreeCleanup) {
469
+ const confirmationResult = await settlePromise(() =>
470
+ localApi.dialogs.confirm(
471
+ [
472
+ "This thread is the only one linked to this worktree:",
473
+ displayWorktreePath ?? orphanedWorktreePath,
474
+ "",
475
+ "Delete the worktree too?",
476
+ ].join("\n"),
477
+ { variant: "destructive" },
478
+ ),
479
);
466
- const displayWorktreePath = orphanedWorktreePath
467
- ? formatWorktreePathForDisplay(orphanedWorktreePath)
468
- : null;
469
- const environmentConfig = appAtomRegistry
470
- .get(environmentServerConfigsAtom)
471
- .get(threadRef.environmentId);
472
- const localApi = readLocalApi();
473
- let canDeleteWorktree = false;
474
- if (
475
- orphanedWorktreePath !== null &&
476
- threadProject !== null &&
477
- !isScratchProject(threadProject, environmentConfig?.scratchWorkspaceRoot) &&
478
- localApi
479
- ) {
480
- const sessionResult = await loadSessionState(threadRef.environmentId);
481
- const permissionFailure = threadOperationFailure(threadRef);
482
- if (permissionFailure) return permissionFailure;
483
- canDeleteWorktree =
484
- sessionResult._tag === "Success" &&
485
- sessionGrantsScope(sessionResult.value, AuthSourceControlWriteScope);
486
- }
487
- let shouldDeleteWorktree = false;
488
- const environmentSettings = environmentConfig?.settings;
489
- const automaticWorktreeCleanup = environmentSettings
490
- ? resolveWorktreeCleanup(environmentSettings, thread.projectId).worktreeOnDelete
491
- : false;
492
- if (canDeleteWorktree && localApi && !automaticWorktreeCleanup) {
493
- const confirmationResult = await settlePromise(() =>
494
- localApi.dialogs.confirm(
495
- [
496
- "This thread is the only one linked to this worktree:",
497
- displayWorktreePath ?? orphanedWorktreePath,
498
- "",
499
- "Delete the worktree too?",
500
- ].join("\n"),
501
- { variant: "destructive" },
502
- ),
503
- );
504
- if (confirmationResult._tag === "Failure") {
505
- return confirmationResult;
506
- }
507
- shouldDeleteWorktree = confirmationResult.value;
508
- }
509
-
510
- if (thread.runtime !== null) {
511
- await stopThreadSession({
512
- environmentId: threadRef.environmentId,
513
- input: { threadId: threadRef.threadId },
514
- });
480
+ if (confirmationResult._tag === "Failure") {
481
+ return confirmationResult;
482
}
483
+ shouldDeleteWorktree = confirmationResult.value;
484
+ }
485
517
- const deletedThreadIds = deletedIds ?? new Set<ThreadId>();
518
- const currentRouteThreadRef = getCurrentRouteThreadRef();
519
- const shouldNavigateToFallback =
520
- currentRouteThreadRef?.threadId === threadRef.threadId &&
521
- currentRouteThreadRef.environmentId === threadRef.environmentId;
522
- const fallbackThreadId = getFallbackThreadIdAfterDelete({
523
- threads,
524
- deletedThreadId: threadRef.threadId,
525
- deletedThreadIds,
526
- sortOrder: sidebarThreadSortOrder,
527
- });
528
- const deleteResult = await deleteThreadMutation({
486
+ if (thread.runtime !== null) {
487
+ await stopThreadSession({
488
environmentId: threadRef.environmentId,
489
input: { threadId: threadRef.threadId },
490
});
532
- if (deleteResult._tag === "Failure") {
533
- return deleteResult;
534
- }
535
- refreshArchivedThreadsForEnvironment(threadRef.environmentId);
536
- releaseComposerDraftUploads(threadRef);
537
- clearComposerDraftForThread(threadRef);
538
- clearProjectDraftThreadById(
539
- scopeProjectRef(threadRef.environmentId, thread.projectId),
540
- threadRef,
541
- );
542
- clearTerminalUiState(threadRef);
543
- clearThreadPreviewState(threadRef);
544
-
545
- if (shouldNavigateToFallback) {
546
- const fallbackThread = fallbackThreadId
547
- ? readThreadShell(scopeThreadRef(threadRef.environmentId, fallbackThreadId))
548
- : null;
549
- await navigateAfterThreadDeletion(() =>
550
- fallbackThread
551
- ? router.navigate({
552
- to: "/$environmentId/$threadId",
553
- params: buildThreadRouteParams(
554
- scopeThreadRef(fallbackThread.environmentId, fallbackThread.id),
555
- ),
556
- replace: true,
557
- })
558
- : router.navigate({ to: "/", replace: true }),
559
- );
560
- }
561
-
562
- if (!shouldDeleteWorktree || !orphanedWorktreePath || !threadProject) {
563
- return deleteResult;
564
- }
491
+ }
492
566
- const removeResult = readEnvironmentScope(
567
- threadRef.environmentId,
568
- AuthSourceControlWriteScope,
569
- )
570
- ? await removeWorktree({
571
- environmentId: threadRef.environmentId,
572
- input: {
573
- cwd: threadProject.workspaceRoot,
574
- path: orphanedWorktreePath,
575
- force: true,
576
- },
577
- })
578
- : AsyncResult.failure(
579
- Cause.fail(
580
- new EnvironmentAuthorizationError({
581
- message: "This connection can no longer remove worktrees.",
582
- requiredScope: AuthSourceControlWriteScope,
583
- }),
584
- ),
585
- );
586
- const refreshResult =
587
- removeResult._tag === "Success"
588
- ? await refreshVcsStatus({
589
- environmentId: threadRef.environmentId,
590
- input: { cwd: threadProject.workspaceRoot },
591
- })
592
- : null;
593
- const cleanupFailure =
594
- removeResult._tag === "Failure"
595
- ? removeResult
596
- : refreshResult?._tag === "Failure"
597
- ? refreshResult
598
- : null;
599
- if (cleanupFailure) {
600
- const removalFailed = removeResult._tag === "Failure";
601
- const error = squashAtomCommandFailure(cleanupFailure);
602
- const message = error instanceof Error ? error.message : "An error occurred.";
603
- console.error("Worktree cleanup failed after thread deletion", {
604
- threadId: threadRef.threadId,
605
- projectCwd: threadProject.workspaceRoot,
606
- worktreePath: orphanedWorktreePath,
607
- error,
608
- });
609
- toastManager.add(
610
- stackedThreadToast({
611
- type: "error",
612
- title: removalFailed
613
- ? "Failed to delete worktree"
614
- : "Worktree deleted, but Git status refresh failed",
615
- description: removalFailed
616
- ? `Could not remove ${displayWorktreePath ?? orphanedWorktreePath}. ${message}`
617
- : message,
618
- }),
619
- );
620
- // The thread was deleted. Cleanup has its own toast; returning its
621
- // failure would make callers incorrectly report a thread deletion error.
622
- }
493
+ const deletedThreadIds = deletedIds ?? new Set<ThreadId>();
494
+ const currentRouteThreadRef = getCurrentRouteThreadRef();
495
+ const shouldNavigateToFallback =
496
+ currentRouteThreadRef?.threadId === threadRef.threadId &&
497
+ currentRouteThreadRef.environmentId === threadRef.environmentId;
498
+ const fallbackThreadId = getFallbackThreadIdAfterDelete({
499
+ threads,
500
+ deletedThreadId: threadRef.threadId,
501
+ deletedThreadIds,
502
+ sortOrder: sidebarThreadSortOrder,
503
+ });
504
+ const deleteResult = await deleteThreadMutation({
505
+ environmentId: threadRef.environmentId,
506
+ input: { threadId: threadRef.threadId },
507
+ });
508
+ if (deleteResult._tag === "Failure") {
509
return deleteResult;
624
- },
625
- [
626
- clearComposerDraftForThread,
627
- clearProjectDraftThreadById,
628
- clearTerminalUiState,
629
- deleteThreadMutation,
630
- getCurrentRouteThreadRef,
631
- loadSessionState,
632
- refreshVcsStatus,
633
- removeWorktree,
634
- router,
635
- resolveThreadTarget,
636
- sidebarThreadSortOrder,
637
- stopThreadSession,
638
- ],
639
- );
640
-
641
- const unsettleThread = useCallback(
642
- async (target: ScopedThreadRef) => {
643
- if (!readEnvironmentSupportsSettlement(target.environmentId)) {
644
- return AsyncResult.failure(
645
- Cause.fail(
646
- new ThreadSettlementUnsupportedError({
647
- environmentId: target.environmentId,
648
- threadId: target.threadId,
649
- }),
650
- ),
651
- );
652
- }
653
- ThreadUndo.invalidate("settle", scopedThreadKey(target));
654
- // reason "user" pins the thread active: auto-settle (PR merged /
655
- // inactivity) stays suppressed until real activity clears the pin.
656
- return unsettleThreadMutation({
657
- environmentId: target.environmentId,
658
- input: { threadId: target.threadId, reason: "user" },
659
- });
660
- },
661
- [unsettleThreadMutation],
662
- );
663
-
664
- const setThreadAutoSettle = useCallback(
665
- async (target: ScopedThreadRef, enabled: boolean) => {
666
- if (!readEnvironmentSupportsAutoSettleOptOut(target.environmentId)) {
667
- return AsyncResult.failure(
668
- Cause.fail(
669
- new ThreadAutoSettleOptOutUnsupportedError({
670
- environmentId: target.environmentId,
671
- threadId: target.threadId,
672
- }),
673
- ),
674
- );
675
- }
676
- return setThreadAutoSettleMutation({
677
- environmentId: target.environmentId,
678
- input: { threadId: target.threadId, enabled },
679
- });
680
- },
681
- [setThreadAutoSettleMutation],
682
- );
510
+ }
511
+ refreshArchivedThreadsForEnvironment(threadRef.environmentId);
512
+ releaseComposerDraftUploads(threadRef);
513
+ clearComposerDraftForThread(threadRef);
514
+ clearProjectDraftThreadById(
515
+ scopeProjectRef(threadRef.environmentId, thread.projectId),
516
+ threadRef,
517
+ );
518
+ clearTerminalUiState(threadRef);
519
+ clearThreadPreviewState(threadRef);
520
684
- const pinThread = useCallback(
685
- async (target: ScopedThreadRef, opts: { orderKey?: string } = {}) => {
686
- if (!readEnvironmentSupportsPinning(target.environmentId)) {
687
- return AsyncResult.failure(
688
- Cause.fail(
689
- new ThreadPinningUnsupportedError({
690
- environmentId: target.environmentId,
691
- threadId: target.threadId,
692
- }),
693
- ),
694
- );
695
- }
696
- // orderKey rides only to servers that decode it; pre-reorder servers get the bare pin they
697
- // understand and the thread stays keyless.
698
- const orderKey = readEnvironmentSupportsPinReorder(target.environmentId)
699
- ? (opts.orderKey ?? topOfPinnedRunOrderKey())
700
- : undefined;
701
- ThreadUndo.invalidate("pin", scopedThreadKey(target));
702
- return pinThreadMutation({
703
- environmentId: target.environmentId,
704
- input: {
705
- threadId: target.threadId,
706
- ...(orderKey !== undefined ? { orderKey } : {}),
707
- },
708
- });
709
- },
710
- [pinThreadMutation],
711
- );
521
+ if (shouldNavigateToFallback) {
522
+ const fallbackThread = fallbackThreadId
523
+ ? readThreadShell(scopeThreadRef(threadRef.environmentId, fallbackThreadId))
524
+ : null;
525
+ await navigateAfterThreadDeletion(() =>
526
+ fallbackThread
527
+ ? router.navigate({
528
+ to: "/$environmentId/$threadId",
529
+ params: buildThreadRouteParams(
530
+ scopeThreadRef(fallbackThread.environmentId, fallbackThread.id),
531
+ ),
532
+ replace: true,
533
+ })
534
+ : router.navigate({ to: "/", replace: true }),
535
+ );
536
+ }
537
713
- const unpinThread = useCallback(
714
- async (target: ScopedThreadRef) => {
715
- if (!readEnvironmentSupportsPinning(target.environmentId)) {
716
- return AsyncResult.failure(
717
- Cause.fail(
718
- new ThreadPinningUnsupportedError({
719
- environmentId: target.environmentId,
720
- threadId: target.threadId,
721
- }),
722
- ),
723
- );
724
- }
725
- const thread = readThreadShell(target);
726
- const orderKey = thread?.pinOrderKey ?? undefined;
727
- const action = ThreadUndo.begin("pin", scopedThreadKey(target));
728
- const result = await unpinThreadMutation({
729
- environmentId: target.environmentId,
730
- input: { threadId: target.threadId },
731
- });
732
- if (result._tag === "Success" && action.isCurrent()) {
733
- showThreadUndoNotice({
734
- action: "Unpinned",
735
- claim: action,
736
- undo: () => pinThread(target, orderKey === undefined ? {} : { orderKey }),
737
- failureTitle: "Failed to undo unpin",
738
- });
739
- } else {
740
- action.finish();
741
- }
742
- return result;
743
- },
744
- [pinThread, unpinThreadMutation],
745
- );
538
+ if (!shouldDeleteWorktree || !orphanedWorktreePath || !threadProject) {
539
+ return deleteResult;
540
+ }
541
747
- const settleThread = useCallback(
748
- async (target: ScopedThreadRef) => {
749
- if (!readEnvironmentSupportsSettlement(target.environmentId)) {
750
- return AsyncResult.failure(
542
+ const removeResult = readEnvironmentScope(threadRef.environmentId, AuthSourceControlWriteScope)
543
+ ? await removeWorktree({
544
+ environmentId: threadRef.environmentId,
545
+ input: {
546
+ cwd: threadProject.workspaceRoot,
547
+ path: orphanedWorktreePath,
548
+ force: true,
549
+ },
550
+ })
551
+ : AsyncResult.failure(
552
Cause.fail(
752
- new ThreadSettlementUnsupportedError({
753
- environmentId: target.environmentId,
754
- threadId: target.threadId,
553
+ new EnvironmentAuthorizationError({
554
+ message: "This connection can no longer remove worktrees.",
555
+ requiredScope: AuthSourceControlWriteScope,
556
}),
557
),
558
);
758
- }
759
- const resolved = resolveThreadTarget(target);
760
- const wokeAt = resolved
761
- ? threadWokeAt(resolved.thread, { now: new Date().toISOString() })
559
+ const refreshResult =
560
+ removeResult._tag === "Success"
561
+ ? await refreshVcsStatus({
562
+ environmentId: threadRef.environmentId,
563
+ input: { cwd: threadProject.workspaceRoot },
564
+ })
565
: null;
763
- // Settling also drops the pin and the snooze server-side, so Undo
764
- // has to put those back as well.
765
- const pinOrderKey = resolved?.thread.pinnedAt != null ? resolved.thread.pinOrderKey : null;
766
- const wasPinned = resolved?.thread.pinnedAt != null;
767
- const snoozedUntil = resolved?.thread.snoozedUntil ?? null;
768
- // An older unpin/snooze Undo would re-pin or re-snooze, and the server
769
- // treats either as a promotion that un-settles; settling supersedes them.
770
- ThreadUndo.invalidate("pin", scopedThreadKey(target));
771
- ThreadUndo.invalidate("snooze", scopedThreadKey(target));
772
- const action = ThreadUndo.begin("settle", scopedThreadKey(target));
773
- const result = await settleThreadMutation({
774
- environmentId: target.environmentId,
775
- input: { threadId: target.threadId },
566
+ const cleanupFailure =
567
+ removeResult._tag === "Failure"
568
+ ? removeResult
569
+ : refreshResult?._tag === "Failure"
570
+ ? refreshResult
571
+ : null;
572
+ if (cleanupFailure) {
573
+ const removalFailed = removeResult._tag === "Failure";
574
+ const error = squashAtomCommandFailure(cleanupFailure);
575
+ const message = error instanceof Error ? error.message : "An error occurred.";
576
+ console.error("Worktree cleanup failed after thread deletion", {
577
+ threadId: threadRef.threadId,
578
+ projectCwd: threadProject.workspaceRoot,
579
+ worktreePath: orphanedWorktreePath,
580
+ error,
581
});
777
- if (result._tag !== "Success") {
778
- action.finish();
779
- return result;
780
- }
781
- if (wokeAt !== null) {
782
- markThreadVisited(scopedThreadKey(target), wokeAt);
783
- }
582
+ toastManager.add(
583
+ stackedThreadToast({
584
+ type: "error",
585
+ title: removalFailed
586
+ ? "Failed to delete worktree"
587
+ : "Worktree deleted, but Git status refresh failed",
588
+ description: removalFailed
589
+ ? `Could not remove ${displayWorktreePath ?? orphanedWorktreePath}. ${message}`
590
+ : message,
591
+ }),
592
+ );
593
+ // The thread was deleted. Cleanup has its own toast; returning its
594
+ // failure would make callers incorrectly report a thread deletion error.
595
+ }
596
+ return deleteResult;
597
+ };
598
+
599
+ const unsettleThread = async (target: ScopedThreadRef) => {
600
+ if (!readEnvironmentSupportsSettlement(target.environmentId)) {
601
+ return AsyncResult.failure(
602
+ Cause.fail(
603
+ new ThreadSettlementUnsupportedError({
604
+ environmentId: target.environmentId,
605
+ threadId: target.threadId,
606
+ }),
607
+ ),
608
+ );
609
+ }
610
+ ThreadUndo.invalidate("settle", scopedThreadKey(target));
611
+ // reason "user" pins the thread active: auto-settle (PR merged /
612
+ // inactivity) stays suppressed until real activity clears the pin.
613
+ return unsettleThreadMutation({
614
+ environmentId: target.environmentId,
615
+ input: { threadId: target.threadId, reason: "user" },
616
+ });
617
+ };
618
+
619
+ const setThreadAutoSettle = async (target: ScopedThreadRef, enabled: boolean) => {
620
+ if (!readEnvironmentSupportsAutoSettleOptOut(target.environmentId)) {
621
+ return AsyncResult.failure(
622
+ Cause.fail(
623
+ new ThreadAutoSettleOptOutUnsupportedError({
624
+ environmentId: target.environmentId,
625
+ threadId: target.threadId,
626
+ }),
627
+ ),
628
+ );
629
+ }
630
+ return setThreadAutoSettleMutation({
631
+ environmentId: target.environmentId,
632
+ input: { threadId: target.threadId, enabled },
633
+ });
634
+ };
635
+
636
+ const pinThread = async (target: ScopedThreadRef, opts: { orderKey?: string } = {}) => {
637
+ if (!readEnvironmentSupportsPinning(target.environmentId)) {
638
+ return AsyncResult.failure(
639
+ Cause.fail(
640
+ new ThreadPinningUnsupportedError({
641
+ environmentId: target.environmentId,
642
+ threadId: target.threadId,
643
+ }),
644
+ ),
645
+ );
646
+ }
647
+ // orderKey rides only to servers that decode it; pre-reorder servers get the bare pin they
648
+ // understand and the thread stays keyless.
649
+ const orderKey = readEnvironmentSupportsPinReorder(target.environmentId)
650
+ ? (opts.orderKey ?? topOfPinnedRunOrderKey())
651
+ : undefined;
652
+ ThreadUndo.invalidate("pin", scopedThreadKey(target));
653
+ return pinThreadMutation({
654
+ environmentId: target.environmentId,
655
+ input: {
656
+ threadId: target.threadId,
657
+ ...(orderKey !== undefined ? { orderKey } : {}),
658
+ },
659
+ });
660
+ };
661
+
662
+ const unpinThread = async (target: ScopedThreadRef) => {
663
+ if (!readEnvironmentSupportsPinning(target.environmentId)) {
664
+ return AsyncResult.failure(
665
+ Cause.fail(
666
+ new ThreadPinningUnsupportedError({
667
+ environmentId: target.environmentId,
668
+ threadId: target.threadId,
669
+ }),
670
+ ),
671
+ );
672
+ }
673
+ const thread = readThreadShell(target);
674
+ const orderKey = thread?.pinOrderKey ?? undefined;
675
+ const action = ThreadUndo.begin("pin", scopedThreadKey(target));
676
+ const result = await unpinThreadMutation({
677
+ environmentId: target.environmentId,
678
+ input: { threadId: target.threadId },
679
+ });
680
+ if (result._tag === "Success" && action.isCurrent()) {
681
showThreadUndoNotice({
785
- action: "Settled",
682
+ action: "Unpinned",
683
claim: action,
787
- undo: async () => {
788
- const unsettled = await unsettleThread(target);
789
- if (unsettled._tag !== "Success") return unsettled;
790
- if (wasPinned) {
791
- const pinned = await pinThread(
792
- target,
793
- pinOrderKey == null ? {} : { orderKey: pinOrderKey },
794
- );
795
- if (pinned._tag !== "Success") return pinned;
796
- }
797
- if (snoozedUntil !== null) {
798
- return snoozeThreadMutation({
799
- environmentId: target.environmentId,
800
- input: { threadId: target.threadId, snoozedUntil },
801
- });
802
- }
803
- return unsettled;
804
- },
805
- failureTitle: "Failed to undo settle",
684
+ undo: () => pinThread(target, orderKey === undefined ? {} : { orderKey }),
685
+ failureTitle: "Failed to undo unpin",
686
});
687
+ } else {
688
+ action.finish();
689
+ }
690
+ return result;
691
+ };
692
+
693
+ const settleThread = async (target: ScopedThreadRef) => {
694
+ if (!readEnvironmentSupportsSettlement(target.environmentId)) {
695
+ return AsyncResult.failure(
696
+ Cause.fail(
697
+ new ThreadSettlementUnsupportedError({
698
+ environmentId: target.environmentId,
699
+ threadId: target.threadId,
700
+ }),
701
+ ),
702
+ );
703
+ }
704
+ const resolved = resolveThreadTarget(target);
705
+ const wokeAt = resolved
706
+ ? threadWokeAt(resolved.thread, { now: new Date().toISOString() })
707
+ : null;
708
+ // Settling also drops the pin and the snooze server-side, so Undo
709
+ // has to put those back as well.
710
+ const pinOrderKey = resolved?.thread.pinnedAt != null ? resolved.thread.pinOrderKey : null;
711
+ const wasPinned = resolved?.thread.pinnedAt != null;
712
+ const snoozedUntil = resolved?.thread.snoozedUntil ?? null;
713
+ // An older unpin/snooze Undo would re-pin or re-snooze, and the server
714
+ // treats either as a promotion that un-settles; settling supersedes them.
715
+ ThreadUndo.invalidate("pin", scopedThreadKey(target));
716
+ ThreadUndo.invalidate("snooze", scopedThreadKey(target));
717
+ const action = ThreadUndo.begin("settle", scopedThreadKey(target));
718
+ const result = await settleThreadMutation({
719
+ environmentId: target.environmentId,
720
+ input: { threadId: target.threadId },
721
+ });
722
+ if (result._tag !== "Success") {
723
+ action.finish();
724
return result;
808
- },
809
- [
810
- markThreadVisited,
811
- pinThread,
812
- resolveThreadTarget,
813
- settleThreadMutation,
814
- snoozeThreadMutation,
815
- unsettleThread,
816
- ],
817
- );
725
+ }
726
+ if (wokeAt !== null) {
727
+ markThreadVisited(scopedThreadKey(target), wokeAt);
728
+ }
729
+ showThreadUndoNotice({
730
+ action: "Settled",
731
+ claim: action,
732
+ undo: async () => {
733
+ const unsettled = await unsettleThread(target);
734
+ if (unsettled._tag !== "Success") return unsettled;
735
+ if (wasPinned) {
736
+ const pinned = await pinThread(
737
+ target,
738
+ pinOrderKey == null ? {} : { orderKey: pinOrderKey },
739
+ );
740
+ if (pinned._tag !== "Success") return pinned;
741
+ }
742
+ if (snoozedUntil !== null) {
743
+ return snoozeThreadMutation({
744
+ environmentId: target.environmentId,
745
+ input: { threadId: target.threadId, snoozedUntil },
746
+ });
747
+ }
748
+ return unsettled;
749
+ },
750
+ failureTitle: "Failed to undo settle",
751
+ });
752
+ return result;
753
+ };
754
+
755
+ const confirmAndUnpinThread = async (target: ScopedThreadRef) => {
756
+ const permissionFailure = threadOperationFailure(target);
757
+ if (permissionFailure) return permissionFailure;
758
+ const localApi = readLocalApi();
759
+ const resolved = resolveThreadTarget(target);
760
+ const confirmationResult = await requestThreadUnpinConfirmation({
761
+ enabled: confirmThreadUnpin,
762
+ title: resolved?.thread.title ?? "this thread",
763
+ confirm: localApi ? (message) => localApi.dialogs.confirm(message) : null,
764
+ });
765
+ if (confirmationResult._tag === "Failure") {
766
+ return confirmationResult;
767
+ }
768
+ if (!confirmationResult.value) {
769
+ return AsyncResult.success(undefined);
770
+ }
771
+ return unpinThread(target);
772
+ };
773
819
- const confirmAndUnpinThread = useCallback(
820
- async (target: ScopedThreadRef) => {
821
- const permissionFailure = threadOperationFailure(target);
822
- if (permissionFailure) return permissionFailure;
823
- const localApi = readLocalApi();
824
- const resolved = resolveThreadTarget(target);
825
- const confirmationResult = await requestThreadUnpinConfirmation({
826
- enabled: confirmThreadUnpin,
827
- title: resolved?.thread.title ?? "this thread",
828
- confirm: localApi ? (message) => localApi.dialogs.confirm(message) : null,
829
- });
774
+ const reorderPinnedThread = async (target: ScopedThreadRef, orderKey: string) => {
775
+ if (!readEnvironmentSupportsPinReorder(target.environmentId)) {
776
+ return AsyncResult.failure(
777
+ Cause.fail(
778
+ new ThreadPinReorderUnsupportedError({
779
+ environmentId: target.environmentId,
780
+ threadId: target.threadId,
781
+ }),
782
+ ),
783
+ );
784
+ }
785
+ ThreadUndo.invalidate("pin", scopedThreadKey(target));
786
+ return reorderPinnedThreadMutation({
787
+ environmentId: target.environmentId,
788
+ input: { threadId: target.threadId, orderKey },
789
+ });
790
+ };
791
+
792
+ const reorderActiveThread = async (target: ScopedThreadRef, orderKey: string) => {
793
+ if (!readEnvironmentSupportsActiveReorder(target.environmentId)) {
794
+ return AsyncResult.failure(
795
+ Cause.fail(
796
+ new ThreadActiveReorderUnsupportedError({
797
+ environmentId: target.environmentId,
798
+ threadId: target.threadId,
799
+ }),
800
+ ),
801
+ );
802
+ }
803
+ return reorderActiveThreadMutation({
804
+ environmentId: target.environmentId,
805
+ input: { threadId: target.threadId, orderKey },
806
+ });
807
+ };
808
+
809
+ const unsnoozeThread = async (target: ScopedThreadRef) => {
810
+ if (!readEnvironmentSupportsSnooze(target.environmentId)) {
811
+ return AsyncResult.failure(
812
+ Cause.fail(
813
+ new ThreadSnoozeUnsupportedError({
814
+ environmentId: target.environmentId,
815
+ threadId: target.threadId,
816
+ }),
817
+ ),
818
+ );
819
+ }
820
+ ThreadUndo.invalidate("snooze", scopedThreadKey(target));
821
+ return unsnoozeThreadMutation({
822
+ environmentId: target.environmentId,
823
+ input: { threadId: target.threadId, reason: "user" },
824
+ });
825
+ };
826
+
827
+ const snoozeThread = async (target: ScopedThreadRef, snoozedUntil: string) => {
828
+ if (!readEnvironmentSupportsSnooze(target.environmentId)) {
829
+ return AsyncResult.failure(
830
+ Cause.fail(
831
+ new ThreadSnoozeUnsupportedError({
832
+ environmentId: target.environmentId,
833
+ threadId: target.threadId,
834
+ }),
835
+ ),
836
+ );
837
+ }
838
+ const resolved = resolveThreadTarget(target);
839
+ if (resolved && !canSnooze(resolved.thread, { now: new Date().toISOString() })) {
840
+ return AsyncResult.failure(
841
+ Cause.fail(
842
+ new ThreadSnoozeBlockedError({
843
+ environmentId: resolved.threadRef.environmentId,
844
+ threadId: resolved.threadRef.threadId,
845
+ }),
846
+ ),
847
+ );
848
+ }
849
+ const action = ThreadUndo.begin("snooze", scopedThreadKey(target));
850
+ const result = await snoozeThreadMutation({
851
+ environmentId: target.environmentId,
852
+ input: { threadId: target.threadId, snoozedUntil },
853
+ });
854
+ if (result._tag !== "Success") {
855
+ action.finish();
856
+ return result;
857
+ }
858
+ showThreadUndoNotice({
859
+ action: "Snoozed",
860
+ claim: action,
861
+ undo: () => unsnoozeThread(target),
862
+ failureTitle: "Failed to wake thread",
863
+ });
864
+ return result;
865
+ };
866
+
867
+ const confirmAndDeleteThread = async (target: ScopedThreadRef) => {
868
+ const permissionFailure = threadOperationFailure(target);
869
+ if (permissionFailure) return permissionFailure;
870
+ const localApi = readLocalApi();
871
+ const resolved = resolveThreadTarget(target);
872
+
873
+ if (confirmThreadDelete && localApi) {
874
+ const title = resolved?.thread.title ?? "this thread";
875
+ const confirmationResult = await settlePromise(() =>
876
+ localApi.dialogs.confirm(
877
+ [
878
+ `Delete thread "${title}"?`,
879
+ "This permanently clears conversation history for this thread.",
880
+ ].join("\n"),
881
+ { variant: "destructive" },
882
+ ),
883
+ );
884
if (confirmationResult._tag === "Failure") {
885
return confirmationResult;
886
}
887
if (!confirmationResult.value) {
888
return AsyncResult.success(undefined);
889
}
836
- return unpinThread(target);
837
- },
838
- [confirmThreadUnpin, resolveThreadTarget, unpinThread],
839
- );
840
-
841
- const reorderPinnedThread = useCallback(
842
- async (target: ScopedThreadRef, orderKey: string) => {
843
- if (!readEnvironmentSupportsPinReorder(target.environmentId)) {
844
- return AsyncResult.failure(
845
- Cause.fail(
846
- new ThreadPinReorderUnsupportedError({
847
- environmentId: target.environmentId,
848
- threadId: target.threadId,
849
- }),
850
- ),
851
- );
852
- }
853
- ThreadUndo.invalidate("pin", scopedThreadKey(target));
854
- return reorderPinnedThreadMutation({
855
- environmentId: target.environmentId,
856
- input: { threadId: target.threadId, orderKey },
857
- });
858
- },
859
- [reorderPinnedThreadMutation],
860
- );
861
-
862
- const reorderActiveThread = useCallback(
863
- async (target: ScopedThreadRef, orderKey: string) => {
864
- if (!readEnvironmentSupportsActiveReorder(target.environmentId)) {
865
- return AsyncResult.failure(
866
- Cause.fail(
867
- new ThreadActiveReorderUnsupportedError({
868
- environmentId: target.environmentId,
869
- threadId: target.threadId,
870
- }),
871
- ),
872
- );
873
- }
874
- return reorderActiveThreadMutation({
875
- environmentId: target.environmentId,
876
- input: { threadId: target.threadId, orderKey },
877
- });
878
- },
879
- [reorderActiveThreadMutation],
880
- );
881
-
882
- const unsnoozeThread = useCallback(
883
- async (target: ScopedThreadRef) => {
884
- if (!readEnvironmentSupportsSnooze(target.environmentId)) {
885
- return AsyncResult.failure(
886
- Cause.fail(
887
- new ThreadSnoozeUnsupportedError({
888
- environmentId: target.environmentId,
889
- threadId: target.threadId,
890
- }),
891
- ),
892
- );
893
- }
894
- ThreadUndo.invalidate("snooze", scopedThreadKey(target));
895
- return unsnoozeThreadMutation({
896
- environmentId: target.environmentId,
897
- input: { threadId: target.threadId, reason: "user" },
898
- });
899
- },
900
- [unsnoozeThreadMutation],
901
- );
902
-
903
- const snoozeThread = useCallback(
904
- async (target: ScopedThreadRef, snoozedUntil: string) => {
905
- if (!readEnvironmentSupportsSnooze(target.environmentId)) {
906
- return AsyncResult.failure(
907
- Cause.fail(
908
- new ThreadSnoozeUnsupportedError({
909
- environmentId: target.environmentId,
910
- threadId: target.threadId,
911
- }),
912
- ),
913
- );
914
- }
915
- const resolved = resolveThreadTarget(target);
916
- if (resolved && !canSnooze(resolved.thread, { now: new Date().toISOString() })) {
917
- return AsyncResult.failure(
918
- Cause.fail(
919
- new ThreadSnoozeBlockedError({
920
- environmentId: resolved.threadRef.environmentId,
921
- threadId: resolved.threadRef.threadId,
922
- }),
923
- ),
924
- );
925
- }
926
- const action = ThreadUndo.begin("snooze", scopedThreadKey(target));
927
- const result = await snoozeThreadMutation({
928
- environmentId: target.environmentId,
929
- input: { threadId: target.threadId, snoozedUntil },
930
- });
931
- if (result._tag !== "Success") {
932
- action.finish();
933
- return result;
934
- }
935
- showThreadUndoNotice({
936
- action: "Snoozed",
937
- claim: action,
938
- undo: () => unsnoozeThread(target),
939
- failureTitle: "Failed to wake thread",
940
- });
941
- return result;
942
- },
943
- [resolveThreadTarget, snoozeThreadMutation, unsnoozeThread],
944
- );
945
-
946
- const confirmAndDeleteThread = useCallback(
947
- async (target: ScopedThreadRef) => {
948
- const permissionFailure = threadOperationFailure(target);
949
- if (permissionFailure) return permissionFailure;
950
- const localApi = readLocalApi();
951
- const resolved = resolveThreadTarget(target);
952
-
953
- if (confirmThreadDelete && localApi) {
954
- const title = resolved?.thread.title ?? "this thread";
955
- const confirmationResult = await settlePromise(() =>
956
- localApi.dialogs.confirm(
957
- [
958
- `Delete thread "${title}"?`,
959
- "This permanently clears conversation history for this thread.",
960
- ].join("\n"),
961
- { variant: "destructive" },
962
- ),
963
- );
964
- if (confirmationResult._tag === "Failure") {
965
- return confirmationResult;
966
- }
967
- if (!confirmationResult.value) {
968
- return AsyncResult.success(undefined);
969
- }
970
- }
971
-
972
- return deleteThread(target);
973
- },
974
- [confirmThreadDelete, deleteThread, resolveThreadTarget],
975
- );
890
+ }
891
977
- return useMemo(
978
- () => ({
979
- archiveThread,
980
- unarchiveThread,
981
- deleteThread,
982
- confirmAndDeleteThread,
983
- settleThread,
984
- unsettleThread,
985
- snoozeThread,
986
- unsnoozeThread,
987
- pinThread,
988
- unpinThread,
989
- confirmAndUnpinThread,
990
- reorderPinnedThread,
991
- reorderActiveThread,
992
- markThreadUnread,
993
- setThreadAutoSettle,
994
- }),
995
- [
996
- archiveThread,
997
- confirmAndDeleteThread,
998
- confirmAndUnpinThread,
999
- deleteThread,
1000
- markThreadUnread,
1001
- pinThread,
1002
- reorderPinnedThread,
1003
- reorderActiveThread,
1004
- setThreadAutoSettle,
1005
- settleThread,
1006
- snoozeThread,
1007
- unarchiveThread,
1008
- unpinThread,
1009
- unsettleThread,
1010
- unsnoozeThread,
1011
- ],
1012
- );
892
+ return deleteThread(target);
893
+ };
894
+
895
+ return {
896
+ archiveThread,
897
+ unarchiveThread,
898
+ deleteThread,
899
+ confirmAndDeleteThread,
900
+ settleThread,
901
+ unsettleThread,
902
+ snoozeThread,
903
+ unsnoozeThread,
904
+ pinThread,
905
+ unpinThread,
906
+ confirmAndUnpinThread,
907
+ reorderPinnedThread,
908
+ reorderActiveThread,
909
+ markThreadUnread,
910
+ setThreadAutoSettle,
911
+ };
912
}