fix(file-explorer): allow filtered folder toggles (#5977)

* fix(file-explorer): allow filtered folder toggles

* fix(file-explorer): keep filtered collapse while typing

* fix filtered explorer drag expansion

Co-authored-by: Orca <help@stably.ai>

---------

Co-authored-by: Jinwoo-H <jinwoo0825@gmail.com>
Co-authored-by: Orca <help@stably.ai>
This commit is contained in:
Ben Newell
2026-06-22 14:04:51 -07:00
committed by GitHub
co-authored by Orca Jinwoo-H
parent 63d1e2421d
commit 10789669ec
6 changed files with 144 additions and 20 deletions
@@ -7,6 +7,10 @@ import { WorktreeOpenInMenuItems } from '@/components/sidebar/WorktreeOpenInMenu
import { FileExplorerToolbar } from './FileExplorerToolbar'
import { FileExplorerNameFilter } from './FileExplorerNameFilter'
import { FileExplorerViewSwitch } from './FileExplorerViewSwitch'
import {
getNameFilterCollapsedPathsAfterExpand,
getNextNameFilterCollapsedPaths
} from './file-explorer-name-filter-projection'
import {
downloadRemoteFile,
FileExplorerRow,
@@ -281,6 +285,25 @@ beforeEach(() => {
delete (globalThis as { __ORCA_WEB_CLIENT__?: boolean }).__ORCA_WEB_CLIENT__
})
describe('getNextNameFilterCollapsedPaths', () => {
it('collapses expanded filtered folders and expands collapsed filtered folders', () => {
const collapsed = getNextNameFilterCollapsedPaths(new Set(), '/repo/src', true)
expect([...collapsed]).toEqual(['/repo/src'])
const expanded = getNextNameFilterCollapsedPaths(collapsed, '/repo/src', false)
expect([...expanded]).toEqual([])
})
it('expands filtered folders without toggling unrelated collapsed paths', () => {
const expanded = getNameFilterCollapsedPathsAfterExpand(
new Set(['/repo/docs', '/repo/src']),
'/repo/src'
)
expect([...expanded]).toEqual(['/repo/docs'])
})
})
describe('FileExplorerToolbar', () => {
it('fires the refresh action from the icon button', () => {
const onRefresh = vi.fn()
@@ -23,7 +23,11 @@ import { SearchResultsPane } from './SearchResultsPane'
import { useFileSearchPanel } from './useFileSearchPanel'
import { FileExplorerTreeStatus } from './FileExplorerTreeStatus'
import { FileExplorerVirtualRows } from './FileExplorerVirtualRows'
import { isFileExplorerNameFilterQueryTooLarge } from './file-explorer-name-filter-projection'
import {
getNameFilterCollapsedPathsAfterExpand,
getNextNameFilterCollapsedPaths,
isFileExplorerNameFilterQueryTooLarge
} from './file-explorer-name-filter-projection'
import { splitPathSegments } from './path-tree'
import { buildFolderStatusMap, buildStatusMap } from './status-display'
import { useFileDeletion } from './useFileDeletion'
@@ -55,6 +59,9 @@ function FileExplorerFiles(): React.JSX.Element {
const showRightSidebarFiles = useAppStore((s) => s.showRightSidebarFiles)
const showRightSidebarSearch = useAppStore((s) => s.showRightSidebarSearch)
const [nameFilterQuery, setNameFilterQuery] = useState('')
const [nameFilterCollapsedPaths, setNameFilterCollapsedPaths] = useState<Set<string>>(
() => new Set()
)
const searchPanel = useFileSearchPanel(explorerView)
const handleSelectExplorerView = useCallback(
@@ -125,6 +132,11 @@ function FileExplorerFiles(): React.JSX.Element {
[nameFilterQuery]
)
const hasNameFilter = isFilesViewActive && hasNameFilterQuery
useEffect(() => {
if (!hasNameFilter) {
setNameFilterCollapsedPaths((current) => (current.size > 0 ? new Set() : current))
}
}, [hasNameFilter])
const nameFilterFiles = useRuntimeFileListForWorktree({
enabled: hasNameFilter && !nameFilterQueryTooLarge,
worktreeId: activeWorktreeId
@@ -162,14 +174,17 @@ function FileExplorerFiles(): React.JSX.Element {
expanded,
activeRepoSupportsGit && isFilesViewActive,
showDotfiles,
nameFilterSource
nameFilterSource,
hasNameFilter ? nameFilterCollapsedPaths : null
)
const rowExpandedPaths = useMemo(
() =>
nameFilterExpandedPaths.size > 0
? new Set([...expanded, ...nameFilterExpandedPaths])
: expanded,
[expanded, nameFilterExpandedPaths]
hasNameFilter
? nameFilterExpandedPaths
: nameFilterExpandedPaths.size > 0
? new Set([...expanded, ...nameFilterExpandedPaths])
: expanded,
[expanded, hasNameFilter, nameFilterExpandedPaths]
)
const visibleRowCount = rowProjection.getVisibleCount()
const manualRefresh = useFileExplorerManualRefresh(refreshTree)
@@ -433,12 +448,24 @@ function FileExplorerFiles(): React.JSX.Element {
() => rowProjection.getRowsByPaths(selectedPaths),
[rowProjection, selectedPaths]
)
const handleToggleNameFilterDir = useCallback(
(_worktreeId: string, dirPath: string) => {
setNameFilterCollapsedPaths((current) =>
getNextNameFilterCollapsedPaths(current, dirPath, rowExpandedPaths.has(dirPath))
)
},
[rowExpandedPaths]
)
const handleExpandNameFilterDir = useCallback((dirPath: string) => {
setNameFilterCollapsedPaths((current) =>
getNameFilterCollapsedPathsAfterExpand(current, dirPath)
)
}, [])
const { handleClick, handleDoubleClick, handleWheelCapture } = useFileExplorerHandlers({
activeWorktreeId,
openFile,
makePreviewFilePermanent,
toggleDir,
canToggleDirectories: !hasNameFilter,
toggleDir: hasNameFilter ? handleToggleNameFilterDir : toggleDir,
loadDir,
statPath,
markPathAsDirectory,
@@ -466,13 +493,13 @@ function FileExplorerFiles(): React.JSX.Element {
containerRef: explorerShellRef,
rowProjection,
expandedPaths: rowExpandedPaths,
canToggleDirectories: !hasNameFilter,
canToggleDirectories: true,
inlineInput,
selectedPaths,
selectedNode,
activateNode,
moveSelection,
toggleDir,
toggleDir: hasNameFilter ? handleToggleNameFilterDir : toggleDir,
startRename,
requestDelete,
requestDeleteAll,
@@ -704,9 +731,11 @@ function FileExplorerFiles(): React.JSX.Element {
onMoveDrop={handleMoveDrop}
onDragTargetChange={setDropTargetDir}
onDragSourceChange={setDragSourcePath}
onDragExpandDir={handleDragExpandDir}
onDragExpandDir={hasNameFilter ? handleExpandNameFilterDir : handleDragExpandDir}
onNativeDragTargetChange={setNativeDropTargetDir}
onNativeDragExpandDir={handleNativeDragExpandDir}
onNativeDragExpandDir={
hasNameFilter ? handleExpandNameFilterDir : handleNativeDragExpandDir
}
dropTargetDir={dropTargetDir}
dragSourcePath={dragSourcePath}
nativeDropTargetDir={nativeDropTargetDir}
@@ -158,8 +158,8 @@ export function applyFileExplorerNavigation(ctx: NavigationContext, e: KeyboardE
if (resolved.type === 'toggle-expand' || resolved.type === 'toggle-collapse') {
e.preventDefault()
e.stopPropagation()
// Why: name-filtered rows are a projection, so toggles should not mutate
// persisted expansion state while filtering is suppressing directory toggles.
// Why: callers can disable directory toggles for projected or transient trees
// where mutating persisted expansion state would be misleading.
if (ctx.activeWorktreeId && ctx.canToggleDirectories !== false) {
ctx.handlers.toggleDir(ctx.activeWorktreeId, resolved.dirPath)
}
@@ -16,6 +16,32 @@ export type FileExplorerNameFilterProjectionSource = {
export const FILE_EXPLORER_NAME_FILTER_QUERY_MAX_BYTES = 2 * 1024
export function getNextNameFilterCollapsedPaths(
collapsedPaths: ReadonlySet<string>,
dirPath: string,
isExpanded: boolean
): Set<string> {
const next = new Set(collapsedPaths)
if (isExpanded) {
next.add(dirPath)
} else {
next.delete(dirPath)
}
return next
}
export function getNameFilterCollapsedPathsAfterExpand(
collapsedPaths: ReadonlySet<string>,
dirPath: string
): Set<string> {
if (!collapsedPaths.has(dirPath)) {
return new Set(collapsedPaths)
}
const next = new Set(collapsedPaths)
next.delete(dirPath)
return next
}
export function isFileExplorerNameFilterQueryTooLarge(
query: string | undefined,
maxBytes = FILE_EXPLORER_NAME_FILTER_QUERY_MAX_BYTES
@@ -119,12 +145,14 @@ function createSyntheticNode(
}
export function createNameFilteredFileExplorerProjection({
collapsedPaths,
ignoredSet,
nameFilter,
showDotfiles,
showGitIgnoredFiles,
worktreePath
}: {
collapsedPaths?: ReadonlySet<string>
ignoredSet: Set<string>
nameFilter: FileExplorerNameFilterProjectionSource
showDotfiles: boolean
@@ -180,14 +208,15 @@ export function createNameFilteredFileExplorerProjection({
}
}
appendNameFilteredEntries(rootChildren.values(), visibleFlatRows, rowsByPath)
appendNameFilteredEntries(rootChildren.values(), visibleFlatRows, rowsByPath, collapsedPaths)
return createFileExplorerRowProjectionFromParts(visibleFlatRows, rowsByPath)
}
function appendNameFilteredEntries(
entries: Iterable<SyntheticTreeEntry>,
visibleFlatRows: TreeNode[],
rowsByPath: Map<string, TreeNode>
rowsByPath: Map<string, TreeNode>,
collapsedPaths?: ReadonlySet<string>
): void {
const sortedEntries = Array.from(entries).sort((a, b) => {
if (a.node.isDirectory !== b.node.isDirectory) {
@@ -198,8 +227,13 @@ function appendNameFilteredEntries(
for (const entry of sortedEntries) {
visibleFlatRows.push(entry.node)
rowsByPath.set(entry.node.path, entry.node)
if (entry.children.size > 0) {
appendNameFilteredEntries(entry.children.values(), visibleFlatRows, rowsByPath)
if (entry.children.size > 0 && !collapsedPaths?.has(entry.node.path)) {
appendNameFilteredEntries(
entry.children.values(),
visibleFlatRows,
rowsByPath,
collapsedPaths
)
}
}
}
@@ -168,6 +168,31 @@ describe('file explorer visible row projection', () => {
])
})
it('hides descendants under collapsed folders while a file-name filter is active', () => {
const projection = createVisibleFileExplorerRowProjection(
input({
'/repo': [row('docs', true, 0), row('src', true, 0)]
}),
{
ignoredSet: new Set(),
nameFilter: {
query: 'ts',
relativePaths: ['docs/guide.ts', 'src/components/FileExplorer.tsx', 'src/index.ts']
},
nameFilterCollapsedPaths: new Set(['/repo/src']),
showDotfiles: true,
showGitIgnoredFiles: true
}
)
expect(projection.getVisibleSlice(0, 10).map((entry) => entry.relativePath)).toEqual([
'docs',
'docs/guide.ts',
'src'
])
expect([...getFileExplorerNameFilterExpandedPaths(projection, 'ts')]).toEqual(['/repo/docs'])
})
it('does not fall back to the partial cached tree while recursive file filtering is loading', () => {
const projection = createVisibleFileExplorerRowProjection(
input(
@@ -30,6 +30,7 @@ export type IgnoredPathResult = {
type VisibleFileExplorerRowProjectionOptions = {
ignoredSet: Set<string>
nameFilter?: FileExplorerNameFilterProjectionSource | null
nameFilterCollapsedPaths?: ReadonlySet<string> | null
showDotfiles: boolean
showGitIgnoredFiles: boolean
}
@@ -81,6 +82,7 @@ export function createVisibleFileExplorerRowProjection(
}
if (options.nameFilter) {
return createNameFilteredFileExplorerProjection({
collapsedPaths: options.nameFilterCollapsedPaths ?? undefined,
ignoredSet: options.ignoredSet,
nameFilter: options.nameFilter,
showDotfiles: options.showDotfiles,
@@ -149,7 +151,8 @@ export function useFileExplorerVisibleRowProjection(
expanded: Set<string>,
activeRepoSupportsGit: boolean,
showDotfiles: boolean,
nameFilter: FileExplorerNameFilterProjectionSource | null
nameFilter: FileExplorerNameFilterProjectionSource | null,
nameFilterCollapsedPaths: ReadonlySet<string> | null = null
): {
rowProjection: FileExplorerRowProjection
ignoredByRelativePath: Set<string>
@@ -235,11 +238,21 @@ export function useFileExplorerVisibleRowProjection(
{
ignoredSet,
nameFilter,
nameFilterCollapsedPaths,
showDotfiles,
showGitIgnoredFiles
}
),
[dirCache, expanded, ignoredSet, nameFilter, showDotfiles, showGitIgnoredFiles, worktreePath]
[
dirCache,
expanded,
ignoredSet,
nameFilter,
nameFilterCollapsedPaths,
showDotfiles,
showGitIgnoredFiles,
worktreePath
]
)
const nameFilterExpandedPaths = useMemo(
() => getFileExplorerNameFilterExpandedPaths(rowProjection, nameFilter?.query ?? ''),