Files
grafana/public/app/features/trails/ActionTabs/LayoutSwitcher.tsx
Ashley Harrison 47f8717149 React: Use new JSX transform (#88802)
* update eslint, tsconfig + esbuild to handle new jsx transform

* remove thing that breaks the new jsx transform

* remove react imports

* adjust grafana-icons build

* is this the correct syntax?

* try this

* well this was much easier than expected...

* change grafana-plugin-configs webpack config

* fixes

* fix lockfile

* fix 2 more violations

* use path.resolve instead of require.resolve

* remove react import

* fix react imports

* more fixes

* remove React import

* remove import React from docs

* remove another react import
2024-06-25 12:43:47 +01:00

58 lines
1.8 KiB
TypeScript

import { SelectableValue } from '@grafana/data';
import { SceneComponentProps, sceneGraph, SceneObject, SceneObjectBase, SceneObjectState } from '@grafana/scenes';
import { Field, RadioButtonGroup } from '@grafana/ui';
import { MetricScene } from '../MetricScene';
import { reportExploreMetrics } from '../interactions';
import { LayoutType } from './types';
export interface LayoutSwitcherState extends SceneObjectState {
layouts: SceneObject[];
options: Array<SelectableValue<LayoutType>>;
}
export class LayoutSwitcher extends SceneObjectBase<LayoutSwitcherState> {
private getMetricScene() {
return sceneGraph.getAncestor(this, MetricScene);
}
public Selector({ model }: { model: LayoutSwitcher }) {
const { options } = model.useState();
const activeLayout = model.useActiveLayout();
return (
<Field label="View">
<RadioButtonGroup options={options} value={activeLayout} onChange={model.onLayoutChange} />
</Field>
);
}
private useActiveLayout() {
const { options } = this.useState();
const { layout } = this.getMetricScene().useState();
const activeLayout = options.map((option) => option.value).includes(layout) ? layout : options[0].value;
return activeLayout;
}
public onLayoutChange = (layout: LayoutType) => {
reportExploreMetrics('breakdown_layout_changed', { layout });
this.getMetricScene().setState({ layout });
};
public static Component = ({ model }: SceneComponentProps<LayoutSwitcher>) => {
const { layouts, options } = model.useState();
const activeLayout = model.useActiveLayout();
const index = options.findIndex((o) => o.value === activeLayout);
if (index === -1) {
return null;
}
const layout = layouts[index];
return <layout.Component model={layout} />;
};
}