feat: persist last-active terminal tab per worktree (#392)
When switching between worktrees, the active terminal tab was reset to the first tab because activeTabId was global. This adds a per-worktree map (activeTabIdByWorktree) mirroring the existing pattern used for editor files, so users return to the same terminal tab they left.
This commit is contained in:
@@ -114,6 +114,7 @@ function App(): React.JSX.Element {
|
||||
const openFiles = useAppStore((s) => s.openFiles)
|
||||
const activeFileIdByWorktree = useAppStore((s) => s.activeFileIdByWorktree)
|
||||
const activeTabTypeByWorktree = useAppStore((s) => s.activeTabTypeByWorktree)
|
||||
const activeTabIdByWorktree = useAppStore((s) => s.activeTabIdByWorktree)
|
||||
|
||||
// Right sidebar + editor state
|
||||
const toggleRightSidebar = useAppStore((s) => s.toggleRightSidebar)
|
||||
@@ -240,6 +241,7 @@ function App(): React.JSX.Element {
|
||||
tabsByWorktree,
|
||||
terminalLayoutsByTabId,
|
||||
activeWorktreeIdsOnShutdown,
|
||||
activeTabIdByWorktree,
|
||||
...buildEditorSessionData(openFiles, activeFileIdByWorktree, activeTabTypeByWorktree)
|
||||
})
|
||||
}, 150)
|
||||
@@ -254,7 +256,8 @@ function App(): React.JSX.Element {
|
||||
terminalLayoutsByTabId,
|
||||
openFiles,
|
||||
activeFileIdByWorktree,
|
||||
activeTabTypeByWorktree
|
||||
activeTabTypeByWorktree,
|
||||
activeTabIdByWorktree
|
||||
])
|
||||
|
||||
// On shutdown, capture terminal scrollback buffers and flush to disk.
|
||||
@@ -282,6 +285,7 @@ function App(): React.JSX.Element {
|
||||
tabsByWorktree: state.tabsByWorktree,
|
||||
terminalLayoutsByTabId: state.terminalLayoutsByTabId,
|
||||
activeWorktreeIdsOnShutdown,
|
||||
activeTabIdByWorktree: state.activeTabIdByWorktree,
|
||||
...buildEditorSessionData(
|
||||
state.openFiles,
|
||||
state.activeFileIdByWorktree,
|
||||
|
||||
@@ -17,6 +17,9 @@ import {
|
||||
export type TerminalSlice = {
|
||||
tabsByWorktree: Record<string, TerminalTab[]>
|
||||
activeTabId: string | null
|
||||
/** Per-worktree last-active terminal tab — restored on worktree switch so
|
||||
* the user returns to the same tab they left, not always tabs[0]. */
|
||||
activeTabIdByWorktree: Record<string, string | null>
|
||||
ptyIdsByTabId: Record<string, string[]>
|
||||
suppressedPtyExitIds: Record<string, true>
|
||||
expandedPaneByTabId: Record<string, boolean>
|
||||
@@ -64,6 +67,7 @@ export type TerminalSlice = {
|
||||
export const createTerminalSlice: StateCreator<AppState, [], [], TerminalSlice> = (set, get) => ({
|
||||
tabsByWorktree: {},
|
||||
activeTabId: null,
|
||||
activeTabIdByWorktree: {},
|
||||
ptyIdsByTabId: {},
|
||||
suppressedPtyExitIds: {},
|
||||
expandedPaneByTabId: {},
|
||||
@@ -150,6 +154,7 @@ export const createTerminalSlice: StateCreator<AppState, [], [], TerminalSlice>
|
||||
[worktreeId]: [...existing, tab]
|
||||
},
|
||||
activeTabId: tab.id,
|
||||
activeTabIdByWorktree: { ...s.activeTabIdByWorktree, [worktreeId]: tab.id },
|
||||
ptyIdsByTabId: { ...s.ptyIdsByTabId, [tab.id]: [] },
|
||||
terminalLayoutsByTabId: { ...s.terminalLayoutsByTabId, [tab.id]: emptyLayoutSnapshot() }
|
||||
}
|
||||
@@ -185,9 +190,20 @@ export const createTerminalSlice: StateCreator<AppState, [], [], TerminalSlice>
|
||||
delete nextCacheTimer[key]
|
||||
}
|
||||
}
|
||||
// Why: keep activeTabIdByWorktree in sync when a tab is closed in a
|
||||
// background worktree. Without this, the remembered tab becomes stale
|
||||
// and restoring it on worktree switch falls back to tabs[0].
|
||||
const nextActiveTabIdByWorktree = { ...s.activeTabIdByWorktree }
|
||||
for (const [wId, tabs] of Object.entries(next)) {
|
||||
if (nextActiveTabIdByWorktree[wId] === tabId) {
|
||||
nextActiveTabIdByWorktree[wId] = tabs[0]?.id ?? null
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
tabsByWorktree: next,
|
||||
activeTabId: s.activeTabId === tabId ? null : s.activeTabId,
|
||||
activeTabIdByWorktree: nextActiveTabIdByWorktree,
|
||||
ptyIdsByTabId: nextPtyIdsByTabId,
|
||||
expandedPaneByTabId: nextExpanded,
|
||||
canExpandPaneByTabId: nextCanExpand,
|
||||
@@ -244,7 +260,16 @@ export const createTerminalSlice: StateCreator<AppState, [], [], TerminalSlice>
|
||||
})
|
||||
},
|
||||
|
||||
setActiveTab: (tabId) => set({ activeTabId: tabId }),
|
||||
setActiveTab: (tabId) =>
|
||||
set((s) => {
|
||||
const worktreeId = s.activeWorktreeId
|
||||
return {
|
||||
activeTabId: tabId,
|
||||
activeTabIdByWorktree: worktreeId
|
||||
? { ...s.activeTabIdByWorktree, [worktreeId]: tabId }
|
||||
: s.activeTabIdByWorktree
|
||||
}
|
||||
}),
|
||||
|
||||
updateTabTitle: (tabId, title) => {
|
||||
set((s) => {
|
||||
@@ -531,10 +556,34 @@ export const createTerminalSlice: StateCreator<AppState, [], [], TerminalSlice>
|
||||
}
|
||||
}
|
||||
|
||||
// Why: restore per-worktree active terminal tab from session.
|
||||
// If the session has the map, validate that each tab ID still exists.
|
||||
// Otherwise, derive it: the active worktree gets activeTabId, others
|
||||
// default to their first tab.
|
||||
let activeTabIdByWorktree: Record<string, string | null> = {}
|
||||
if (session.activeTabIdByWorktree) {
|
||||
for (const [wId, tabId] of Object.entries(session.activeTabIdByWorktree)) {
|
||||
if (validWorktreeIds.has(wId) && tabId && validTabIds.has(tabId)) {
|
||||
activeTabIdByWorktree[wId] = tabId
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Legacy sessions: best-effort derivation
|
||||
if (activeWorktreeId && activeTabId) {
|
||||
activeTabIdByWorktree[activeWorktreeId] = activeTabId
|
||||
}
|
||||
for (const [wId, tabs] of Object.entries(tabsByWorktree)) {
|
||||
if (!activeTabIdByWorktree[wId] && tabs.length > 0) {
|
||||
activeTabIdByWorktree[wId] = tabs[0].id
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
activeRepoId,
|
||||
activeWorktreeId,
|
||||
activeTabId,
|
||||
activeTabIdByWorktree,
|
||||
tabsByWorktree,
|
||||
pendingReconnectWorktreeIds,
|
||||
pendingReconnectTabByWorktree,
|
||||
|
||||
@@ -301,10 +301,20 @@ export const createWorktreeSlice: StateCreator<AppState, [], [], WorktreeSlice>
|
||||
activeTabType = fallbackFile ? 'editor' : 'terminal'
|
||||
}
|
||||
|
||||
// Why: restore the last-active terminal tab for this worktree so the
|
||||
// user returns to the same tab they left, not always the first one.
|
||||
const restoredTabId = s.activeTabIdByWorktree[worktreeId] ?? null
|
||||
const worktreeTabs = s.tabsByWorktree[worktreeId] ?? []
|
||||
const tabStillExists = restoredTabId
|
||||
? worktreeTabs.some((t) => t.id === restoredTabId)
|
||||
: false
|
||||
const activeTabId = tabStillExists ? restoredTabId : (worktreeTabs[0]?.id ?? null)
|
||||
|
||||
return {
|
||||
activeWorktreeId: worktreeId,
|
||||
activeFileId,
|
||||
activeTabType,
|
||||
activeTabId,
|
||||
worktreesByRepo: applyWorktreeUpdates(
|
||||
s.worktreesByRepo,
|
||||
worktreeId,
|
||||
|
||||
@@ -149,6 +149,8 @@ export type WorkspaceSessionState = {
|
||||
activeFileIdByWorktree?: Record<string, string | null>
|
||||
/** Per-worktree active tab type (terminal vs editor) at shutdown. */
|
||||
activeTabTypeByWorktree?: Record<string, 'terminal' | 'editor'>
|
||||
/** Per-worktree last-active terminal tab ID at shutdown. */
|
||||
activeTabIdByWorktree?: Record<string, string | null>
|
||||
/** Unified tab model — present when saved by a build that includes TabsSlice.
|
||||
* Read-path checks for this first; falls back to legacy fields if absent. */
|
||||
unifiedTabs?: Record<string, Tab[]>
|
||||
|
||||
Reference in New Issue
Block a user