Skip to content

Commit f040a83

Browse files
sd2kmckn
andauthored
Plugins: Add Command Palette extension point (#78098)
Co-authored-by: Marcus Andersson <[email protected]>
1 parent 69784ef commit f040a83

File tree

8 files changed

+43
-3
lines changed

8 files changed

+43
-3
lines changed

packages/grafana-data/src/types/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,6 @@ export {
6363
type PluginExtensionEventHelpers,
6464
type PluginExtensionPanelContext,
6565
type PluginExtensionDataSourceConfigContext,
66+
type PluginExtensionCommandPaletteContext,
6667
type PluginExtensionOpenModalOptions,
6768
} from './pluginExtensions';

packages/grafana-data/src/types/pluginExtensions.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ export type PluginExtensionEventHelpers<Context extends object = object> = {
118118
// Extension Points available in core Grafana
119119
export enum PluginExtensionPoints {
120120
AlertInstanceAction = 'grafana/alerting/instance/action',
121+
CommandPalette = 'grafana/commandpalette/action',
121122
DashboardPanelMenu = 'grafana/dashboard/panel/menu',
122123
DataSourceConfig = 'grafana/datasources/config',
123124
ExploreToolbarAction = 'grafana/explore/toolbar/action',
@@ -154,6 +155,8 @@ export type PluginExtensionDataSourceConfigContext<JsonData extends DataSourceJs
154155
setJsonData: (jsonData: JsonData) => void;
155156
};
156157

158+
export type PluginExtensionCommandPaletteContext = {};
159+
157160
type Dashboard = {
158161
uid: string;
159162
title: string;

public/app/core/components/AppChrome/AppChrome.test.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ import { Page } from '../Page/Page';
1414

1515
import { AppChrome } from './AppChrome';
1616

17+
jest.mock('@grafana/runtime', () => ({
18+
...jest.requireActual('@grafana/runtime'),
19+
getPluginLinkExtensions: jest.fn().mockReturnValue({ extensions: [] }),
20+
}));
21+
1722
const pageNav: NavModelItem = {
1823
text: 'pageNav title',
1924
children: [

public/app/features/commandPalette/actions/dashboardActions.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ describe('dashboardActions', () => {
8787
{
8888
id: 'recent-dashboards/my-dashboard-1',
8989
name: 'My dashboard 1',
90-
priority: 5,
90+
priority: 6,
9191
section: 'Recent dashboards',
9292
url: '/my-dashboard-1',
9393
},
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { PluginExtensionCommandPaletteContext, PluginExtensionPoints } from '@grafana/data';
2+
import { getPluginLinkExtensions } from '@grafana/runtime';
3+
4+
import { CommandPaletteAction } from '../types';
5+
import { EXTENSIONS_PRIORITY } from '../values';
6+
7+
export default function getExtensionActions(): CommandPaletteAction[] {
8+
const context: PluginExtensionCommandPaletteContext = {};
9+
const { extensions } = getPluginLinkExtensions({
10+
extensionPointId: PluginExtensionPoints.CommandPalette,
11+
context,
12+
limitPerPlugin: 3,
13+
});
14+
return extensions.map((extension) => ({
15+
section: extension.category ?? 'Extensions',
16+
priority: EXTENSIONS_PRIORITY,
17+
id: extension.id,
18+
name: extension.title,
19+
target: extension.path,
20+
perform: () => extension.onClick && extension.onClick(),
21+
}));
22+
}

public/app/features/commandPalette/actions/staticActions.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { changeTheme } from 'app/core/services/theme';
66
import { CommandPaletteAction } from '../types';
77
import { ACTIONS_PRIORITY, DEFAULT_PRIORITY, PREFERENCES_PRIORITY } from '../values';
88

9+
import getExtensionActions from './extensionActions';
10+
911
// TODO: Clean this once ID is mandatory on nav items
1012
function idForNavItem(navItem: NavModelItem) {
1113
return 'navModel.' + navItem.id ?? navItem.url ?? navItem.text ?? navItem.subTitle;
@@ -84,7 +86,8 @@ export default (navBarTree: NavModelItem[]): CommandPaletteAction[] => {
8486
},
8587
];
8688

89+
const extensionActions = getExtensionActions();
8790
const navBarActions = navTreeToActions(navBarTree);
8891

89-
return [...globalActions, ...navBarActions];
92+
return [...globalActions, ...extensionActions, ...navBarActions];
9093
};

public/app/features/commandPalette/values.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
export const RECENT_DASHBOARDS_PRORITY = 5;
1+
export const RECENT_DASHBOARDS_PRORITY = 6;
2+
export const EXTENSIONS_PRIORITY = 5;
23
export const ACTIONS_PRIORITY = 4;
34
export const DEFAULT_PRIORITY = 3;
45
export const PREFERENCES_PRIORITY = 2;

public/app/features/dashboard/containers/DashboardPage.test.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ jest.mock('app/core/core', () => ({
6666
},
6767
}));
6868

69+
jest.mock('@grafana/runtime', () => ({
70+
...jest.requireActual('@grafana/runtime'),
71+
getPluginLinkExtensions: jest.fn().mockReturnValue({ extensions: [] }),
72+
}));
73+
6974
jest.mock('react-virtualized-auto-sizer', () => {
7075
// The size of the children need to be small enough to be outside the view.
7176
// So it does not trigger the query to be run by the PanelQueryRunner.

0 commit comments

Comments
 (0)