diff --git a/public/app/features/commandPalette/KBarResults.tsx b/public/app/features/commandPalette/KBarResults.tsx index 7dc0fa1409a..af01165f0b4 100644 --- a/public/app/features/commandPalette/KBarResults.tsx +++ b/public/app/features/commandPalette/KBarResults.tsx @@ -3,6 +3,8 @@ import { usePointerMovedSinceMount } from 'kbar/lib/utils'; import * as React from 'react'; import { useVirtual } from 'react-virtual'; +import { URLCallback } from './types'; + // From https://github.com/timc1/kbar/blob/main/src/KBarResults.tsx // TODO: Go back to KBarResults from kbar when https://github.com/timc1/kbar/issues/281 is fixed // Remember to remove dependency on react-virtual when removing this file @@ -157,7 +159,7 @@ export const KBarResults = (props: KBarResultsProps) => { {rowVirtualizer.virtualItems.map((virtualRow) => { // eslint-disable-next-line @typescript-eslint/consistent-type-assertions const item = itemsRef.current[virtualRow.index] as ActionImpl & { - url?: string; + url?: string | URLCallback; target?: React.HTMLAttributeAnchorTarget; }; @@ -202,7 +204,7 @@ export const KBarResults = (props: KBarResultsProps) => { return ( ) : null} diff --git a/public/app/features/commandPalette/actions/staticActions.ts b/public/app/features/commandPalette/actions/staticActions.ts index ed93b54d5e9..3f31f872562 100644 --- a/public/app/features/commandPalette/actions/staticActions.ts +++ b/public/app/features/commandPalette/actions/staticActions.ts @@ -29,6 +29,14 @@ function navTreeToActions(navTree: NavModelItem[], parents: NavModelItem[] = []) continue; } + let urlOrCallback: CommandPaletteAction['url'] = url; + if (url && navItem.id === 'connections-add-new-connection') { + urlOrCallback = (searchQuery: string) => { + const matchingKeyword = keywords?.find((keyword) => keyword.toLowerCase().includes(searchQuery.toLowerCase())); + return matchingKeyword ? `${url}?search=${matchingKeyword}` : url; + }; + } + const section = isCreateAction ? t('command-palette.section.actions', 'Actions') : t('command-palette.section.pages', 'Pages'); @@ -39,13 +47,13 @@ function navTreeToActions(navTree: NavModelItem[], parents: NavModelItem[] = []) const action: CommandPaletteAction = { id: idForNavItem(navItem), name: text, - section: section, - url, + section, + url: urlOrCallback, target, parent: parents.length > 0 && !isCreateAction ? idForNavItem(parents[parents.length - 1]) : undefined, perform: onClick, keywords: keywords?.join(' '), - priority: priority, + priority, subtitle: isCreateAction ? undefined : subtitle, }; diff --git a/public/app/features/commandPalette/types.ts b/public/app/features/commandPalette/types.ts index 564d691928d..20f66a8d9f2 100644 --- a/public/app/features/commandPalette/types.ts +++ b/public/app/features/commandPalette/types.ts @@ -6,16 +6,18 @@ type NotNullable = Exclude; // Parent actions require a section, but not child actions export type CommandPaletteAction = RootCommandPaletteAction | ChildCommandPaletteAction; +export type URLCallback = (searchQuery: string) => string; + type RootCommandPaletteAction = Omit & { section: NotNullable; priority: NotNullable; target?: React.HTMLAttributeAnchorTarget; - url?: string; + url?: string | URLCallback; }; type ChildCommandPaletteAction = Action & { parent: NotNullable; priority: NotNullable; target?: React.HTMLAttributeAnchorTarget; - url?: string; + url?: string | URLCallback; };