From 4d5c7c83c080f76a5f5fa11a67e14ea34826ab75 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 11 Jul 2026 18:12:06 -0500 Subject: simplify selection logic, improve performance significantly --- src/web/src/components/Browse/FileBrowser.jsx | 38 +++++++++++++++++++-------- src/web/src/components/Browse/Selection.jsx | 15 ++++++----- 2 files changed, 35 insertions(+), 18 deletions(-) (limited to 'src') diff --git a/src/web/src/components/Browse/FileBrowser.jsx b/src/web/src/components/Browse/FileBrowser.jsx index 699b9ad7..0f84a3cc 100644 --- a/src/web/src/components/Browse/FileBrowser.jsx +++ b/src/web/src/components/Browse/FileBrowser.jsx @@ -136,11 +136,7 @@ const DirectoryRow = ({ checked={allChildSelected} disabled={disabled} fitted - onChange={(_, data) => { - for (const f of childFiles) { - onSelectionChange(f, data.checked); - } - }} + onChange={(_, data) => onSelectionChange(childFiles, data.checked)} /> )} @@ -259,6 +255,8 @@ const FileBrowser = ({ }) => { const [folded, setFolded] = useState(false); + // flatten the list of files that we've been given and turn it into a map + // keyed on the fully qualified name of the file and with a value of the file object const fileMap = useMemo(() => { const map = new Map(); for (const f of files || []) { @@ -271,7 +269,7 @@ const FileBrowser = ({ const directoryName = rootDirectory?.name || ''; const sep = separator || '\\'; - const directFiles = useMemo( + const filesInRoot = useMemo( () => (rootDirectory?.files || []) .map((f) => ({ @@ -318,16 +316,34 @@ const FileBrowser = ({ (files?.length ?? 0) > 0 && files.every((f) => f.selected); const handleSelectAll = (_, data) => { + onSelectionChange(data.checked ? (files || []).map((f) => f.filename) : []); + }; + + const handleFileToggle = (file, checked) => { + const result = []; + for (const f of files || []) { + if (f === file ? checked : f.selected) result.push(f.filename); + } + + onSelectionChange(result); + }; + + const handleDirToggle = (childFiles, checked) => { + const childSet = new Set(childFiles.map((f) => f.filename)); + const result = []; for (const f of files || []) { - onSelectionChange(f, data.checked); + if (childSet.has(f.filename) ? checked : f.selected) + result.push(f.filename); } + + onSelectionChange(result); }; const handleExpandDirectory = (name) => { onExpandedDirectoryChange(expandedDirectory === name ? null : name); }; - const hasContent = directFiles.length > 0 || childDirs.length > 0; + const hasContent = filesInRoot.length > 0 || childDirs.length > 0; return (
@@ -390,11 +406,11 @@ const FileBrowser = ({ isExpanded={expandedDirectory === child.name} key={child.name} onExpand={handleExpandDirectory} - onSelectionChange={onSelectionChange} + onSelectionChange={handleDirToggle} sep={sep} /> ))} - {directFiles.map((f) => { + {filesInRoot.map((f) => { const flatFile = fileMap.get(f.fullFilename); return ( ); })} diff --git a/src/web/src/components/Browse/Selection.jsx b/src/web/src/components/Browse/Selection.jsx index e8bc34eb..2a5539b6 100644 --- a/src/web/src/components/Browse/Selection.jsx +++ b/src/web/src/components/Browse/Selection.jsx @@ -7,6 +7,7 @@ import { Button, Card, Icon, Label } from 'semantic-ui-react'; const initialState = { downloadError: '', downloadRequest: undefined, + files: [], }; const getAllFilesFromNode = (node, separator) => { @@ -48,20 +49,20 @@ class Selection extends Component { } } - handleSelectionChange = (file, selected) => { + handleSelectionChange = (selectedFilenames) => { + const selectedSet = new Set(selectedFilenames); this.setState( (prevState) => ({ downloadError: '', downloadRequest: undefined, - files: prevState.files.map((f) => - f === file ? { ...f, selected } : f, - ), + files: prevState.files.map((f) => ({ + ...f, + selected: selectedSet.has(f.filename), + })), }), () => this.props.onStateChange?.({ - files: this.state.files - .filter((f) => f.selected) - .map((f) => f.filename), + files: selectedFilenames, subdirectory: this.state.expandedDirectory, }), ); -- cgit v1.2.3