From 6fa075de51a294453848f4ac7287c53d1a7ad04d Mon Sep 17 00:00:00 2001 From: iliya Date: Wed, 25 Mar 2026 13:58:48 +0200 Subject: [PATCH] fix: sidebar header repo/branch not syncing when switching tabs Cherry-picked from upstream d2341d50 --- src/renderer/store/slices/tabSlice.ts | 2 +- test/renderer/store/tabSlice.test.ts | 72 +++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 1 deletion(-) diff --git a/src/renderer/store/slices/tabSlice.ts b/src/renderer/store/slices/tabSlice.ts index 7a4be286..4fc1e8c4 100644 --- a/src/renderer/store/slices/tabSlice.ts +++ b/src/renderer/store/slices/tabSlice.ts @@ -292,7 +292,7 @@ export const createTabSlice: StateCreator = (set, ge for (const repo of state.repositoryGroups) { for (const wt of repo.worktrees) { - if (wt.sessions.includes(sessionId)) { + if (wt.id === projectId) { foundRepo = repo.id; foundWorktree = wt.id; break; diff --git a/test/renderer/store/tabSlice.test.ts b/test/renderer/store/tabSlice.test.ts index 582006c3..b1090044 100644 --- a/test/renderer/store/tabSlice.test.ts +++ b/test/renderer/store/tabSlice.test.ts @@ -234,6 +234,78 @@ describe('tabSlice', () => { expect(store.getState().activeTabId).toBe(tab1Id); }); + it('should sync selectedRepositoryId and selectedWorktreeId when switching tabs across repos', () => { + // Setup repositoryGroups with two repos, each with one worktree + store.setState({ + repositoryGroups: [ + { + id: 'repo-A', + identity: null, + name: 'Repo A', + worktrees: [ + { + id: 'worktree-A', + path: '/path/a', + name: 'main', + isMainWorktree: true, + source: 'git', + sessions: [], + createdAt: 0, + }, + ], + totalSessions: 0, + }, + { + id: 'repo-B', + identity: null, + name: 'Repo B', + worktrees: [ + { + id: 'worktree-B', + path: '/path/b', + name: 'develop', + isMainWorktree: true, + source: 'git', + sessions: [], + createdAt: 0, + }, + ], + totalSessions: 0, + }, + ] as never[], + selectedRepositoryId: 'repo-A', + selectedWorktreeId: 'worktree-A', + }); + + // Open tab from repo A + store.getState().openTab({ + type: 'session', + sessionId: 'session-A', + projectId: 'worktree-A', + label: 'Session A', + }); + const tabAId = store.getState().activeTabId; + + // Open tab from repo B + store.getState().openTab({ + type: 'session', + sessionId: 'session-B', + projectId: 'worktree-B', + label: 'Session B', + }); + + // Switch back to tab A + store.getState().setActiveTab(tabAId!); + expect(store.getState().selectedRepositoryId).toBe('repo-A'); + expect(store.getState().selectedWorktreeId).toBe('worktree-A'); + + // Switch to tab B + const tabBId = store.getState().openTabs.find((t) => t.sessionId === 'session-B')?.id; + store.getState().setActiveTab(tabBId!); + expect(store.getState().selectedRepositoryId).toBe('repo-B'); + expect(store.getState().selectedWorktreeId).toBe('worktree-B'); + }); + it('should preserve sidebar state for non-session tabs', () => { // Setup initial state with projects data so setActiveTab can find the project store.setState({