mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Command Palette: Prevent stale search results from overwriting newer results (#68377)
* only update the state if this is the most recent request * fix empty state as well * improve perf of recent dashboards
This commit is contained in:
parent
ede8df846e
commit
446885bd1a
@ -1,5 +1,5 @@
|
|||||||
import debounce from 'debounce-promise';
|
import debounce from 'debounce-promise';
|
||||||
import { useEffect, useState } from 'react';
|
import { useEffect, useRef, useState } from 'react';
|
||||||
|
|
||||||
import { locationUtil } from '@grafana/data';
|
import { locationUtil } from '@grafana/data';
|
||||||
import { config } from '@grafana/runtime';
|
import { config } from '@grafana/runtime';
|
||||||
@ -83,17 +83,27 @@ export async function getSearchResultActions(searchQuery: string): Promise<Comma
|
|||||||
export function useSearchResults(searchQuery: string, isShowing: boolean) {
|
export function useSearchResults(searchQuery: string, isShowing: boolean) {
|
||||||
const [searchResults, setSearchResults] = useState<CommandPaletteAction[]>([]);
|
const [searchResults, setSearchResults] = useState<CommandPaletteAction[]>([]);
|
||||||
const [isFetchingSearchResults, setIsFetchingSearchResults] = useState(false);
|
const [isFetchingSearchResults, setIsFetchingSearchResults] = useState(false);
|
||||||
|
const lastRequestTimestamp = useRef<number>();
|
||||||
|
|
||||||
// Hit dashboards API
|
// Hit dashboards API
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
const timestamp = Date.now();
|
||||||
if (isShowing && searchQuery.length > 0) {
|
if (isShowing && searchQuery.length > 0) {
|
||||||
setIsFetchingSearchResults(true);
|
setIsFetchingSearchResults(true);
|
||||||
debouncedSearch(searchQuery).then((resultActions) => {
|
debouncedSearch(searchQuery).then((resultActions) => {
|
||||||
setSearchResults(resultActions);
|
// Only update the state if this is the most recent request
|
||||||
setIsFetchingSearchResults(false);
|
// We don't need to worry about clearing the isFetching state either
|
||||||
|
// If there's a later request in progress, this will clear it for us
|
||||||
|
if (!lastRequestTimestamp.current || timestamp > lastRequestTimestamp.current) {
|
||||||
|
setSearchResults(resultActions);
|
||||||
|
setIsFetchingSearchResults(false);
|
||||||
|
lastRequestTimestamp.current = timestamp;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
setSearchResults([]);
|
setSearchResults([]);
|
||||||
|
setIsFetchingSearchResults(false);
|
||||||
|
lastRequestTimestamp.current = timestamp;
|
||||||
}
|
}
|
||||||
}, [isShowing, searchQuery]);
|
}, [isShowing, searchQuery]);
|
||||||
|
|
||||||
|
@ -30,10 +30,8 @@ export default function useActions(searchQuery: string) {
|
|||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
console.error('Error loading recent dashboard actions', err);
|
console.error('Error loading recent dashboard actions', err);
|
||||||
});
|
});
|
||||||
} else {
|
|
||||||
setRecentDashboardActions([]);
|
|
||||||
}
|
}
|
||||||
}, [searchQuery]);
|
}, [searchQuery]);
|
||||||
|
|
||||||
return [...recentDashboardActions, ...navTreeActions];
|
return searchQuery ? navTreeActions : [...recentDashboardActions, ...navTreeActions];
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user