diff --git a/public/app/features/commandPalette/actions/dashboardActions.ts b/public/app/features/commandPalette/actions/dashboardActions.ts index 69016f4d80e..a46c4443859 100644 --- a/public/app/features/commandPalette/actions/dashboardActions.ts +++ b/public/app/features/commandPalette/actions/dashboardActions.ts @@ -1,5 +1,5 @@ import debounce from 'debounce-promise'; -import { useEffect, useState } from 'react'; +import { useEffect, useRef, useState } from 'react'; import { locationUtil } from '@grafana/data'; import { config } from '@grafana/runtime'; @@ -83,17 +83,27 @@ export async function getSearchResultActions(searchQuery: string): Promise([]); const [isFetchingSearchResults, setIsFetchingSearchResults] = useState(false); + const lastRequestTimestamp = useRef(); // Hit dashboards API useEffect(() => { + const timestamp = Date.now(); if (isShowing && searchQuery.length > 0) { setIsFetchingSearchResults(true); debouncedSearch(searchQuery).then((resultActions) => { - setSearchResults(resultActions); - setIsFetchingSearchResults(false); + // Only update the state if this is the most recent request + // 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 { setSearchResults([]); + setIsFetchingSearchResults(false); + lastRequestTimestamp.current = timestamp; } }, [isShowing, searchQuery]); diff --git a/public/app/features/commandPalette/actions/useActions.ts b/public/app/features/commandPalette/actions/useActions.ts index 15f5547266f..a3895f694ca 100644 --- a/public/app/features/commandPalette/actions/useActions.ts +++ b/public/app/features/commandPalette/actions/useActions.ts @@ -30,10 +30,8 @@ export default function useActions(searchQuery: string) { .catch((err) => { console.error('Error loading recent dashboard actions', err); }); - } else { - setRecentDashboardActions([]); } }, [searchQuery]); - return [...recentDashboardActions, ...navTreeActions]; + return searchQuery ? navTreeActions : [...recentDashboardActions, ...navTreeActions]; }