diff options
| author | JP Dillingham <jp@dillingham.me> | 2022-05-17 10:00:31 -0500 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2022-05-17 10:00:31 -0500 |
| commit | b8bf146c78e26dd7d8a9fc849b508434b5a2a3e2 (patch) | |
| tree | c086ba498f51cd36dc553056accb73db2ad287ab /src | |
| parent | 30d5f990c5bd0a5519f3a8242c91a23d663d8b60 (diff) | |
| download | slskd-b8bf146c78e26dd7d8a9fc849b508434b5a2a3e2.tar.gz | |
clean up and document
Diffstat (limited to 'src')
| -rw-r--r-- | src/web/src/components/Search/Detail/SearchDetail.js | 50 | ||||
| -rw-r--r-- | src/web/src/components/Search/Detail/SearchDetailHeader.js | 22 | ||||
| -rw-r--r-- | src/web/src/components/Search/Search.css | 3 | ||||
| -rw-r--r-- | src/web/src/components/Search/Searches.js | 70 |
4 files changed, 102 insertions, 43 deletions
diff --git a/src/web/src/components/Search/Detail/SearchDetail.js b/src/web/src/components/Search/Detail/SearchDetail.js index 19cc1b84..880d5cd0 100644 --- a/src/web/src/components/Search/Detail/SearchDetail.js +++ b/src/web/src/components/Search/Detail/SearchDetail.js @@ -8,6 +8,7 @@ import { Button, } from 'semantic-ui-react'; +import { sleep } from '../../../lib/util'; import Switch from '../../Shared/Switch'; import ErrorSegment from '../../Shared/ErrorSegment'; import Response from '../Response'; @@ -20,7 +21,7 @@ const sortDropdownOptions = [ { key: 'queueLength', text: 'Queue Depth (Least to Most)', value: 'queueLength' } ]; -const SearchDetail = ({ search, onCreate, onStop, onRemove }) => { +const SearchDetail = ({ search, creating, stopping, removing, onCreate, onStop, onRemove }) => { const { id, state, isComplete, fileCount, lockedFileCount, responseCount } = search; const [loading, setLoading] = useState(false); @@ -28,6 +29,7 @@ const SearchDetail = ({ search, onCreate, onStop, onRemove }) => { const [results, setResults] = useState([]); + // filters and sorting options const [hiddenResults, setHiddenResults] = useState([]); const [resultSort, setResultSort] = useState('uploadSpeed'); const [hideLocked, setHideLocked] = useState(true); @@ -36,10 +38,17 @@ const SearchDetail = ({ search, onCreate, onStop, onRemove }) => { const [resultFilters, setResultFilters] = useState(''); const [displayCount, setDisplayCount] = useState(5); + // when the search transitions from !isComplete -> isComplete, + // fetch the results from the server useEffect(() => { const get = async () => { try { setLoading(true); + + // the results may not be ready yet. this is very rare, but + // if it happens the search will complete with no results. + await sleep(500); + const responses = await getResponses({ id }); setResults(responses); setLoading(false); @@ -54,6 +63,8 @@ const SearchDetail = ({ search, onCreate, onStop, onRemove }) => { } }, [id, isComplete]) + // apply sorting and filters. this can take a while for larger result + // sets, so memoize it. const sortedAndFilteredResults = useMemo(() => { const sortOptions = { uploadSpeed: { field: 'uploadSpeed', order: 'desc' }, @@ -85,8 +96,29 @@ const SearchDetail = ({ search, onCreate, onStop, onRemove }) => { }, [results, hideLocked, hideNoFreeSlots, resultFilters, resultSort, hiddenResults]) + // when a user uses the action buttons, we will *probably* re-use this component, + // but with a new search ID. clear everything to prepare for the transition + const reset = () => { + setLoading(false); + setError(undefined); + setResults([]); + setHiddenResults([]); + setDisplayCount(5); + } + + const create = async ({ search, navigate }) => { + reset(); + onCreate({ search, navigate }) + } + + const remove = async () => { + reset(); + onRemove(search); + } + const filteredCount = results?.length - sortedAndFilteredResults.length; const remainingCount = sortedAndFilteredResults.length - displayCount; + const loaded = (!removing && !creating && !loading && results && results.length > 0) if (error) { return (<ErrorSegment caption={error?.message ?? error}/>) @@ -96,10 +128,14 @@ const SearchDetail = ({ search, onCreate, onStop, onRemove }) => { <> <SearchDetailHeader loading={loading} + loaded={loaded} + removing={removing} + stopping={stopping} + creating={creating} search={search} - onCreate={onCreate} + onCreate={create} onStop={onStop} - onRemove={onRemove} + onRemove={remove} /> <Switch searching={!isComplete && @@ -110,7 +146,7 @@ const SearchDetail = ({ search, onCreate, onStop, onRemove }) => { } loading={loading && <LoaderSegment/>} > - {(results && results.length > 0) && + {loaded && <Segment className='search-options' raised> <Dropdown button @@ -155,7 +191,7 @@ const SearchDetail = ({ search, onCreate, onStop, onRemove }) => { /> </Segment> } - {sortedAndFilteredResults.slice(0, displayCount).map((r, i) => + {loaded && sortedAndFilteredResults.slice(0, displayCount).map((r, i) => <Response key={i} response={r} @@ -163,13 +199,13 @@ const SearchDetail = ({ search, onCreate, onStop, onRemove }) => { isInitiallyFolded={foldResults} /> )} - {remainingCount > 0 ? + {loaded && (remainingCount > 0 ? <Button className='showmore-button' size='large' fluid primary onClick={() => setDisplayCount(displayCount + 5)}> Show {remainingCount > 5 ? 5 : remainingCount} More Results {`(${remainingCount} remaining, ${filteredCount} hidden by filter(s))`} </Button> : filteredCount > 0 ? - <Button className='showmore-button' size='large' fluid disabled>{`All results shown. ${filteredCount} results hidden by filter(s)`}</Button> : ''} + <Button className='showmore-button' size='large' fluid disabled>{`All results shown. ${filteredCount} results hidden by filter(s)`}</Button> : '')} </Switch> </> ) diff --git a/src/web/src/components/Search/Detail/SearchDetailHeader.js b/src/web/src/components/Search/Detail/SearchDetailHeader.js index 7b78d134..d31f8a49 100644 --- a/src/web/src/components/Search/Detail/SearchDetailHeader.js +++ b/src/web/src/components/Search/Detail/SearchDetailHeader.js @@ -9,11 +9,12 @@ import { } from 'semantic-ui-react'; import SearchStatusIcon from '../SearchStatusIcon'; -const SearchDetailHeader = ({ search, loading, onCreate, onStop, onRemove }) => { +const SearchDetailHeader = ({ search, loading, loaded, creating, removing, stopping, onCreate, onStop, onRemove }) => { const isSmallScreen = useMediaQuery({ query: '(max-width: 899px)' }); const isTinyScreen = useMediaQuery({ query: '(max-width: 684px)'}); const { searchText, isComplete, state } = search; + const working = loading || creating || removing || stopping; const stopOrRemove = () => { if (isComplete) { @@ -23,27 +24,32 @@ const SearchDetailHeader = ({ search, loading, onCreate, onStop, onRemove }) => } } - const RefreshButton = ({ ...props }) => + const RefreshButton = () => loaded && <Button - disabled={loading} + disabled={working} icon={isSmallScreen && !isTinyScreen} onClick={() => onCreate({ search: searchText, navigate: true })} + loading={creating} > <Icon name='refresh'/>{(!isSmallScreen || isTinyScreen) && 'Search Again'} </Button> - const StopOrDeleteButton = ({ ...props }) => + const StopOrDeleteButton = () => <Button negative - disabled={loading} + disabled={working} onClick={stopOrRemove} - floated={isTinyScreen ? 'right' : ''} + loading={removing || stopping} + floated={isTinyScreen ? 'right' : undefined} icon={isSmallScreen && !isTinyScreen} > <Icon name={isComplete ? 'trash alternate' : 'stop circle'}/> - {(!isSmallScreen || isTinyScreen) && (isComplete ? 'Delete' : 'Stop')} + {(!isSmallScreen || isTinyScreen) && ((loaded && isComplete) ? 'Delete' : 'Stop')} </Button> + // if the screen is full width, display the header and action buttons in the same segment, with full + // button text. if the screen is between 684 and 899 pixels, display the buttons with no text. + // if the screen is less than 684 pixels, display the action buttons in a new segment, with full text. return ( <> <Segment className='search-segment' raised> @@ -55,7 +61,7 @@ const SearchDetailHeader = ({ search, loading, onCreate, onStop, onRemove }) => <StopOrDeleteButton/> </div>} </Segment> - {isTinyScreen && <Segment> + {isTinyScreen && <Segment> <RefreshButton/> <StopOrDeleteButton/> </Segment>} diff --git a/src/web/src/components/Search/Search.css b/src/web/src/components/Search/Search.css index fb90353d..42912ed5 100644 --- a/src/web/src/components/Search/Search.css +++ b/src/web/src/components/Search/Search.css @@ -101,8 +101,7 @@ .search-detail-header { font-weight: normal !important; - margin-top: 13px !important; - margin-left: 16px !important; + margin-top: 12px !important; } .search-detail-action-button { diff --git a/src/web/src/components/Search/Searches.js b/src/web/src/components/Search/Searches.js index 32496bf9..33fb4176 100644 --- a/src/web/src/components/Search/Searches.js +++ b/src/web/src/components/Search/Searches.js @@ -14,27 +14,29 @@ import { Input, Segment, Button, - Loader, } from 'semantic-ui-react'; import SearchDetail from './Detail/SearchDetail'; import ErrorSegment from '../Shared/ErrorSegment'; const Searches = () => { - const [{ connected, connecting, connectError } , setConnected] = useState({ connected: false, connecting: true, connectError: false }); - const [{ creating, createError }, setCreating] = useState({ creating: false, createError: false }); + const [connecting, setConnecting] = useState(true); const [error, setError] = useState(undefined); const [searches, setSearches] = useState({}); + + const [removing, setRemoving] = useState(false); + const [stopping, setStopping] = useState(false); + const [creating, setCreating] = useState(false); + const inputRef = useRef(); - const searchRef = useRef(); const { id: searchId } = useParams(); const history = useHistory(); const match = useRouteMatch(); - const onConnecting = () => setConnected({ connected: false, connecting: true, connectError: false }) - const onConnected = () => setConnected({ connected: true, connecting: false, connectError: false }); - const onConnectionError = (error) => setConnected({ connected: false, connecting: false, connectError: error }) + const onConnecting = () => { setConnecting(true); } + const onConnected = () => { setConnecting(false); setError(undefined); } + const onConnectionError = (error) => { setConnecting(false); setError(error); } const onUpdate = (update) => { onConnected(); @@ -47,7 +49,6 @@ const Searches = () => { const searchHub = createSearchHubConnection(); searchHub.on('list', searches => { - console.log('list of searches received', searches); onUpdate(searches.reduce((acc, search) => { acc[search.id] = search; return acc; @@ -56,7 +57,6 @@ const Searches = () => { }) searchHub.on('update', search => { - console.log(search.responses) onUpdate(old => ({ ...old, [search.id]: search })); }); @@ -75,10 +75,8 @@ const Searches = () => { const connect = async () => { try { - console.log('connecting to search hub'); onConnecting(); await searchHub.start(); - console.log('search hub connected'); } catch (error) { onConnectionError(error?.message ?? 'Failed to connect') } @@ -87,65 +85,85 @@ const Searches = () => { connect(); return () => { - console.log('stopping search hub'); searchHub.stop(); - console.log('search hub stopped'); } }, []); + // create a new search, and optionally navigate to it to display the details + // we do this if the user clicks the search icon, or repeats an existing search const create = async ({ search, navigate = false } = {}) => { const searchText = search || inputRef.current.inputRef.current.value; const id = uuidv4(); try { - setCreating({ creating: true, createError: false }) + setCreating(true) await lib.create({ id, searchText }) - setCreating({ creating: false, createError: false }) - + try { inputRef.current.inputRef.current.value = ''; } catch { - // no-op + // we are probably repeating an existing search; the input isn't mounted. no-op. } + + setCreating(false) if (navigate) { - history.push(`/searches/${id}`) + history.push(`${match.url.replace(`/${searchId}`, '')}/${id}`); } } catch (error) { console.error(error) - setCreating({ creating: false, createError: error.message }) + setCreating(false) } } + // delete a search const remove = async (search) => { try { + setRemoving(true); + await lib.remove({ id: search.id }) setSearches(old => { delete old[search.id]; return { ...old } }); + + setRemoving(false); } catch (err) { - setError(error?.message ?? error); + console.error(err); + setRemoving(false); } }; + // stop an in-progress search const stop = async (search) => { - await lib.stop({ id: search.id }) + try { + setStopping(true); + await lib.stop({ id: search.id }) + setStopping(false); + } catch (error) { + console.error(error); + setStopping(false); + } } if (connecting) { return <LoaderSegment/> } - if (connectError) { - return <ErrorSegment caption={connectError}/>; + if (error) { + return <ErrorSegment caption={error?.message ?? error}/>; } + // if searchId is not null, there's an id in the route. + // display the details for the search, if there is one if (searchId) { if (searches[searchId]) { return ( <SearchDetail search={searches[searchId]} + creating={creating} + stopping={stopping} + removing={removing} onCreate={create} onStop={stop} onRemove={remove} @@ -153,9 +171,9 @@ const Searches = () => { ); } - return ( - <ErrorSegment caption='Invalid Search ID'/> - ); + // if the searchId doesn't match a search we know about, chop + // the id off of the url and force navigation back to the list + history.replace(match.url.replace(`/${searchId}`, '')); } return ( |
