From 6085447277fa5f90512f7af5834944b70e62a757 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E5=91=98=E9=98=BF=E6=B1=9F=28Relakkes?= =?UTF-8?q?=29?= Date: Thu, 9 Apr 2026 20:17:55 +0800 Subject: [PATCH] fix(desktop): swap session in-place when switching workDir on empty session MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Instead of close tab → delete session → create session → open tab (which causes visual flicker and tab jumping), create the new session first then replace the tab's sessionId in-place via a new replaceTabSession method. Co-Authored-By: Claude Opus 4.6 (1M context) --- desktop/src/components/chat/ChatInput.tsx | 13 +++++++------ desktop/src/stores/tabStore.ts | 12 ++++++++++++ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/desktop/src/components/chat/ChatInput.tsx b/desktop/src/components/chat/ChatInput.tsx index cf64ccf5..ac5da05c 100644 --- a/desktop/src/components/chat/ChatInput.tsx +++ b/desktop/src/components/chat/ChatInput.tsx @@ -531,16 +531,17 @@ export function ChatInput() { value={gitInfo?.workDir || activeSession?.workDir || ''} onChange={async (newWorkDir) => { if (!activeTabId) return - // Recreate session with new workDir + const oldId = activeTabId const { deleteSession, createSession } = useSessionStore.getState() - const { closeTab, openTab } = useTabStore.getState() + const { replaceTabSession } = useTabStore.getState() const { disconnectSession, connectToSession } = useChatStore.getState() - disconnectSession(activeTabId) - closeTab(activeTabId) - await deleteSession(activeTabId) + // Create new session first, then swap tab in-place const newId = await createSession(newWorkDir) - openTab(newId, t('sidebar.newSession')) + disconnectSession(oldId) + replaceTabSession(oldId, newId) connectToSession(newId) + // Clean up old session in background + deleteSession(oldId).catch(() => {}) }} /> )} diff --git a/desktop/src/stores/tabStore.ts b/desktop/src/stores/tabStore.ts index d2a12c8b..4fd0a564 100644 --- a/desktop/src/stores/tabStore.ts +++ b/desktop/src/stores/tabStore.ts @@ -29,6 +29,7 @@ type TabStore = { setActiveTab: (sessionId: string) => void updateTabTitle: (sessionId: string, title: string) => void updateTabStatus: (sessionId: string, status: Tab['status']) => void + replaceTabSession: (oldSessionId: string, newSessionId: string) => void moveTab: (fromIndex: number, toIndex: number) => void saveTabs: () => void @@ -93,6 +94,17 @@ export const useTabStore = create((set, get) => ({ })) }, + replaceTabSession: (oldSessionId, newSessionId) => { + const { activeTabId } = get() + set((s) => ({ + tabs: s.tabs.map((t) => + t.sessionId === oldSessionId ? { ...t, sessionId: newSessionId } : t, + ), + activeTabId: activeTabId === oldSessionId ? newSessionId : activeTabId, + })) + get().saveTabs() + }, + moveTab: (fromIndex, toIndex) => { if (fromIndex === toIndex) return const { tabs } = get()