fix: pr-bug-scan validated finding from #2793 (#3024)

Co-authored-by: orca-bug-scan-bot <orca-bug-scan-bot@stably.ai>
Co-authored-by: Neil <4138956+nwparker@users.noreply.github.com>
This commit is contained in:
buf0-bot[bot] 2026-05-29 16:29:40 -07:00 committed by GitHub
parent a1496d54d4
commit 425dee5085
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 41 additions and 0 deletions

View File

@ -8,6 +8,7 @@ import { folderRelativePathToIncludeGlob } from './file-search-include-pattern'
import { ScrollArea } from '@/components/ui/scroll-area'
import { cn } from '@/lib/utils'
import { isGitRepoKind } from '../../../../shared/repo-kind'
import { shouldResetFileExplorerForVisibleWorktree } from './file-explorer-reset'
import { FileExplorerBackgroundMenu } from './FileExplorerBackgroundMenu'
import { FileExplorerToolbar } from './FileExplorerToolbar'
import { FileExplorerTreeStatus } from './FileExplorerTreeStatus'
@ -157,12 +158,22 @@ function FileExplorerInner(): React.JSX.Element {
scrollRef
})
const lastResetWorktreePathRef = useRef<string | null>(null)
useEffect(() => {
if (!visibleWorktreePath) {
return
}
// Why: the sidebar remains mounted while closed to preserve caches, but
// loading the hidden tree would probe every clicked workspace on macOS.
if (
!shouldResetFileExplorerForVisibleWorktree(
lastResetWorktreePathRef.current,
visibleWorktreePath
)
) {
return
}
lastResetWorktreePathRef.current = visibleWorktreePath
resetSelection()
resetAndLoad()
clearFileExplorerUndoHistory()

View File

@ -0,0 +1,24 @@
import { describe, expect, it } from 'vitest'
import { shouldResetFileExplorerForVisibleWorktree } from './file-explorer-reset'
describe('shouldResetFileExplorerForVisibleWorktree', () => {
it('preserves explorer state across hide and reopen of the same worktree', () => {
let lastResetWorktreePath: string | null = null
const shouldReset = (visibleWorktreePath: string | null): boolean => {
if (shouldResetFileExplorerForVisibleWorktree(lastResetWorktreePath, visibleWorktreePath)) {
lastResetWorktreePath = visibleWorktreePath
return true
}
return false
}
expect(shouldReset(null)).toBe(false)
expect(shouldReset('/repo')).toBe(true)
expect(shouldReset(null)).toBe(false)
expect(shouldReset('/repo')).toBe(false)
})
it('resets when the visible worktree path changes', () => {
expect(shouldResetFileExplorerForVisibleWorktree('/repo', '/repo-next')).toBe(true)
})
})

View File

@ -0,0 +1,6 @@
export function shouldResetFileExplorerForVisibleWorktree(
lastResetWorktreePath: string | null,
visibleWorktreePath: string | null
): visibleWorktreePath is string {
return visibleWorktreePath !== null && lastResetWorktreePath !== visibleWorktreePath
}