grafana/public/app/features/scenes/components/ScenePanelRepeater.tsx
Torkel Ödegaard 8c585a4ebf
Scene: Variables interpolation formats and multi value handling (#58591)
* Component that can cache and extract variable dependencies

* Component that can cache and extract variable dependencies

* Updates

* Refactoring

* Lots of refactoring and iterations of supporting both re-rendering and query re-execution

* Updated SceneCanvasText

* Updated name of file

* Updated

* Refactoring a bit

* Added back getName

* Added comment

* minor fix

* Minor fix

* Merge fixes

* Scene variable interpolation progress

* Merge fixes

* Added all format registeries

* Progress on multi value support

* Progress on multi value support

* Updates

* Progress on scoped vars

* Fixed circular dependency

* Updates

* Some review fixes

* Updated comment

* Added forceRender function

* Add back fail on console log

* Update public/app/features/scenes/variables/interpolation/sceneInterpolator.test.ts

* Moving functions from SceneObjectBase

* fixing tests

* Fixed e2e

Co-authored-by: Dominik Prokop <dominik.prokop@grafana.com>
2022-11-16 11:36:30 +01:00

62 lines
1.6 KiB
TypeScript

import React from 'react';
import { LoadingState, PanelData } from '@grafana/data';
import { SceneDataNode } from '../core/SceneDataNode';
import { SceneObjectBase } from '../core/SceneObjectBase';
import { sceneGraph } from '../core/sceneGraph';
import {
SceneComponentProps,
SceneObject,
SceneObjectStatePlain,
SceneLayoutState,
SceneLayoutChild,
} from '../core/types';
interface RepeatOptions extends SceneObjectStatePlain {
layout: SceneObject<SceneLayoutState>;
}
export class ScenePanelRepeater extends SceneObjectBase<RepeatOptions> {
public activate(): void {
super.activate();
this._subs.add(
sceneGraph.getData(this).subscribeToState({
next: (data) => {
if (data.data?.state === LoadingState.Done) {
this.performRepeat(data.data);
}
},
})
);
}
private performRepeat(data: PanelData) {
// assume parent is a layout
const firstChild = this.state.layout.state.children[0]!;
const newChildren: SceneLayoutChild[] = [];
for (const series of data.series) {
const clone = firstChild.clone({
key: `${newChildren.length}`,
$data: new SceneDataNode({
data: {
...data,
series: [series],
},
}),
});
newChildren.push(clone);
}
this.state.layout.setState({ children: newChildren });
}
public static Component = ({ model, isEditing }: SceneComponentProps<ScenePanelRepeater>) => {
const { layout } = model.useState();
return <layout.Component model={layout} isEditing={isEditing} />;
};
}