File Browser: add filter input and collapse-all button
Some checks are pending
CodeQL / Analyze (push) Waiting to run
Controller Tests / controller-tests (push) Waiting to run
Jest Tests / jest-tests (push) Waiting to run

Add a client-side filter above the File Browser table. Typing reduces
the visible rows to files/directories whose name matches, plus their
parent folders. A folder whose own name matches keeps its entire
subtree, and opening a matching folder shows its contents unfiltered.

* 'f' focuses the filter input, like the search inputs on other pages.
* An attached X button clears the filter; disabled while empty.
* A collapse-all (angle double up) button closes all open folders,
  clearing the ?folders= query param and deselecting hidden children.
* The filter row is right-aligned and double-width on desktop, and
  fills a single row on mobile.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Roland 2026-07-03 11:17:11 -06:00
parent 2162396212
commit 8044fd1240
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
3 changed files with 396 additions and 3 deletions

View file

@ -24,6 +24,7 @@ import {
} from "./Common";
import React, {useContext, useEffect, useState} from "react";
import {useDropzone} from "react-dropzone";
import {useHotkeys} from "react-hotkeys-hook";
import {deleteFile, ignoreDirectory, makeDirectory, movePaths, renamePath, unignoreDirectory} from "../api";
import _ from 'lodash';
import {SortableTable} from "./SortableTable";
@ -67,6 +68,32 @@ export function splitPathParentAndName(path) {
return [path.substring(0, slashIndex), path.substring(slashIndex + 1, path.length)];
}
export function filterBrowseFiles(browseFiles, filterStr) {
// Reduce the file tree to the rows whose name matches `filterStr`, keeping the ancestor
// folders of every match. A folder whose own name matches keeps its entire subtree.
if (!filterStr) {
return browseFiles;
}
const needle = filterStr.toLowerCase();
const filterList = (files) => {
// `files` may be a dict keyed by name (from the API) or an array.
const fileList = Array.isArray(files) ? files : Object.values(files);
const matches = [];
for (const file of fileList) {
if (pathName(file.path).toLowerCase().includes(needle)) {
matches.push(file);
} else if (file.children) {
const children = filterList(file.children);
if (children.length > 0) {
matches.push({...file, children});
}
}
}
return matches;
};
return filterList(browseFiles);
}
function Folder({folder, onFolderClick, sortData, onFileClick, disabled, loadingFolders}) {
const {handleDragStart, handleDragMove, getDragState, shouldAllowClick, selectedPaths, onSelect} = useContext(DragSelectionContext);
// Creates a single table row for a folder, or a row for itself and indented rows for its children.
@ -219,6 +246,17 @@ export function FileBrowser() {
const pendingAutoSelectRef = React.useRef(null);
// Tagged file groups that would be deleted - shown in confirmation modal
const [taggedFileGroups, setTaggedFileGroups] = React.useState(null);
// Reduces the visible rows to those matching, and their parent folders.
const [filterStr, setFilterStr] = React.useState('');
const filterInputRef = React.useRef();
// 'f' focuses the filter input, like the search inputs on other pages.
useHotkeys('f', (e) => {
e.preventDefault();
if (filterInputRef.current) {
filterInputRef.current.focus();
}
}, {enableOnFormTags: false});
const {settings, fetchSettings} = React.useContext(SettingsContext);
const {inverted} = React.useContext(ThemeContext);
@ -457,6 +495,17 @@ export function FileBrowser() {
/>
</div>;
const onCollapseAll = () => {
if (!openFolders) return;
// Deselect any paths that will no longer be visible (descendants of the open folders).
const newSelectedPaths = selectedPaths.filter(p =>
!openFolders.some(folder => p.startsWith(folder) && p !== folder));
if (newSelectedPaths.length !== selectedPaths.length) {
setSelectedPaths(newSelectedPaths);
}
setOpenFolders(null);
}
const onFolderClick = async (folder) => {
let newFolders;
if (openFolders.indexOf(folder) >= 0) {
@ -481,6 +530,11 @@ export function FileBrowser() {
setOpenFolders(newFolders);
}
const filteredFiles = React.useMemo(
() => browseFiles ? filterBrowseFiles(browseFiles, filterStr) : browseFiles,
[browseFiles, filterStr],
);
if (browseFiles === null) {
return <Placeholder>
<PlaceholderLine/>
@ -493,11 +547,40 @@ export function FileBrowser() {
return <DragSelectionProvider
selectedPaths={selectedPaths}
setSelectedPaths={setSelectedPaths}
browseFiles={browseFiles}
browseFiles={filteredFiles}
openFolders={openFolders}
>
<div className='file-browser-filter-container'>
<Input
icon='filter'
iconPosition='left'
placeholder='Filter files...'
value={filterStr}
onChange={(e, {value}) => setFilterStr(value)}
aria-label='Filter files'
className='file-browser-filter'
ref={filterInputRef}
action={<Button
icon='close'
type='button'
onClick={() => setFilterStr('')}
disabled={!filterStr}
className='search-clear'
aria-label='Clear filter'
/>}
/>
<Button
icon='angle double up'
type='button'
onClick={onCollapseAll}
disabled={!openFolders || openFolders.length === 0}
title='Close all folders'
aria-label='Close all folders'
style={{marginLeft: '0.5em'}}
/>
</div>
<FileBrowserContent
browseFiles={browseFiles}
browseFiles={filteredFiles}
headers={headers}
onFolderClick={onFolderClick}
setPreviewFile={setPreviewFile}