diff --git a/app/src/components/Common.js b/app/src/components/Common.js index e55c23c7..ea2963a0 100644 --- a/app/src/components/Common.js +++ b/app/src/components/Common.js @@ -514,6 +514,9 @@ export function SearchResultsInput({ handleChange, } = useClearableInput(searchStr, onChange, onClear, onSubmit, size, placeholder); + // Show a "Loading" message rather than "No results" while results are pending. + const loadingResults = {'loading': {name: 'Loading', results: [{title: 'Results are pending...'}]}}; + const localHandleResultSelect = (e, data) => { if (e) { e.preventDefault(); @@ -535,7 +538,7 @@ export function SearchResultsInput({ onResultSelect={localHandleResultSelect} value={value} size={size} - results={results} + results={!loading && results ? results : loadingResults} resultRenderer={resultRenderer} className='search-input' loading={loading} diff --git a/app/src/components/Search.js b/app/src/components/Search.js index db178f76..f767f6d8 100644 --- a/app/src/components/Search.js +++ b/app/src/components/Search.js @@ -34,14 +34,10 @@ export function useSearchSuggestions() { const normalizeSuggestions = (newSuggestions) => { // Convert the suggestions from the Backend to what the Semantic expects. const lowerSearchStr = searchStr.toLowerCase(); - const matchingApps = SUGGESTED_APPS.filter(i => - i.title.toLowerCase().includes(lowerSearchStr) - || fuzzyMatch(i.title.toLowerCase(), lowerSearchStr)); - const zimSum = newSuggestions.zimsEstimates.reduce((i, j) => i + j['estimate'], 0).toString(); + let matchingSuggestions = {}; // Suggested results are ordered. - let matchingSuggestions = {}; if (newSuggestions.fileGroups > 0) { matchingSuggestions.fileGroups = { name: 'Files', results: [ @@ -53,6 +49,8 @@ export function useSearchSuggestions() { ] }; } + + const zimSum = newSuggestions.zimsEstimates.reduce((i, j) => i + j['estimate'], 0).toString(); if (zimSum > 0) { matchingSuggestions.zimsSum = { name: 'Zims', results: [ @@ -81,12 +79,18 @@ export function useSearchSuggestions() { } } - matchingSuggestions.tags = { - name: 'Tags', results: fuzzyMatchTagsByName(searchStr).map(i => { - return {type: 'tag', title: i.name, location: `/search?tag=${encodeURIComponent(i.title)}`} - }) + const matchingTags = fuzzyMatchTagsByName(searchStr); + if (matchingTags && matchingTags.length > 0) { + matchingSuggestions.tags = { + name: 'Tags', results: matchingTags.map(i => { + return {type: 'tag', title: i.name, location: `/search?tag=${encodeURIComponent(i.title)}`} + }) + } } + const matchingApps = SUGGESTED_APPS.filter(i => + i.title.toLowerCase().includes(lowerSearchStr) + || fuzzyMatch(i.title.toLowerCase(), lowerSearchStr)); if (matchingApps && matchingApps.length > 0) { matchingSuggestions.apps = {name: 'Apps', results: matchingApps}; } @@ -106,9 +110,10 @@ export function useSearchSuggestions() { console.debug('Not getting suggestions because there is no search.'); return; } + setSuggestions(undefined); // Use the useLatestRequest to handle user typing. - sendRequest(searchSuggestions(searchStr)); + sendRequest(async () => await searchSuggestions(searchStr)); }, [searchStr, sendRequest]); const handleResultSelect = ({result}) => navigate(result.location); diff --git a/app/src/hooks/customHooks.js b/app/src/hooks/customHooks.js index 3b7fd662..3b32020f 100644 --- a/app/src/hooks/customHooks.js +++ b/app/src/hooks/customHooks.js @@ -64,14 +64,15 @@ export const useRecurringTimeout = (callback, delay) => { } export const useLatestRequest = (delay = 300) => { - // A hook which cancels older requests and will only set `data` to the latest response's data. + // A hook which ignores older requests and will only set `data` to the latest response's data. + // usage: sendRequest(async () => await yourAPICall(...args)); const [data, setData] = React.useState(null); const latestRequestRef = React.useRef(0); const debounceTimerRef = React.useRef(null); const [loading, setLoading] = React.useState(false); - const sendRequest = React.useCallback((fetchCoroutine) => { + const sendRequest = React.useCallback((fetchFunction) => { if (debounceTimerRef.current) { clearTimeout(debounceTimerRef.current); } @@ -81,7 +82,7 @@ export const useLatestRequest = (delay = 300) => { const requestId = ++latestRequestRef.current; try { - const result = await fetchCoroutine; + const result = await fetchFunction(); if (requestId === latestRequestRef.current) { setData(result);