diff --git a/ui/desktop/src/hooks/useNavigationSessions.ts b/ui/desktop/src/hooks/useNavigationSessions.ts index be99d1a6..2ce10b1a 100644 --- a/ui/desktop/src/hooks/useNavigationSessions.ts +++ b/ui/desktop/src/hooks/useNavigationSessions.ts @@ -138,6 +138,39 @@ export function useNavigationSessions(options: UseNavigationSessionsOptions = {} }; }, []); + useEffect(() => { + const handleSessionDeleted = (event: Event) => { + const { sessionId } = (event as CustomEvent<{ sessionId: string }>).detail; + + setRecentSessions((prev) => prev.filter((session) => session.id !== sessionId)); + sessionsRef.current = sessionsRef.current.filter((session) => session.id !== sessionId); + + if (lastSessionIdRef.current === sessionId) { + lastSessionIdRef.current = null; + } + }; + + const handleSessionRenamed = (event: Event) => { + const { sessionId, newName } = + (event as CustomEvent<{ sessionId: string; newName: string }>).detail; + + setRecentSessions((prev) => + prev.map((session) => (session.id === sessionId ? { ...session, name: newName } : session)) + ); + sessionsRef.current = sessionsRef.current.map((session) => + session.id === sessionId ? { ...session, name: newName } : session + ); + }; + + window.addEventListener(AppEvents.SESSION_DELETED, handleSessionDeleted); + window.addEventListener(AppEvents.SESSION_RENAMED, handleSessionRenamed); + + return () => { + window.removeEventListener(AppEvents.SESSION_DELETED, handleSessionDeleted); + window.removeEventListener(AppEvents.SESSION_RENAMED, handleSessionRenamed); + }; + }, []); + const handleNavClick = useCallback( (path: string) => { if (path === '/pair') {