import { css, cx } from '@emotion/css'; import React, { FC, FormEvent } from 'react'; import { GrafanaTheme, SelectableValue } from '@grafana/data'; import { Button, ButtonSelect, Icon, InlineFieldRow, Input, Select, useStyles } from '@grafana/ui'; import alertDef, { EvalFunction } from '../../alerting/state/alertDef'; import { ClassicCondition, ReducerType } from '../types'; interface Props { condition: ClassicCondition; onChange: (condition: ClassicCondition) => void; onRemoveCondition: (id: number) => void; index: number; refIds: Array>; } const reducerFunctions = alertDef.reducerTypes.map((rt) => ({ label: rt.text, value: rt.value })); const evalOperators = alertDef.evalOperators.map((eo) => ({ label: eo.text, value: eo.value })); const evalFunctions = alertDef.evalFunctions.map((ef) => ({ label: ef.text, value: ef.value })); export const Condition: FC = ({ condition, index, onChange, onRemoveCondition, refIds }) => { const styles = useStyles(getStyles); const onEvalOperatorChange = (evalOperator: SelectableValue) => { onChange({ ...condition, operator: { type: evalOperator.value! }, }); }; const onReducerFunctionChange = (conditionFunction: SelectableValue) => { onChange({ ...condition, reducer: { type: conditionFunction.value! as ReducerType, params: [] }, }); }; const onRefIdChange = (refId: SelectableValue) => { onChange({ ...condition, query: { params: [refId.value!] }, }); }; const onEvalFunctionChange = (evalFunction: SelectableValue) => { onChange({ ...condition, evaluator: { params: condition.evaluator.params, type: evalFunction.value! }, }); }; const onEvaluateValueChange = (event: FormEvent, index: number) => { const newValue = parseFloat(event.currentTarget.value); const newParams = [...condition.evaluator.params]; newParams[index] = newValue; onChange({ ...condition, evaluator: { ...condition.evaluator, params: newParams }, }); }; const buttonWidth = css` width: 60px; `; const isRange = condition.evaluator.type === EvalFunction.IsWithinRange || condition.evaluator.type === EvalFunction.IsOutsideRange; return ( {index === 0 ? (
WHEN
) : ( ea.value === condition.operator!.type)} /> )} r.value === condition.query.params[0])} /> ef.value === condition.evaluator.type)} /> {isRange ? ( <> onEvaluateValueChange(event, 0)} value={condition.evaluator.params[0]} />
TO
onEvaluateValueChange(event, 1)} value={condition.evaluator.params[1]} /> ) : condition.evaluator.type !== EvalFunction.HasNoValue ? ( onEvaluateValueChange(event, 0)} value={condition.evaluator.params[0]} /> ) : null}
); }; const getStyles = (theme: GrafanaTheme) => { const buttonStyle = css` color: ${theme.colors.textBlue}; font-size: ${theme.typography.size.sm}; `; return { buttonSelectText: buttonStyle, button: cx( css` display: flex; align-items: center; border-radius: ${theme.border.radius.sm}; font-weight: ${theme.typography.weight.semibold}; border: 1px solid ${theme.colors.border1}; white-space: nowrap; padding: 0 ${theme.spacing.sm}; background-color: ${theme.colors.bodyBg}; `, buttonStyle ), }; };