Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VizPanel: Harden plugin change handling #902

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion packages/scenes/src/components/VizPanel/VizPanel.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { SeriesVisibilityChangeMode } from '@grafana/ui';
import { SceneTimeRange } from '../../core/SceneTimeRange';
import { act, render, screen } from '@testing-library/react';
import { RefreshEvent } from '@grafana/runtime';
import { SceneDeactivationHandler } from '../../core/types';

let pluginToLoad: PanelPlugin | undefined;

Expand Down Expand Up @@ -289,6 +290,7 @@ describe('VizPanel', () => {

describe('When changing plugin', () => {
let panel: VizPanel<OptionsPlugin1, FieldConfigPlugin1>;
let deactivate: SceneDeactivationHandler;

beforeEach(async () => {
panel = new VizPanel<OptionsPlugin1, FieldConfigPlugin1>({
Expand All @@ -300,7 +302,11 @@ describe('VizPanel', () => {
});

pluginToLoad = getTestPlugin1();
panel.activate();
deactivate = panel.activate();
});

afterEach(() => {
deactivate();
});

it('Should successfully change from one viz type to another', async () => {
Expand Down Expand Up @@ -365,6 +371,18 @@ describe('VizPanel', () => {
expect(panel.state.options.showThresholds).toBe(true);
expect(panel.state.options.option2).toBe('hello');
});

it('Should detect plugin change on activation', async () => {
pluginToLoad = getTestPlugin2();
deactivate();

panel.setState({ pluginId: pluginToLoad.meta.id });
deactivate = panel.activate();

await Promise.resolve();

expect(panel.getPlugin()).toBe(pluginToLoad);
});
});

describe('getLegacyPanelId', () => {
Expand Down
30 changes: 20 additions & 10 deletions packages/scenes/src/components/VizPanel/VizPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,17 @@ export class VizPanel<TOptions = {}, TFieldConfig extends {} = {}> extends Scene
}

private _onActivate() {
if (!this._plugin) {
if (!this._plugin || this.state.pluginId !== this._plugin.meta.id) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ivanortegaalba this code did not check if the plugin id had changed

this._loadPlugin(this.state.pluginId);
}
}

private async _loadPlugin(pluginId: string, overwriteOptions?: DeepPartial<{}>, overwriteFieldConfig?: FieldConfigSource, isAfterPluginChange?: boolean) {
private async _loadPlugin(
pluginId: string,
overwriteOptions?: DeepPartial<{}>,
overwriteFieldConfig?: FieldConfigSource,
isAfterPluginChange?: boolean
) {
const plugin = loadPanelPluginSync(pluginId);

if (plugin) {
Expand Down Expand Up @@ -157,7 +162,12 @@ export class VizPanel<TOptions = {}, TFieldConfig extends {} = {}> extends Scene
return panelId;
}

private async _pluginLoaded(plugin: PanelPlugin, overwriteOptions?: DeepPartial<{}>, overwriteFieldConfig?: FieldConfigSource, isAfterPluginChange?: boolean) {
private async _pluginLoaded(
plugin: PanelPlugin,
overwriteOptions?: DeepPartial<{}>,
overwriteFieldConfig?: FieldConfigSource,
isAfterPluginChange?: boolean
) {
const { options, fieldConfig, title, pluginVersion, _UNSAFE_customMigrationHandler } = this.state;

const panel: PanelModel = {
Expand Down Expand Up @@ -217,6 +227,10 @@ export class VizPanel<TOptions = {}, TFieldConfig extends {} = {}> extends Scene
return this._plugin;
}

public getPluginAsync(): PanelPlugin | undefined {
return this._plugin;
}

public getPanelContext(): PanelContext {
this._panelContext ??= this.buildPanelContext();

Expand Down Expand Up @@ -257,11 +271,7 @@ export class VizPanel<TOptions = {}, TFieldConfig extends {} = {}> extends Scene
};

public async changePluginType(pluginId: string, newOptions?: DeepPartial<{}>, newFieldConfig?: FieldConfigSource) {
const {
options: prevOptions,
fieldConfig: prevFieldConfig,
pluginId: prevPluginId,
} = this.state;
const { options: prevOptions, fieldConfig: prevFieldConfig, pluginId: prevPluginId } = this.state;

//clear field config cache to update it later
this._dataWithFieldConfig = undefined;
Expand All @@ -276,8 +286,8 @@ export class VizPanel<TOptions = {}, TFieldConfig extends {} = {}> extends Scene
type: pluginId,
};

// onPanelTypeChanged is mainly used by plugins to migrate from Angular to React.
// For example, this will migrate options from 'graph' to 'timeseries' if the previous and new plugin ID matches.
// onPanelTypeChanged is mainly used by plugins to migrate from Angular to React.
// For example, this will migrate options from 'graph' to 'timeseries' if the previous and new plugin ID matches.
const updatedOptions = this._plugin?.onPanelTypeChanged?.(panel, prevPluginId, prevOptions, prevFieldConfig);

if (updatedOptions && !isEmpty(updatedOptions)) {
Expand Down
Loading