@@ -144,6 +144,21 @@ export async function requestThreadUnpinConfirmation(input: {
144144 ) ;
145145}
146146
147+ /** Report navigation separately so a completed deletion can still finish worktree cleanup. */
148+ export async function navigateAfterThreadDeletion ( navigate : ( ) => Promise < void > ) {
149+ const result = await settlePromise ( navigate ) ;
150+ if ( result . _tag === "Failure" ) {
151+ const error = squashAtomCommandFailure ( result ) ;
152+ toastManager . add (
153+ stackedThreadToast ( {
154+ type : "error" ,
155+ title : "Thread deleted, but navigation failed" ,
156+ description : error instanceof Error ? error . message : "An error occurred." ,
157+ } ) ,
158+ ) ;
159+ }
160+ }
161+
147162export function useThreadActions ( ) {
148163 const closeTerminal = useAtomCommand ( terminalEnvironment . close ) ;
149164 const archiveThreadMutation = useAtomCommand ( threadEnvironment . archive , {
@@ -383,39 +398,20 @@ export function useThreadActions() {
383398 clearTerminalUiState ( threadRef ) ;
384399
385400 if ( shouldNavigateToFallback ) {
386- if ( fallbackThreadId ) {
387- const fallbackThread = readThreadShell (
388- scopeThreadRef ( threadRef . environmentId , fallbackThreadId ) ,
389- ) ;
390- if ( fallbackThread ) {
391- const navigationResult = await settlePromise ( ( ) =>
392- router . navigate ( {
401+ const fallbackThread = fallbackThreadId
402+ ? readThreadShell ( scopeThreadRef ( threadRef . environmentId , fallbackThreadId ) )
403+ : null ;
404+ await navigateAfterThreadDeletion ( ( ) =>
405+ fallbackThread
406+ ? router . navigate ( {
393407 to : "/$environmentId/$threadId" ,
394408 params : buildThreadRouteParams (
395409 scopeThreadRef ( fallbackThread . environmentId , fallbackThread . id ) ,
396410 ) ,
397411 replace : true ,
398- } ) ,
399- ) ;
400- if ( navigationResult . _tag === "Failure" ) {
401- return navigationResult ;
402- }
403- } else {
404- const navigationResult = await settlePromise ( ( ) =>
405- router . navigate ( { to : "/" , replace : true } ) ,
406- ) ;
407- if ( navigationResult . _tag === "Failure" ) {
408- return navigationResult ;
409- }
410- }
411- } else {
412- const navigationResult = await settlePromise ( ( ) =>
413- router . navigate ( { to : "/" , replace : true } ) ,
414- ) ;
415- if ( navigationResult . _tag === "Failure" ) {
416- return navigationResult ;
417- }
418- }
412+ } )
413+ : router . navigate ( { to : "/" , replace : true } ) ,
414+ ) ;
419415 }
420416
421417 if ( ! shouldDeleteWorktree || ! orphanedWorktreePath || ! threadProject ) {
@@ -444,9 +440,10 @@ export function useThreadActions() {
444440 ? refreshResult
445441 : null ;
446442 if ( cleanupFailure ) {
443+ const removalFailed = removeResult . _tag === "Failure" ;
447444 const error = squashAtomCommandFailure ( cleanupFailure ) ;
448- const message = error instanceof Error ? error . message : "Unknown error removing worktree ." ;
449- console . error ( "Failed to remove orphaned worktree after thread deletion" , {
445+ const message = error instanceof Error ? error . message : "An error occurred ." ;
446+ console . error ( "Worktree cleanup failed after thread deletion" , {
450447 threadId : threadRef . threadId ,
451448 projectCwd : threadProject . workspaceRoot ,
452449 worktreePath : orphanedWorktreePath ,
@@ -455,11 +452,16 @@ export function useThreadActions() {
455452 toastManager . add (
456453 stackedThreadToast ( {
457454 type : "error" ,
458- title : "Thread deleted, but worktree removal failed" ,
459- description : `Could not remove ${ displayWorktreePath ?? orphanedWorktreePath } . ${ message } ` ,
455+ title : removalFailed
456+ ? "Failed to delete worktree"
457+ : "Worktree deleted, but Git status refresh failed" ,
458+ description : removalFailed
459+ ? `Could not remove ${ displayWorktreePath ?? orphanedWorktreePath } . ${ message } `
460+ : message ,
460461 } ) ,
461462 ) ;
462- return cleanupFailure ;
463+ // The thread was deleted. Cleanup has its own toast; returning its
464+ // failure would make callers incorrectly report a thread deletion error.
463465 }
464466 return deleteResult ;
465467 } ,
0 commit comments