mirror of
https://github.com/grafana/grafana.git
synced 2025-07-31 06:22:56 +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
75 lines
2.2 KiB
TypeScript
75 lines
2.2 KiB
TypeScript
import { useCallback } from 'react';
|
|
|
|
import { DataQuery, DataSourceApi, DataSourceJsonData, QueryEditorProps, StandardVariableQuery } from '@grafana/data';
|
|
import { getTemplateSrv } from '@grafana/runtime';
|
|
|
|
import { importDataSourcePlugin } from '../../plugins/plugin_loader';
|
|
import {
|
|
hasCustomVariableSupport,
|
|
hasDatasourceVariableSupport,
|
|
hasLegacyVariableSupport,
|
|
hasStandardVariableSupport,
|
|
} from '../guard';
|
|
import { VariableQueryEditorType } from '../types';
|
|
|
|
import { LegacyVariableQueryEditor } from './LegacyVariableQueryEditor';
|
|
|
|
export async function getVariableQueryEditor<
|
|
TQuery extends DataQuery = DataQuery,
|
|
TOptions extends DataSourceJsonData = DataSourceJsonData,
|
|
VariableQuery extends DataQuery = TQuery,
|
|
>(
|
|
datasource: DataSourceApi<TQuery, TOptions>,
|
|
importDataSourcePluginFunc = importDataSourcePlugin
|
|
): Promise<VariableQueryEditorType> {
|
|
if (hasCustomVariableSupport(datasource)) {
|
|
return datasource.variables.editor;
|
|
}
|
|
|
|
if (hasDatasourceVariableSupport(datasource)) {
|
|
const dsPlugin = await importDataSourcePluginFunc(datasource.meta!);
|
|
|
|
if (!dsPlugin.components.QueryEditor) {
|
|
throw new Error('Missing QueryEditor in plugin definition.');
|
|
}
|
|
|
|
return dsPlugin.components.QueryEditor ?? null;
|
|
}
|
|
|
|
if (hasStandardVariableSupport(datasource)) {
|
|
return StandardVariableQueryEditor;
|
|
}
|
|
|
|
if (hasLegacyVariableSupport(datasource)) {
|
|
const dsPlugin = await importDataSourcePluginFunc(datasource.meta!);
|
|
return dsPlugin.components.VariableQueryEditor ?? LegacyVariableQueryEditor;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
export function StandardVariableQueryEditor<
|
|
TQuery extends DataQuery = DataQuery,
|
|
TOptions extends DataSourceJsonData = DataSourceJsonData,
|
|
>({
|
|
datasource: propsDatasource,
|
|
query: propsQuery,
|
|
onChange: propsOnChange,
|
|
}: QueryEditorProps<any, TQuery, TOptions, StandardVariableQuery>) {
|
|
const onChange = useCallback(
|
|
(query: any) => {
|
|
propsOnChange({ refId: 'StandardVariableQuery', query });
|
|
},
|
|
[propsOnChange]
|
|
);
|
|
|
|
return (
|
|
<LegacyVariableQueryEditor
|
|
query={propsQuery.query}
|
|
onChange={onChange}
|
|
datasource={propsDatasource}
|
|
templateSrv={getTemplateSrv()}
|
|
/>
|
|
);
|
|
}
|