import React, { useCallback, useEffect, useRef } from 'react'; import { DataSourceApi, QueryEditorProps, SelectableValue } from '@grafana/data'; import { InlineField, Select } from '@grafana/ui'; import { ClassicConditions } from './components/ClassicConditions'; import { Math } from './components/Math'; import { Reduce } from './components/Reduce'; import { Resample } from './components/Resample'; import { Threshold } from './components/Threshold'; import { ExpressionQuery, ExpressionQueryType, gelTypes } from './types'; import { getDefaults } from './utils/expressionTypes'; type Props = QueryEditorProps, ExpressionQuery>; const labelWidth = 14; type NonClassicExpressionType = Exclude; type ExpressionTypeConfigStorage = Partial>; function useExpressionsCache() { const expressionCache = useRef({}); const getCachedExpression = useCallback((queryType: ExpressionQueryType) => { switch (queryType) { case ExpressionQueryType.math: case ExpressionQueryType.reduce: case ExpressionQueryType.resample: case ExpressionQueryType.threshold: return expressionCache.current[queryType]; case ExpressionQueryType.classic: return undefined; } }, []); const setCachedExpression = useCallback((queryType: ExpressionQueryType, value: string | undefined) => { switch (queryType) { case ExpressionQueryType.math: expressionCache.current.math = value; break; // We want to use the same value for Reduce, Resample and Threshold case ExpressionQueryType.reduce: case ExpressionQueryType.resample: case ExpressionQueryType.resample: expressionCache.current.reduce = value; expressionCache.current.resample = value; expressionCache.current.threshold = value; break; } }, []); return { getCachedExpression, setCachedExpression }; } export function ExpressionQueryEditor(props: Props) { const { query, queries, onRunQuery, onChange } = props; const { getCachedExpression, setCachedExpression } = useExpressionsCache(); useEffect(() => { setCachedExpression(query.type, query.expression); }, [query.expression, query.type, setCachedExpression]); const onSelectExpressionType = useCallback( (item: SelectableValue) => { const cachedExpression = getCachedExpression(item.value!); const defaults = getDefaults({ ...query, type: item.value! }); onChange({ ...defaults, expression: cachedExpression ?? defaults.expression }); }, [query, onChange, getCachedExpression] ); const renderExpressionType = () => { const refIds = queries!.filter((q) => query.refId !== q.refId).map((q) => ({ value: q.refId, label: q.refId })); switch (query.type) { case ExpressionQueryType.math: return ; case ExpressionQueryType.reduce: return ; case ExpressionQueryType.resample: return ; case ExpressionQueryType.classic: return ; case ExpressionQueryType.threshold: return ; } }; const selected = gelTypes.find((o) => o.value === query.type); return (