From 041f5c58f08c23d9b04847c9e2ac88abc4399083 Mon Sep 17 00:00:00 2001 From: Damien de Lemeny Date: Tue, 7 May 2024 20:59:39 -0500 Subject: [PATCH] Don't fetch field_caps on TimeRange update spam --- src/datasource/utils.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/datasource/utils.ts b/src/datasource/utils.ts index e0491a6..e07ba7b 100644 --- a/src/datasource/utils.ts +++ b/src/datasource/utils.ts @@ -18,11 +18,20 @@ export type Suggestion = { export function useDatasourceFields(datasource: BaseQuickwitDataSource, range: TimeRange) { const [fields, setFields] = useState([]); + const [niceRange, setNiceRange] = useState(()=>range) + + useEffect(() => { + // range may change several times during a render with a delta of a few hundred milliseconds + // we don't need to fetch with such a granularity, this effect filters out range updates that are within the same minute + if (range.from.isSame(niceRange.from, 'minute') && range.to.isSame(niceRange.to, 'minute')) { return } + setNiceRange(range) + },[range, niceRange]) + useEffect(() => { if (datasource.getTagKeys) { - datasource.getTagKeys({ searchable: true, timeRange: range}).then(setFields); + datasource.getTagKeys({ searchable: true, timeRange: niceRange}).then(setFields); } - }, [datasource, range, setFields]); + }, [datasource, niceRange]) const getSuggestions = useCallback(async (word: string): Promise => { let suggestions: Suggestion = { from: 0, options: [] };