diff options
| author | JP Dillingham <jpdillingham@gmail.com> | 2022-04-27 09:33:36 -0500 |
|---|---|---|
| committer | JP Dillingham <jpdillingham@gmail.com> | 2022-04-27 09:33:36 -0500 |
| commit | 483df13b252ba8e64e63640414b132ebbc1177a6 (patch) | |
| tree | 697477719a669c90754f4ca484e2a2842d5aaa3f /src/web | |
| parent | 823ee6d70dde6a579d12c2d173102ad1870a95df (diff) | |
| download | slskd-483df13b252ba8e64e63640414b132ebbc1177a6.tar.gz | |
layout wip
Diffstat (limited to 'src/web')
| -rw-r--r-- | src/web/src/components/Search/List/SearchList.js | 151 | ||||
| -rw-r--r-- | src/web/src/components/Search/List/SearchListAlt.js | 168 | ||||
| -rw-r--r-- | src/web/src/components/Search/List/SearchListRow.js | 54 | ||||
| -rw-r--r-- | src/web/src/components/Search/Searches.js | 62 |
4 files changed, 378 insertions, 57 deletions
diff --git a/src/web/src/components/Search/List/SearchList.js b/src/web/src/components/Search/List/SearchList.js new file mode 100644 index 00000000..9e206e42 --- /dev/null +++ b/src/web/src/components/Search/List/SearchList.js @@ -0,0 +1,151 @@ +import React, { useState, useEffect, useRef } from 'react'; +import { v4 as uuidv4 } from 'uuid'; + + +import * as lib from '../../../lib/searches'; +import { createSearchLogHubConnection } from '../../../lib/hubFactory'; + +import SearchListRow from './SearchListRow'; + +import { + Card, + Table, + Icon +} from 'semantic-ui-react'; +import SearchIcon from '../SearchIcon'; + +const SearchList = () => { + const [{ loading, loadError }, setLoading] = useState({ loading: true, loadError: false }); + const [{ creating, createError }, setCreating] = useState({ creating: false, createError: false }); + const [connected, setConnected] = useState(false); + const [searches, setSearches] = useState([]); + const inputRef = useRef(); + + useEffect(() => { + get(); + + const searchHub = createSearchLogHubConnection(); + + searchHub.on('update', search => { + setConnected(true); + setSearches(old => { + const idx = old.findIndex(s => s.id === search.id); + + if (idx < 0) { + return [search, ...old] + } else { + old[idx] = search + return [...old]; + } + }) + }); + + searchHub.on('response', () => { }); + + searchHub.onreconnecting(() => setConnected(false)); + searchHub.onclose(() => setConnected(false)); + searchHub.onreconnected(() => setConnected(true)); + + searchHub.start(); + }, []); + + const create = async () => { + const searchText = inputRef.current.inputRef.current.value; + const id = uuidv4(); + + try { + setCreating({ creating: true, createError: false }) + lib.create({ id, searchText }) + setCreating({ creating: false, createError: false }) + inputRef.current.inputRef.current.value = ''; + } catch (error) { + setCreating({ creating: false, createError: error.message }) + } + } + + const get = async () => { + setLoading({ loading: true, error: false }); + + try { + const all = await lib.getAll(); + setSearches(all); + setLoading({ loading: false, error: false }) + } catch (error) { + setLoading({ loading: false, error: error.message }) + } + } + + const remove = async (search) => { + console.log('remove', searches) + try { + await lib.remove({ id: search.id }) + setSearches(old => old.filter(s => s.id !== search.id)) + } catch (err) { + console.error(err) + // noop + } + } + + const stop = async (search) => { + await lib.stop({ id: search.id }) + } + + const cancelAndDeleteAll = () => { + // todo + } + + return ( + <> + <Card className='search-card' raised> + <Card.Content> + <Card.Header> + <Icon name='search'/> + Searches + <Icon.Group className='close-button' style={{ marginLeft: 10 }}> + <Icon + name='trash alternate' + color='red' + link + onClick={() => cancelAndDeleteAll()} + /> + <Icon corner name='asterisk'/> + </Icon.Group> + <Icon.Group className='close-button' > + <Icon + name='stop circle' + color='black' + link + onClick={() => cancelAndDeleteAll()} + /> + <Icon corner name='asterisk'/> + </Icon.Group> + </Card.Header> + <Table size='large' selectable> + <Table.Header> + <Table.Row> + <Table.HeaderCell></Table.HeaderCell> + <Table.HeaderCell>Started</Table.HeaderCell> + <Table.HeaderCell>Search Phrase</Table.HeaderCell> + <Table.HeaderCell>Responses</Table.HeaderCell> + <Table.HeaderCell>Files (Locked)</Table.HeaderCell> + <Table.HeaderCell></Table.HeaderCell> + </Table.Row> + </Table.Header> + <Table.Body> + {searches + .sort((a, b) => (new Date(b.startedAt) - new Date(a.startedAt))) + .map((search, index) => <SearchListRow + search={search} + key={index} + onRemove={remove} + onStop={stop} + />)} + </Table.Body> + </Table> + </Card.Content> + </Card> + </> + ) +}; + +export default SearchList;
\ No newline at end of file diff --git a/src/web/src/components/Search/List/SearchListAlt.js b/src/web/src/components/Search/List/SearchListAlt.js new file mode 100644 index 00000000..0ef0fca0 --- /dev/null +++ b/src/web/src/components/Search/List/SearchListAlt.js @@ -0,0 +1,168 @@ +import React, { useState, useEffect, useRef } from 'react'; +import { v4 as uuidv4 } from 'uuid'; + + +import * as lib from '../../../lib/searches'; +import { createSearchLogHubConnection } from '../../../lib/hubFactory'; + +import SearchListRow from './SearchListRow'; + +import { + Card, + Item, + Icon, + Button, + Label, + Table, + Statistic +} from 'semantic-ui-react'; +import SearchIcon from '../SearchIcon'; + +const SearchListAlt = () => { + const [{ loading, loadError }, setLoading] = useState({ loading: true, loadError: false }); + const [{ creating, createError }, setCreating] = useState({ creating: false, createError: false }); + const [connected, setConnected] = useState(false); + const [searches, setSearches] = useState([]); + const inputRef = useRef(); + + useEffect(() => { + get(); + + const searchHub = createSearchLogHubConnection(); + + searchHub.on('update', search => { + setConnected(true); + setSearches(old => { + const idx = old.findIndex(s => s.id === search.id); + + if (idx < 0) { + return [search, ...old] + } else { + old[idx] = search + return [...old]; + } + }) + }); + + searchHub.on('response', () => { }); + + searchHub.onreconnecting(() => setConnected(false)); + searchHub.onclose(() => setConnected(false)); + searchHub.onreconnected(() => setConnected(true)); + + searchHub.start(); + }, []); + + const create = async () => { + const searchText = inputRef.current.inputRef.current.value; + const id = uuidv4(); + + try { + setCreating({ creating: true, createError: false }) + lib.create({ id, searchText }) + setCreating({ creating: false, createError: false }) + inputRef.current.inputRef.current.value = ''; + } catch (error) { + setCreating({ creating: false, createError: error.message }) + } + } + + const get = async () => { + setLoading({ loading: true, error: false }); + + try { + const all = await lib.getAll(); + setSearches(all); + setLoading({ loading: false, error: false }) + } catch (error) { + setLoading({ loading: false, error: error.message }) + } + } + + const remove = async (search) => { + console.log('remove', searches) + try { + await lib.remove({ id: search.id }) + setSearches(old => old.filter(s => s.id !== search.id)) + } catch (err) { + console.error(err) + // noop + } + } + + const stop = async (search) => { + await lib.stop({ id: search.id }) + } + + const cancelAndDeleteAll = () => { + // todo + } + + return ( + <Card className='search-card' raised> + <Card.Content> + <Card.Header> + <Icon name='search'/> + Searches + <Icon.Group className='close-button' style={{ marginLeft: 10 }}> + <Icon + name='trash alternate' + color='red' + link + onClick={() => cancelAndDeleteAll()} + /> + <Icon corner name='asterisk'/> + </Icon.Group> + <Icon.Group className='close-button' > + <Icon + name='stop circle' + color='black' + link + onClick={() => cancelAndDeleteAll()} + /> + <Icon corner name='asterisk'/> + </Icon.Group> + </Card.Header> + <Item.Group divided> + {searches + .sort((a, b) => (new Date(b.startedAt) - new Date(a.startedAt))) + .map((search, index) => + <Item> + {/* <Item.Image src='https://react.semantic-ui.com/images/wireframe/image.png' /> */} + <Item.Content> + <Item.Header> + {search.searchText} + </Item.Header> + <Item.Meta>Basic Search | {search.state}</Item.Meta> + <Item.Description> + </Item.Description> + <Item.Extra> + <Statistic.Group widths='4' style={{marginTop: 30}} size='mini'> + <Statistic> + <Statistic.Value>{search.responseCount}</Statistic.Value> + <Statistic.Label>Responses</Statistic.Label> + </Statistic> + <Statistic> + <Statistic.Value>{search.fileCount}</Statistic.Value> + <Statistic.Label>Files</Statistic.Label> + </Statistic> + <Statistic color='yellow'> + <Statistic.Value><Icon name='lock'/>{search.lockedFileCount}</Statistic.Value> + <Statistic.Label> + Locked + </Statistic.Label> + </Statistic> + </Statistic.Group> + <Icon size='large' style={{float: 'right', marginTop: -50}} name='trash alternate' color='red'/> + </Item.Extra> + </Item.Content> + </Item> + ) + } + </Item.Group> + </Card.Content> + </Card> + ) +}; + +export default SearchListAlt;
\ No newline at end of file diff --git a/src/web/src/components/Search/List/SearchListRow.js b/src/web/src/components/Search/List/SearchListRow.js new file mode 100644 index 00000000..1c6b4ae6 --- /dev/null +++ b/src/web/src/components/Search/List/SearchListRow.js @@ -0,0 +1,54 @@ +import React, { useState } from 'react'; + +import SearchIcon from '../SearchIcon'; + +import { + Table, + Icon +} from 'semantic-ui-react'; + +const SearchListRow = ({ search, index, onRemove, onStop }) => { + const [working, setWorking] = useState(false); + + const invoke = async (func) => { + setWorking(true); + await func(); + setWorking(false); + } + + const ActionIcon = () => { + if (working) { + return (<Icon name='hourglass half'/>) + } + + if (search.state.includes('Completed')) { + return (<Icon + name="trash alternate" + color='red' + onClick={() => invoke(() => onRemove(search))} + />) + } + + return (<Icon + name="stop circle" + color="red" + onClick={() => invoke(() => onStop(search))} + />) + } + + return ( + <Table.Row + disabled={working} + style={{ cursor: working ? 'wait' : 'pointer' }} + > + <Table.Cell>{working}<SearchIcon state={search.state}/></Table.Cell> + <Table.Cell>{search.startedAt}</Table.Cell> + <Table.Cell>{search.searchText}</Table.Cell> + <Table.Cell>{search.responseCount}</Table.Cell> + <Table.Cell>{search.fileCount} ({search.lockedFileCount})</Table.Cell> + <Table.Cell><ActionIcon/></Table.Cell> + </Table.Row> + ) +} + +export default SearchListRow;
\ No newline at end of file diff --git a/src/web/src/components/Search/Searches.js b/src/web/src/components/Search/Searches.js index 789be02f..6f7efe4e 100644 --- a/src/web/src/components/Search/Searches.js +++ b/src/web/src/components/Search/Searches.js @@ -1,4 +1,3 @@ -import { orderBy } from 'lodash'; import React, { useState, useEffect, useRef } from 'react'; import { v4 as uuidv4 } from 'uuid'; @@ -6,6 +5,9 @@ import { v4 as uuidv4 } from 'uuid'; import * as lib from '../../lib/searches'; import { createSearchLogHubConnection } from '../../lib/hubFactory'; +import SearchList from './List/SearchList'; +import SearchListAlt from './List/SearchListAlt'; + import './Search.css'; import { @@ -112,62 +114,8 @@ const Searches = () => { onKeyUp={(e) => e.key === 'Enter' ? create() : ''} /> </Segment> - <Card className='search-card' raised> - <Card.Content> - <Card.Header> - <Icon name='search'/> - Searches - <Icon.Group className='close-button' style={{ marginLeft: 10 }}> - <Icon - name='trash alternate' - color='red' - link - onClick={() => cancelAndDeleteAll()} - /> - <Icon corner name='asterisk'/> - </Icon.Group> - <Icon.Group className='close-button' > - <Icon - name='stop circle' - color='black' - link - onClick={() => cancelAndDeleteAll()} - /> - <Icon corner name='asterisk'/> - </Icon.Group> - </Card.Header> - <Table selectable> - <Table.Header> - <Table.Row> - <Table.HeaderCell></Table.HeaderCell> - <Table.HeaderCell>Started</Table.HeaderCell> - <Table.HeaderCell>Search Phrase</Table.HeaderCell> - <Table.HeaderCell>Responses</Table.HeaderCell> - <Table.HeaderCell>Files (Locked)</Table.HeaderCell> - <Table.HeaderCell></Table.HeaderCell> - </Table.Row> - </Table.Header> - <Table.Body> - {searches - .sort((a, b) => { - //console.log(new Date(b.startedAt), new Date(a.startedAt)) - return new Date(b.startedAt) - new Date(a.startedAt) - }) - .map((s, index) => <Table.Row style={{ cursor: 'pointer' }} key={index}> - <Table.Cell><SearchIcon state={s.state} alt={s.state}/></Table.Cell> - <Table.Cell>{s.startedAt}</Table.Cell> - <Table.Cell>{s.searchText}</Table.Cell> - <Table.Cell>{s.responseCount}</Table.Cell> - <Table.Cell>{s.fileCount} ({s.lockedFileCount})</Table.Cell> - <Table.Cell>{s.state.includes('Completed') ? - <Icon name="trash alternate" color='red' onClick={() => remove(s)}/> : - <Icon name="stop circle" color="red" onClick={() => stop(s)}/>} - </Table.Cell> - </Table.Row>)} - </Table.Body> - </Table> - </Card.Content> - </Card> + <SearchList/> + <SearchListAlt/> </div> ) }; |
