diff --git a/src/renderer/src/components/right-sidebar/SourceControl.virtual-file-list.test.tsx b/src/renderer/src/components/right-sidebar/SourceControl.virtual-file-list.test.tsx index 4d3c172c1ba9..1d49dc920a82 100644 --- a/src/renderer/src/components/right-sidebar/SourceControl.virtual-file-list.test.tsx +++ b/src/renderer/src/components/right-sidebar/SourceControl.virtual-file-list.test.tsx @@ -328,7 +328,7 @@ describe('SourceControl virtualized changed-files list', () => { expect(row('src/file-000.ts')?.className).toContain('bg-accent/60') }) - it('collapses and re-expands a virtualized section', () => { + it('retains a collapsed virtualized section across panel remount', () => { resetState({ gitStatusByWorktree: { [mocks.activeWorktree.id]: manyEntries(500) } }) @@ -346,8 +346,17 @@ describe('SourceControl virtualized changed-files list', () => { expect(mountedRows().length).toBe(0) expect(virtualList()).toBeNull() + act(() => root.unmount()) + root = createRoot(container) + renderSourceControl() + expect(mountedRows().length).toBe(0) + expect(virtualList()).toBeNull() + + const remountedHeader = Array.from(container.querySelectorAll('button')).find((button) => + button.textContent?.includes('Changes') + ) act(() => { - changesHeader?.dispatchEvent(new MouseEvent('click', { bubbles: true })) + remountedHeader?.dispatchEvent(new MouseEvent('click', { bubbles: true })) }) expect(mountedRows().length).toBeGreaterThan(0) expect(mountedRows().length).toBeLessThanOrEqual(MAX_MOUNTED_ROWS) diff --git a/src/renderer/src/components/right-sidebar/source-control/panel/use-panel-view-state.test.tsx b/src/renderer/src/components/right-sidebar/source-control/panel/use-panel-view-state.test.tsx new file mode 100644 index 000000000000..ad3f8ae71118 --- /dev/null +++ b/src/renderer/src/components/right-sidebar/source-control/panel/use-panel-view-state.test.tsx @@ -0,0 +1,63 @@ +// @vitest-environment happy-dom + +import { act } from 'react' +import { createRoot, type Root } from 'react-dom/client' +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' +import { useSourceControlPanelViewState } from './use-panel-view-state' + +let container: HTMLDivElement +let root: Root +let toggle: (section: string) => void = () => {} +let collapsed = new Set() + +function Probe({ worktreeId }: { worktreeId: string | null }) { + const viewState = useSourceControlPanelViewState({ + activeWorktreeId: worktreeId, + settings: null, + updateSettings: vi.fn() + }) + toggle = viewState.toggleSection + collapsed = viewState.collapsedSections + return null +} + +function render(worktreeId: string | null): void { + act(() => root.render()) +} + +function remount(worktreeId: string | null): void { + act(() => root.unmount()) + root = createRoot(container) + render(worktreeId) +} + +beforeEach(() => { + globalThis.IS_REACT_ACT_ENVIRONMENT = true + container = document.createElement('div') + root = createRoot(container) +}) + +afterEach(() => { + act(() => root.unmount()) +}) + +describe('useSourceControlPanelViewState section disclosure', () => { + it('keeps a worktree choice across remount without leaking it to another worktree', () => { + render('wt-a') + act(() => toggle('unstaged')) + act(() => toggle('history')) + expect([...collapsed].sort()).toEqual(['unstaged']) + + remount('wt-a') + expect([...collapsed].sort()).toEqual(['unstaged']) + + render('wt-b') + expect([...collapsed].sort()).toEqual(['history']) + + remount('wt-b') + expect([...collapsed].sort()).toEqual(['history']) + + render('wt-a') + expect([...collapsed].sort()).toEqual(['unstaged']) + }) +}) diff --git a/src/renderer/src/components/right-sidebar/source-control/panel/use-panel-view-state.ts b/src/renderer/src/components/right-sidebar/source-control/panel/use-panel-view-state.ts index 5b46ce01973e..86d25fc7005f 100644 --- a/src/renderer/src/components/right-sidebar/source-control/panel/use-panel-view-state.ts +++ b/src/renderer/src/components/right-sidebar/source-control/panel/use-panel-view-state.ts @@ -1,4 +1,5 @@ import { useCallback, useMemo, useRef, useState } from 'react' +import { BoundedMap } from '../../../../../../shared/bounded-map' import { resolveSourceControlGroupOrder } from '../listing/section-order' import { getNextSourceControlViewMode } from './header-toolbar' import { normalizeSourceControlViewMode } from '../commit/commit-drafts' @@ -11,6 +12,16 @@ function createDefaultCollapsedSections(): Set { return new Set(DEFAULT_COLLAPSED_SECTIONS) } +// Why: the panel unmounts on sidebar tab switch; retain per-worktree disclosure for this renderer session. +const collapsedSectionsByWorktree = new BoundedMap>({ maxEntries: 512 }) + +function readCollapsedSections(worktreeId: string | null): Set { + return ( + (worktreeId ? collapsedSectionsByWorktree.get(worktreeId) : undefined) ?? + createDefaultCollapsedSections() + ) +} + /** * Local presentation state for the Source Control panel: filter, collapsed sections/directories, * list-vs-tree mode and the base-ref dialog, plus the per-worktree reset that keeps a worktree @@ -30,8 +41,8 @@ export function useSourceControlPanelViewState({ const [fileListScrollElement, setFileListScrollElement] = useState(null) const isMac = useMemo(() => navigator.userAgent.includes('Mac'), []) const [filterExpanded, setFilterExpanded] = useState(false) - const [collapsedSections, setCollapsedSections] = useState>( - createDefaultCollapsedSections + const [collapsedSections, setCollapsedSections] = useState>(() => + readCollapsedSections(activeWorktreeId) ) const persistedSourceControlViewMode = normalizeSourceControlViewMode( settings?.sourceControlViewMode @@ -57,7 +68,7 @@ export function useSourceControlPanelViewState({ if (viewStateWorktreeId !== activeWorktreeId) { setViewStateWorktreeId(activeWorktreeId) setFilterExpanded(false) - setCollapsedSections(createDefaultCollapsedSections()) + setCollapsedSections(readCollapsedSections(activeWorktreeId)) setCollapsedTreeDirs(new Set()) setBaseRefDialogOpen(false) // Why: don't reset defaultBaseRef here — it's repo-scoped (resolved on activeRepo change); resetting would clobber non-main defaults. @@ -65,17 +76,21 @@ export function useSourceControlPanelViewState({ // Why: don't reset commit-in-flight state — it's per-worktree; resetting would re-enable Commit for an incoming worktree mid-commit. } - const toggleSection = useCallback((section: string) => { - setCollapsedSections((prev) => { - const next = new Set(prev) + const toggleSection = useCallback( + (section: string) => { + const next = new Set(collapsedSections) if (next.has(section)) { next.delete(section) } else { next.add(section) } - return next - }) - }, []) + setCollapsedSections(next) + if (activeWorktreeId) { + collapsedSectionsByWorktree.set(activeWorktreeId, next) + } + }, + [activeWorktreeId, collapsedSections] + ) const toggleTreeDir = useCallback((key: string) => { setCollapsedTreeDirs((prev) => {