Reworking useLatestRequest to actually debounce before fetching. Modifying search to display loading message when results are pending.

This commit is contained in:
Roland 2023-11-22 10:35:01 -07:00
parent dd3fad963c
commit 94a2c50531
No known key found for this signature in database
GPG key ID: 9D6DBD3ACF11AB93
3 changed files with 23 additions and 14 deletions

View file

@ -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}

View file

@ -34,14 +34,10 @@ export function useSearchSuggestions() {
const normalizeSuggestions = (newSuggestions) => {
// Convert the suggestions from the Backend to what the Semantic <Search> 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);

View file

@ -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);