fix(desktop): swap session in-place when switching workDir on empty session

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) <noreply@anthropic.com>
This commit is contained in:
程序员阿江(Relakkes) 2026-04-09 20:17:55 +08:00
parent 211226c7a1
commit 6085447277
2 changed files with 19 additions and 6 deletions

View File

@ -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(() => {})
}}
/>
)}

View File

@ -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<TabStore>((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()