diff options
| author | JP Dillingham <jp@dillingham.me> | 2023-07-23 17:47:10 -0500 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2023-07-23 17:47:10 -0500 |
| commit | 5c139be17a369039dde9033ebb0ec2de20f4c6fc (patch) | |
| tree | d069b405fc00fd1ac7134c522c13b022179ed98b /src | |
| parent | 3ef512d000390a3f984394f462bceba6b30b9385 (diff) | |
| download | slskd-5c139be17a369039dde9033ebb0ec2de20f4c6fc.tar.gz | |
implement deletes
Diffstat (limited to 'src')
| -rw-r--r-- | src/web/src/components/System/Files/Explorer.js | 111 |
1 files changed, 84 insertions, 27 deletions
diff --git a/src/web/src/components/System/Files/Explorer.js b/src/web/src/components/System/Files/Explorer.js index adc8d3bf..aa2ae784 100644 --- a/src/web/src/components/System/Files/Explorer.js +++ b/src/web/src/components/System/Files/Explorer.js @@ -3,58 +3,115 @@ import React, { useState, useEffect } from 'react'; import { Table, Icon, - Message + Message, + Modal, + Header, } from 'semantic-ui-react'; -import { list } from '../../../lib/files'; +import { list, deleteDirectory, deleteFile } from '../../../lib/files'; import { formatBytes, formatDate } from '../../../lib/util'; import { LoaderSegment } from '../../Shared'; -const Explorer = ({ root, isActive }) => { +const Explorer = ({ root }) => { const [directory, setDirectory] = useState({ files: [], directories: [] }); const [subdirectory, setSubdirectory] = useState([]); const [loading, setLoading] = useState(false); useEffect(() => { - setLoading(true); - - console.log('fetching...', subdirectory); - const fetch = async () => { - const dir = await list({ root, subdirectory: subdirectory.join('/') }); - console.log(dir) - setDirectory(dir); - setLoading(false); - }; - fetch(); - }, [isActive, subdirectory]); + }, [subdirectory]); // eslint-disable-line react-hooks/exhaustive-deps + + useEffect(() => { + setSubdirectory([]); + }, [root]); + + const fetch = async () => { + setLoading(true); + const dir = await list({ root, subdirectory: subdirectory.join('/') }); + setDirectory(dir); + setLoading(false); + }; const select = ({ path }) => { setSubdirectory([...subdirectory, path]); - console.log(path) }; - const row = (icon, { name, fullName, modifiedAt, length }, onClick = () => { }) => <Table.Row key={name}> - <Table.Cell - style={{ cursor: 'pointer' }} - onClick={() => onClick({ path: name })} - ><Icon name={icon} />{name}</Table.Cell> + const upOneSubdirectory = () => { + const copy = [...subdirectory]; + copy.pop(); + setSubdirectory(copy); + }; + + const FileRow = ({ name, fullName, modifiedAt, length }) => <Table.Row key={fullName}> + <Table.Cell><Icon name='file outline'/>{name}</Table.Cell> <Table.Cell>{modifiedAt ? formatDate(modifiedAt) : ''}</Table.Cell> <Table.Cell>{length ? formatBytes(length) : ''}</Table.Cell> <Table.Cell> - <Icon name="trash alternate" color="red" onClick={() => console.log(fullName)} style={{ cursor: 'pointer' }}/> + <Modal + trigger={ + <Icon name="trash alternate" color="red" style={{ cursor: 'pointer' }}/> + } + centered + size='small' + header={<Header icon='trash alternate' content='Confirm File Delete' />} + content={`Are you sure you want to delete file '${fullName}'?`} + actions={[ + 'Cancel', + { + key: 'done', + content: 'Delete', + negative: true, + onClick: async () => { + await deleteFile({ root, path: `${subdirectory.join('/')}/${fullName}`}); + fetch(); + }, + }, + ]} + /> </Table.Cell> </Table.Row>; + const DirectoryRow = ({ name, fullName, modifiedAt, onClick = () => {} }) => <Table.Row key={fullName}> + <Table.Cell + style={{ cursor: 'pointer' }} + onClick={onClick} + ><Icon name='folder'/>{name}</Table.Cell> + <Table.Cell>{modifiedAt ? formatDate(modifiedAt) : ''}</Table.Cell> + <Table.Cell></Table.Cell> + <Table.Cell> + <Modal + trigger={ + <Icon name="trash alternate" color="red" style={{ cursor: 'pointer' }}/> + } + centered + size='small' + header={<Header icon='trash alternate' content='Confirm Directory Delete' />} + content={`Are you sure you want to delete directory '${fullName}'?`} + actions={[ + 'Cancel', + { + key: 'done', + content: 'Delete', + negative: true, + onClick: async () => { + await deleteDirectory({ root, path: `${subdirectory.join('/')}/${fullName}`}); + fetch(); + }, + }, + ]} + /> + </Table.Cell> + </Table.Row>; + if (loading) { return <LoaderSegment/>; } return ( <> - {!!subdirectory && <Message className='no-grow edit-code-header'> - <Icon name='folder open'/>{root + '/' + subdirectory} - </Message>} + <Message className='no-grow explorer-working-directory'> + <Icon name='folder open'/>{'/' + root + '/' + subdirectory.join('/')} + </Message> <Table size='large' className='unstackable'> <Table.Header> <Table.Row> @@ -65,9 +122,9 @@ const Explorer = ({ root, isActive }) => { </Table.Row> </Table.Header> <Table.Body> - {!!subdirectory && row('folder', { name: '..', fullName: '..' })} - {directory?.directories?.map((d => row('folder', d, select)))} - {directory?.files?.map(f => row('file outline', f))} + {subdirectory.length > 0 && <DirectoryRow name=".." fullName=".." onClick={upOneSubdirectory}/>} + {directory?.directories?.map(d => <DirectoryRow onClick={() => select({ path: d.name })} {...d}/>)} + {directory?.files?.map(f => <FileRow {...f}/>)} </Table.Body> </Table> </> |
