From b8bf146c78e26dd7d8a9fc849b508434b5a2a3e2 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 17 May 2022 10:00:31 -0500 Subject: clean up and document --- .../src/components/Search/Detail/SearchDetail.js | 50 +++++++++++++--- .../components/Search/Detail/SearchDetailHeader.js | 22 ++++--- src/web/src/components/Search/Search.css | 3 +- src/web/src/components/Search/Searches.js | 70 ++++++++++++++-------- 4 files changed, 102 insertions(+), 43 deletions(-) (limited to 'src') 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 () @@ -96,10 +128,14 @@ const SearchDetail = ({ search, onCreate, onStop, onRemove }) => { <> { } loading={loading && } > - {(results && results.length > 0) && + {loaded && { /> } - {sortedAndFilteredResults.slice(0, displayCount).map((r, i) => + {loaded && sortedAndFilteredResults.slice(0, displayCount).map((r, i) => { isInitiallyFolded={foldResults} /> )} - {remainingCount > 0 ? + {loaded && (remainingCount > 0 ? : filteredCount > 0 ? - : ''} + : '')} ) 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 && - const StopOrDeleteButton = ({ ...props }) => + const StopOrDeleteButton = () => + // 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 ( <> @@ -55,7 +61,7 @@ const SearchDetailHeader = ({ search, loading, onCreate, onStop, onRemove }) => } - {isTinyScreen && + {isTinyScreen && } 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 } - if (connectError) { - return ; + if (error) { + return ; } + // 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 ( { ); } - return ( - - ); + // 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 ( -- cgit v1.2.3