diff options
| author | JP Dillingham <jp@dillingham.me> | 2026-07-11 13:30:09 -0500 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2026-07-11 13:30:09 -0500 |
| commit | 0f5fc71a0f956b4984c42d8d830f130bd32bf814 (patch) | |
| tree | 8ce34fa2efdded07f45a1f5adab42ba6fe1ae141 /src/web | |
| parent | 9584bc07cdc4ce5e94ec29273d07a1c67904fdda (diff) | |
| download | slskd-0f5fc71a0f956b4984c42d8d830f130bd32bf814.tar.gz | |
replace Directory with Selection
Diffstat (limited to 'src/web')
| -rw-r--r-- | src/web/src/components/Browse/Directory.jsx | 136 | ||||
| -rw-r--r-- | src/web/src/components/Browse/Selection.jsx | 221 |
2 files changed, 221 insertions, 136 deletions
diff --git a/src/web/src/components/Browse/Directory.jsx b/src/web/src/components/Browse/Directory.jsx deleted file mode 100644 index ed88f131..00000000 --- a/src/web/src/components/Browse/Directory.jsx +++ /dev/null @@ -1,136 +0,0 @@ -import * as transfers from '../../lib/transfers'; -import { formatBytes } from '../../lib/util'; -import FileList from '../Shared/FileList'; -import React, { Component } from 'react'; -import { Button, Card, Icon, Label } from 'semantic-ui-react'; - -const initialState = { - downloadError: '', - downloadRequest: undefined, -}; - -class Directory extends Component { - constructor(props) { - super(props); - - this.state = { - ...initialState, - files: this.props.files.map((f) => ({ selected: false, ...f })), - }; - } - - componentDidUpdate(previousProps) { - if (this.props.name !== previousProps.name) { - this.setState({ - files: this.props.files.map((f) => ({ selected: false, ...f })), - }); - } - } - - handleFileSelectionChange = (file, state) => { - file.selected = state; - this.setState((previousState) => ({ - downloadError: '', - downloadRequest: undefined, - tree: previousState.tree, - })); - }; - - download = (username, files) => { - this.setState({ downloadRequest: 'inProgress' }, async () => { - try { - const requests = (files || []).map(({ filename, size }) => ({ - filename, - size, - })); - await transfers.download({ files: requests, username }); - - this.setState({ downloadRequest: 'complete' }); - } catch (error) { - this.setState({ - downloadError: error.response, - downloadRequest: 'error', - }); - } - }); - }; - - render() { - const { locked, marginTop, name, onClose, username } = this.props; - const { downloadError, downloadRequest, files } = this.state; - - const selectedFiles = files.filter((f) => f.selected); - - const selectedSize = formatBytes( - selectedFiles.reduce((total, f) => total + f.size, 0), - ); - - return ( - <Card - className="result-card" - raised - > - <Card.Content> - <div style={{ marginTop: marginTop || 0 }}> - <FileList - directoryName={name} - disabled={downloadRequest === 'inProgress'} - files={files} - locked={locked} - onClose={onClose} - onSelectionChange={this.handleFileSelectionChange} - /> - </div> - </Card.Content> - {selectedFiles.length > 0 && ( - <Card.Content extra> - <span> - <Button - color="green" - content="Download" - disabled={downloadRequest === 'inProgress'} - icon="download" - label={{ - as: 'a', - basic: false, - content: `${selectedFiles.length} file${selectedFiles.length === 1 ? '' : 's'}, ${selectedSize}`, - }} - labelPosition="right" - onClick={() => this.download(username, selectedFiles)} - /> - {downloadRequest === 'inProgress' && ( - <Icon - loading - name="circle notch" - size="large" - /> - )} - {downloadRequest === 'complete' && ( - <Icon - color="green" - name="checkmark" - size="large" - /> - )} - {downloadRequest === 'error' && ( - <span> - <Icon - color="red" - name="x" - size="large" - /> - <Label> - {downloadError.data + - ` (HTTP ${downloadError.status} ${downloadError.statusText})`} - </Label> - </span> - )} - </span> - </Card.Content> - )} - </Card> - ); - } -} - -export default Directory; diff --git a/src/web/src/components/Browse/Selection.jsx b/src/web/src/components/Browse/Selection.jsx new file mode 100644 index 00000000..e8bc34eb --- /dev/null +++ b/src/web/src/components/Browse/Selection.jsx @@ -0,0 +1,221 @@ +import * as transfers from '../../lib/transfers'; +import { formatBytes } from '../../lib/util'; +import FileBrowser from './FileBrowser'; +import React, { Component } from 'react'; +import { Button, Card, Icon, Label } from 'semantic-ui-react'; + +const initialState = { + downloadError: '', + downloadRequest: undefined, +}; + +const getAllFilesFromNode = (node, separator) => { + const direct = (node.files || []).map((f) => ({ + ...f, + filename: `${node.name}${separator}${f.filename}`, + })); + return [ + ...direct, + ...(node.children || []).flatMap((child) => + getAllFilesFromNode(child, separator), + ), + ]; +}; + +class Selection extends Component { + constructor(props) { + super(props); + const selectedFilenames = new Set(props.defaultSelectedFiles || []); + this.state = { + ...initialState, + expandedDirectory: props.defaultSubdirectory ?? null, + files: getAllFilesFromNode(props.node, props.separator).map((f) => ({ + selected: selectedFilenames.has(f.filename), + ...f, + })), + }; + } + + componentDidUpdate(previousProps) { + if (this.props.node?.name !== previousProps.node?.name) { + this.setState({ + ...initialState, + expandedDirectory: this.props.node?.children?.[0]?.name ?? null, + files: getAllFilesFromNode(this.props.node, this.props.separator).map( + (f) => ({ selected: false, ...f }), + ), + }); + } + } + + handleSelectionChange = (file, selected) => { + this.setState( + (prevState) => ({ + downloadError: '', + downloadRequest: undefined, + files: prevState.files.map((f) => + f === file ? { ...f, selected } : f, + ), + }), + () => + this.props.onStateChange?.({ + files: this.state.files + .filter((f) => f.selected) + .map((f) => f.filename), + subdirectory: this.state.expandedDirectory, + }), + ); + }; + + handleExpandedDirectoryChange = (name) => { + this.setState({ expandedDirectory: name }, () => + this.props.onStateChange?.({ + files: this.state.files + .filter((f) => f.selected) + .map((f) => f.filename), + subdirectory: this.state.expandedDirectory, + }), + ); + }; + + handleDownload = () => { + const { name, separator, username } = this.props; + const selectedFiles = this.state.files.filter((f) => f.selected); + const parent = name.split(separator).slice(0, -1).join(separator); + const prefix = name + separator; + + this.setState({ downloadRequest: 'inProgress' }, async () => { + try { + const groups = new Map(); + for (const file of selectedFiles) { + const relative = file.filename.startsWith(prefix) + ? file.filename.slice(prefix.length) + : file.filename; + const parts = relative.split(separator).filter(Boolean); + const group = parts.length > 1 ? parts[0] : ''; + if (!groups.has(group)) { + groups.set(group, []); + } + + groups.get(group).push(file); + } + + const rootFiles = groups.get('') || []; + if (rootFiles.length > 0) { + await transfers.download({ + files: rootFiles.map(({ filename, size }) => ({ filename, size })), + username, + }); + } + + for (const [dirName, dirFiles] of groups) { + if (dirName === '') { + continue; + } + + const destination = (prefix + dirName) + .slice(parent.length > 0 ? parent.length + 1 : 0) + .split(separator) + .join('/'); + await transfers.enqueueBatch({ + files: dirFiles.map(({ filename, size }) => ({ filename, size })), + options: { destination }, + username, + }); + } + + this.setState({ downloadRequest: 'complete' }); + } catch (error) { + this.setState({ + downloadError: error.response, + downloadRequest: 'error', + }); + } + }); + }; + + render() { + const { directorySuffix, locked, marginTop, node, onClose, separator } = + this.props; + const { downloadError, downloadRequest, expandedDirectory, files } = + this.state; + + const selectedFiles = files.filter((f) => f.selected); + const totalSize = formatBytes( + selectedFiles.reduce((sum, f) => sum + (f.size || 0), 0), + ); + + return ( + <Card + className="result-card" + raised + > + <Card.Content> + <div style={{ marginTop: marginTop || 0 }}> + <FileBrowser + directorySuffix={directorySuffix} + disabled={downloadRequest === 'inProgress'} + expandedDirectory={expandedDirectory} + files={files} + locked={locked} + onClose={onClose} + onExpandedDirectoryChange={this.handleExpandedDirectoryChange} + onSelectionChange={this.handleSelectionChange} + rootDirectory={node} + separator={separator} + /> + </div> + </Card.Content> + {selectedFiles.length > 0 && ( + <Card.Content extra> + <span> + <Button + color="green" + content="Download" + disabled={downloadRequest === 'inProgress'} + icon="download" + label={{ + as: 'a', + basic: false, + content: `${selectedFiles.length} file${selectedFiles.length === 1 ? '' : 's'}, ${totalSize}`, + }} + labelPosition="right" + onClick={this.handleDownload} + /> + {downloadRequest === 'inProgress' && ( + <Icon + loading + name="circle notch" + size="large" + /> + )} + {downloadRequest === 'complete' && ( + <Icon + color="green" + name="checkmark" + size="large" + /> + )} + {downloadRequest === 'error' && ( + <span> + <Icon + color="red" + name="x" + size="large" + /> + <Label> + {downloadError?.data + ? `${downloadError.data} (HTTP ${downloadError.status} ${downloadError.statusText})` + : 'Download failed'} + </Label> + </span> + )} + </span> + </Card.Content> + )} + </Card> + ); + } +} + +export default Selection; |
