11"use client" ;
22
3- import { useState , useEffect , useCallback , useMemo } from "react" ;
3+ import { useState , useEffect , useCallback , useMemo , useRef } from "react" ;
44import { usePathname , useRouter } from "next/navigation" ;
55import { TooltipProvider } from "@/components/ui/tooltip" ;
66import { NavRail } from "./NavRail" ;
@@ -208,14 +208,15 @@ export function AppShell({ children }: { children: React.ReactNode }) {
208208 } ) ;
209209 } , [ sessionId , sessionTitle , workingDirectory ] ) ;
210210
211+ const pendingNavigateRef = useRef < string | null > ( null ) ;
212+
211213 const removeFromSplit = useCallback ( ( removeId : string ) => {
212214 setSplitSessions ( ( prev ) => {
213215 const next = prev . filter ( ( s ) => s . sessionId !== removeId ) ;
214216 if ( next . length <= 1 ) {
215- // Exit split mode
217+ // Exit split mode — defer navigation to useEffect
216218 if ( next . length === 1 ) {
217- // Navigate to the remaining session
218- router . replace ( `/chat/${ next [ 0 ] . sessionId } ` ) ;
219+ pendingNavigateRef . current = next [ 0 ] . sessionId ;
219220 }
220221 return [ ] ;
221222 }
@@ -225,7 +226,16 @@ export function AppShell({ children }: { children: React.ReactNode }) {
225226 ) ;
226227 return next ;
227228 } ) ;
228- } , [ router ] ) ;
229+ } , [ ] ) ;
230+
231+ // Deferred navigation after split exit (avoids setState-during-render)
232+ useEffect ( ( ) => {
233+ if ( pendingNavigateRef . current ) {
234+ const target = pendingNavigateRef . current ;
235+ pendingNavigateRef . current = null ;
236+ router . replace ( `/chat/${ target } ` ) ;
237+ }
238+ } , [ splitSessions , router ] ) ;
229239
230240 const exitSplit = useCallback ( ( ) => {
231241 const firstSession = splitSessions [ 0 ] ;
0 commit comments