diff --git a/packages/grafana-data/src/types/pluginExtensions.ts b/packages/grafana-data/src/types/pluginExtensions.ts index 796c4c6e24b..0d1fc7ff143 100644 --- a/packages/grafana-data/src/types/pluginExtensions.ts +++ b/packages/grafana-data/src/types/pluginExtensions.ts @@ -1,3 +1,5 @@ +import { DataQuery } from '@grafana/schema'; + import { RawTimeRange, TimeZone } from './time'; // Plugin Extensions types @@ -73,7 +75,7 @@ export type PluginExtensionPanelContext = { timeRange: RawTimeRange; timeZone: TimeZone; dashboard: Dashboard; - targets: Target[]; + targets: DataQuery[]; }; type Dashboard = { @@ -81,8 +83,3 @@ type Dashboard = { title: string; tags: string[]; }; - -type Target = { - pluginId: string; - refId: string; -}; diff --git a/public/app/features/dashboard/utils/getPanelMenu.test.ts b/public/app/features/dashboard/utils/getPanelMenu.test.ts index 41c0011959c..391f4bf82c1 100644 --- a/public/app/features/dashboard/utils/getPanelMenu.test.ts +++ b/public/app/features/dashboard/utils/getPanelMenu.test.ts @@ -228,7 +228,9 @@ describe('getPanelMenu()', () => { targets: [ { refId: 'A', - pluginId: 'testdata', + datasource: { + type: 'testdata', + }, }, ], dashboard: { diff --git a/public/app/features/dashboard/utils/getPanelMenu.ts b/public/app/features/dashboard/utils/getPanelMenu.ts index c8b7dea7086..405e971c6a0 100644 --- a/public/app/features/dashboard/utils/getPanelMenu.ts +++ b/public/app/features/dashboard/utils/getPanelMenu.ts @@ -332,16 +332,13 @@ function createExtensionContext(panel: PanelModel, dashboard: DashboardModel): P id: panel.id, pluginId: panel.type, title: panel.title, - timeRange: Object.assign({}, dashboard.time), + timeRange: dashboard.time, timeZone: dashboard.timezone, dashboard: { uid: dashboard.uid, title: dashboard.title, tags: Array.from(dashboard.tags), }, - targets: panel.targets.map((t) => ({ - refId: t.refId, - pluginId: t.datasource?.type ?? 'unknown', - })), + targets: panel.targets, }; } diff --git a/public/app/features/plugins/extensions/getPluginExtensions.test.ts b/public/app/features/plugins/extensions/getPluginExtensions.test.ts index d68b0e1f130..54d5b63b838 100644 --- a/public/app/features/plugins/extensions/getPluginExtensions.test.ts +++ b/public/app/features/plugins/extensions/getPluginExtensions.test.ts @@ -246,4 +246,43 @@ describe('getPluginExtensions()', () => { expect(global.console.warn).toHaveBeenCalledTimes(1); expect(global.console.warn).toHaveBeenCalledWith('[Plugin Extensions] Something went wrong!'); }); + + test('should pass a frozen copy of the context to the onClick() function', () => { + const context = { title: 'New title from the context!' }; + + link2.path = undefined; + link2.onClick = jest.fn(); + + const registry = createPluginExtensionRegistry([{ pluginId, extensionConfigs: [link2] }]); + const { extensions } = getPluginExtensions({ registry, context, extensionPointId: extensionPoint2 }); + const [extension] = extensions; + + assertPluginExtensionLink(extension); + extension.onClick?.({} as React.MouseEvent); + + const helpers = (link2.onClick as jest.Mock).mock.calls[0][1]; + + expect(link2.configure).toHaveBeenCalledTimes(1); + expect(Object.isFrozen(helpers.context)).toBe(true); + expect(() => { + helpers.context.title = 'New title'; + }).toThrow(); + }); + + test('should should not freeze the original context', () => { + const context = { + title: 'New title from the context!', + nested: { title: 'title' }, + array: ['a'], + }; + + const registry = createPluginExtensionRegistry([{ pluginId, extensionConfigs: [link2] }]); + getPluginExtensions({ registry, context, extensionPointId: extensionPoint2 }); + + expect(() => { + context.title = 'Updating the title'; + context.nested.title = 'new title'; + context.array.push('b'); + }).not.toThrow(); + }); });