@@ -76,7 +76,7 @@ import { threadEnvironment } from "../state/threads";
7676import { projectEnvironment } from "../state/projects" ;
7777import { useEnvironmentQuery } from "../state/query" ;
7878import { useAtomCommand } from "../state/use-atom-command" ;
79- import { buildThreadRouteParams , resolveThreadRouteRef } from "../threadRoutes" ;
79+ import { buildThreadRouteParams , resolveThreadRouteTarget } from "../threadRoutes" ;
8080import { formatRelativeTimeLabel } from "../timestampFormat" ;
8181import type { SidebarThreadSummary } from "../types" ;
8282import { cn } from "~/lib/utils" ;
@@ -86,6 +86,7 @@ import {
8686 isTrailingDoubleClick ,
8787 resolveAdjacentThreadId ,
8888 resolveSidebarV2Status ,
89+ shouldNavigateAfterProjectRemoval ,
8990 sortThreadsForSidebarV2 ,
9091} from "./Sidebar.logic" ;
9192import { resolveLocalCheckoutBranchMismatch } from "./BranchToolbar.logic" ;
@@ -740,11 +741,14 @@ export default function SidebarV2() {
740741 const toggleThreadSelection = useThreadSelectionStore ( ( s ) => s . toggleThread ) ;
741742 const rangeSelectTo = useThreadSelectionStore ( ( s ) => s . rangeSelectTo ) ;
742743 const markThreadUnread = useUiStateStore ( ( s ) => s . markThreadUnread ) ;
743- const routeThreadRef = useParams ( {
744+ const routeTarget = useParams ( {
744745 strict : false ,
745- select : ( params ) => resolveThreadRouteRef ( params ) ,
746+ select : ( params ) => resolveThreadRouteTarget ( params ) ,
746747 } ) ;
748+ const routeThreadRef = routeTarget ?. kind === "server" ? routeTarget . threadRef : null ;
747749 const routeThreadKey = routeThreadRef ? scopedThreadKey ( routeThreadRef ) : null ;
750+ const routeTargetRef = useRef ( routeTarget ) ;
751+ routeTargetRef . current = routeTarget ;
748752 // Post-settle navigation validates against the CURRENT route, not the one
749753 // captured when the settle started: if the user navigated elsewhere while
750754 // the command was in flight, completing it must not yank them away.
@@ -894,23 +898,21 @@ export default function SidebarV2() {
894898
895899 const draftStore = useComposerDraftStore . getState ( ) ;
896900 const projectDraftThread = draftStore . getDraftThreadByProjectRef ( projectRef ) ;
901+ const shouldNavigate = shouldNavigateAfterProjectRemoval ( {
902+ routeTarget : routeTargetRef . current ,
903+ projectThreads,
904+ projectDraftId : projectDraftThread ?. draftId ?? null ,
905+ } ) ;
897906 if ( projectDraftThread ) {
898907 draftStore . clearDraftThread ( projectDraftThread . draftId ) ;
899908 }
900909 draftStore . clearProjectDraftThreadId ( projectRef ) ;
901910
902- if (
903- routeThreadRef &&
904- projectThreads . some (
905- ( thread ) =>
906- thread . environmentId === routeThreadRef . environmentId &&
907- thread . id === routeThreadRef . threadId ,
908- )
909- ) {
911+ if ( shouldNavigate ) {
910912 void router . navigate ( { to : "/" } ) ;
911913 }
912914 } ,
913- [ deleteProject , routeThreadRef , router , threads ] ,
915+ [ deleteProject , router , threads ] ,
914916 ) ;
915917
916918 // Settled threads stay in the live shell stream (settled ≠ archived), so
0 commit comments