import React, { FC } from 'react'; import { SelectableValue } from '@grafana/data'; import { InlineField, InlineFieldRow, Input, Select } from '@grafana/ui'; import { ExpressionQuery, ExpressionQuerySettings, ReducerMode, reducerMode, reducerTypes } from '../types'; interface Props { labelWidth: number; refIds: Array>; query: ExpressionQuery; onChange: (query: ExpressionQuery) => void; } export const Reduce: FC = ({ labelWidth, onChange, refIds, query }) => { const reducer = reducerTypes.find((o) => o.value === query.reducer); const onRefIdChange = (value: SelectableValue) => { onChange({ ...query, expression: value.value }); }; const onSelectReducer = (value: SelectableValue) => { onChange({ ...query, reducer: value.value }); }; const onSettingsChanged = (settings: ExpressionQuerySettings) => { onChange({ ...query, settings: settings }); }; const onModeChanged = (value: SelectableValue) => { let newSettings: ExpressionQuerySettings; switch (value.value) { case ReducerMode.ReplaceNonNumbers: let replaceWithNumber = 0; if (query.settings?.mode === ReducerMode.ReplaceNonNumbers) { replaceWithNumber = query.settings?.replaceWithValue ?? 0; } newSettings = { mode: ReducerMode.ReplaceNonNumbers, replaceWithValue: replaceWithNumber, }; break; default: newSettings = { mode: value.value, }; } onSettingsChanged(newSettings); }; const onReplaceWithChanged = (e: React.FormEvent) => { const value = e.currentTarget.valueAsNumber; onSettingsChanged({ mode: ReducerMode.ReplaceNonNumbers, replaceWithValue: value ?? 0 }); }; const mode = query.settings?.mode ?? ReducerMode.Strict; const replaceWithNumber = () => { if (mode !== ReducerMode.ReplaceNonNumbers) { return; } return ( ); }; return (