From d042b06e8b57618045776bb3d0e4486902f420a6 Mon Sep 17 00:00:00 2001 From: Roland Date: Mon, 20 Nov 2023 11:07:19 -0700 Subject: [PATCH] Debouncing search estimate. --- app/src/components/Search.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/app/src/components/Search.js b/app/src/components/Search.js index 66997794..4244271a 100644 --- a/app/src/components/Search.js +++ b/app/src/components/Search.js @@ -5,6 +5,7 @@ import {usePages, useQuery} from "../hooks/customHooks"; import {ZimSearchView} from "./Zim"; import {searchEstimate, searchSuggestions} from "../api"; import {normalizeEstimate, TabLinks} from "./Common"; +import _ from "lodash"; export function useSearchSuggestions() { const {searchParams} = useQuery(); @@ -28,11 +29,14 @@ export function useSearchSuggestions() { }) }, }; + console.debug('newSuggestions', newSuggestions); setSuggestions(newSuggestions); } catch (e) { console.error(e); console.error('Failed to get search suggestions'); } + } else { + setSuggestions(null); } } @@ -127,8 +131,12 @@ export const useSearchEstimate = (search_str, activeTags) => { } } + const debouncedEstimate = _.debounce(async () => await localFetchSearchEstimate(), 800); + useEffect(() => { - localFetchSearchEstimate(); + debouncedEstimate(); + + return () => debouncedEstimate.cancel(); }, [search_str, JSON.stringify(activeTags)]); return {fileGroups, zimsSum, zims}