From c6db57c7d0671fe795589d049b1938bdd1918132 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Thu, 22 Dec 2022 08:19:43 +0100 Subject: [PATCH] Scene: Exploring typed scene panels (#58168) * Scene: Exploring typed scene panels * Updated * minor rename --- .../scenes/components/VizPanel/VizPanel.tsx | 13 ++++---- .../components/VizPanel/panelBuilders.ts | 32 +++++++++++++++++++ public/app/features/scenes/scenes/demo.tsx | 24 ++++++++------ 3 files changed, 53 insertions(+), 16 deletions(-) create mode 100644 public/app/features/scenes/components/VizPanel/panelBuilders.ts diff --git a/public/app/features/scenes/components/VizPanel/VizPanel.tsx b/public/app/features/scenes/components/VizPanel/VizPanel.tsx index f028216b986..22d6b453a08 100644 --- a/public/app/features/scenes/components/VizPanel/VizPanel.tsx +++ b/public/app/features/scenes/components/VizPanel/VizPanel.tsx @@ -1,3 +1,4 @@ +import { DeepPartial } from '@reduxjs/toolkit'; import React from 'react'; import { AbsoluteTimeRange, FieldConfigSource, PanelModel, PanelPlugin, toUtc } from '@grafana/data'; @@ -15,23 +16,21 @@ import { VizPanelRenderer } from './VizPanelRenderer'; export interface VizPanelState extends SceneLayoutChildState { title: string; pluginId: string; - options: TOptions; - fieldConfig: FieldConfigSource; + options: DeepPartial; + fieldConfig: FieldConfigSource>; pluginVersion?: string; // internal state pluginLoadError?: string; } -export class VizPanel extends SceneObjectBase< - VizPanelState, TFieldConfig> -> { +export class VizPanel extends SceneObjectBase> { public static Component = VizPanelRenderer; public static Editor = VizPanelEditor; // Not part of state as this is not serializable private _plugin?: PanelPlugin; - public constructor(state: Partial, TFieldConfig>>) { + public constructor(state: Partial>) { super({ options: {}, fieldConfig: { defaults: {}, overrides: [] }, @@ -109,7 +108,7 @@ export class VizPanel extends SceneObjectBase< this.setState({ options }); }; - public onFieldConfigChange = (fieldConfig: FieldConfigSource) => { + public onFieldConfigChange = (fieldConfig: FieldConfigSource) => { this.setState({ fieldConfig }); }; } diff --git a/public/app/features/scenes/components/VizPanel/panelBuilders.ts b/public/app/features/scenes/components/VizPanel/panelBuilders.ts new file mode 100644 index 00000000000..5fb453715c2 --- /dev/null +++ b/public/app/features/scenes/components/VizPanel/panelBuilders.ts @@ -0,0 +1,32 @@ +import { GraphFieldConfig, TableFieldOptions } from '@grafana/schema'; +import { PanelOptions as BarGaugePanelOptions } from 'app/plugins/panel/bargauge/models.gen'; +import { PanelOptions as TablePanelOptions } from 'app/plugins/panel/table/models.gen'; +import { TimeSeriesOptions } from 'app/plugins/panel/timeseries/types'; + +import { VizPanel, VizPanelState } from './VizPanel'; + +export type TypedVizPanelState = Omit< + Partial>, + 'pluginId' +>; + +export const panelBuilders = { + newTable: (state: TypedVizPanelState) => { + return new VizPanel({ + ...state, + pluginId: 'table', + }); + }, + newGraph: (state: TypedVizPanelState) => { + return new VizPanel({ + ...state, + pluginId: 'timeseries', + }); + }, + newBarGauge: (state: TypedVizPanelState) => { + return new VizPanel({ + ...state, + pluginId: 'bargauge', + }); + }, +}; diff --git a/public/app/features/scenes/scenes/demo.tsx b/public/app/features/scenes/scenes/demo.tsx index 98793e5fe6b..456cc924006 100644 --- a/public/app/features/scenes/scenes/demo.tsx +++ b/public/app/features/scenes/scenes/demo.tsx @@ -8,6 +8,7 @@ import { VizPanel, } from '../components'; import { EmbeddedScene } from '../components/Scene'; +import { panelBuilders } from '../components/VizPanel/panelBuilders'; import { SceneTimeRange } from '../core/SceneTimeRange'; import { SceneEditManager } from '../editor/SceneEditManager'; @@ -19,21 +20,27 @@ export function getFlexLayoutTest(standalone: boolean): Scene { layout: new SceneFlexLayout({ direction: 'row', children: [ - new VizPanel({ + panelBuilders.newGraph({ size: { minWidth: '70%' }, - pluginId: 'timeseries', title: 'Dynamic height and width', $data: getQueryRunnerWithRandomWalkQuery({}, { maxDataPointsFromWidth: true }), }), new SceneFlexLayout({ direction: 'column', children: [ - new VizPanel({ - pluginId: 'timeseries', + panelBuilders.newGraph({ title: 'Fill height', + options: {}, + fieldConfig: { + defaults: { + custom: { + fillOpacity: 20, + }, + }, + overrides: [], + }, }), - new VizPanel({ - pluginId: 'timeseries', + panelBuilders.newGraph({ title: 'Fill height', }), new SceneCanvasText({ @@ -42,10 +49,9 @@ export function getFlexLayoutTest(standalone: boolean): Scene { fontSize: 20, align: 'center', }), - new VizPanel({ - size: { height: 300 }, - pluginId: 'timeseries', + panelBuilders.newGraph({ title: 'Fixed height', + size: { height: 300 }, }), ], }),