mirror of
https://github.com/grafana/grafana.git
synced 2025-08-06 04:29:29 +08:00

* 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
88 lines
2.4 KiB
TypeScript
88 lines
2.4 KiB
TypeScript
import { cloneDeep } from 'lodash';
|
|
import { createElement, PureComponent } from 'react';
|
|
|
|
import { DataSourcePluginMeta, DataSourceSettings } from '@grafana/data';
|
|
import { AngularComponent, getAngularLoader } from '@grafana/runtime';
|
|
|
|
import { GenericDataSourcePlugin } from '../types';
|
|
|
|
export interface Props {
|
|
plugin: GenericDataSourcePlugin;
|
|
dataSource: DataSourceSettings;
|
|
dataSourceMeta: DataSourcePluginMeta;
|
|
onModelChange: (dataSource: DataSourceSettings) => void;
|
|
}
|
|
|
|
export class DataSourcePluginSettings extends PureComponent<Props> {
|
|
element: HTMLDivElement | null = null;
|
|
component?: AngularComponent;
|
|
scopeProps: {
|
|
ctrl: { datasourceMeta: DataSourcePluginMeta; current: DataSourceSettings };
|
|
onModelChanged: (dataSource: DataSourceSettings) => void;
|
|
};
|
|
|
|
constructor(props: Props) {
|
|
super(props);
|
|
|
|
this.scopeProps = {
|
|
ctrl: { datasourceMeta: props.dataSourceMeta, current: cloneDeep(props.dataSource) },
|
|
onModelChanged: this.onModelChanged,
|
|
};
|
|
this.onModelChanged = this.onModelChanged.bind(this);
|
|
}
|
|
|
|
componentDidMount() {
|
|
const { plugin } = this.props;
|
|
|
|
if (!this.element) {
|
|
return;
|
|
}
|
|
|
|
if (!plugin.components.ConfigEditor) {
|
|
// React editor is not specified, let's render angular editor
|
|
// How to approach this better? Introduce ReactDataSourcePlugin interface and typeguard it here?
|
|
const loader = getAngularLoader();
|
|
const template = '<plugin-component type="datasource-config-ctrl" />';
|
|
|
|
this.component = loader.load(this.element, this.scopeProps, template);
|
|
}
|
|
}
|
|
|
|
componentDidUpdate(prevProps: Props) {
|
|
const { plugin } = this.props;
|
|
if (!plugin.components.ConfigEditor && this.props.dataSource !== prevProps.dataSource) {
|
|
this.scopeProps.ctrl.current = cloneDeep(this.props.dataSource);
|
|
|
|
this.component?.digest();
|
|
}
|
|
}
|
|
|
|
componentWillUnmount() {
|
|
if (this.component) {
|
|
this.component.destroy();
|
|
}
|
|
}
|
|
|
|
onModelChanged = (dataSource: DataSourceSettings) => {
|
|
this.props.onModelChange(dataSource);
|
|
};
|
|
|
|
render() {
|
|
const { plugin, dataSource } = this.props;
|
|
|
|
if (!plugin) {
|
|
return null;
|
|
}
|
|
|
|
return (
|
|
<div ref={(element) => (this.element = element)}>
|
|
{plugin.components.ConfigEditor &&
|
|
createElement(plugin.components.ConfigEditor, {
|
|
options: dataSource,
|
|
onOptionsChange: this.onModelChanged,
|
|
})}
|
|
</div>
|
|
);
|
|
}
|
|
}
|