import React, { FC, useState } from 'react'; import { css } from '@emotion/css'; import { GrafanaTheme, parseDuration, addDurationToDate } from '@grafana/data'; import { Field, InlineLabel, Input, InputControl, Select, Switch, useStyles } from '@grafana/ui'; import { useFormContext, RegisterOptions } from 'react-hook-form'; import { RuleFormType, RuleFormValues } from '../../types/rule-form'; import { timeOptions, timeValidationPattern } from '../../utils/time'; import { ConditionField } from './ConditionField'; import { GrafanaAlertStatePicker } from './GrafanaAlertStatePicker'; import { RuleEditorSection } from './RuleEditorSection'; import { PreviewRule } from './PreviewRule'; const MIN_TIME_RANGE_STEP_S = 10; // 10 seconds const timeRangeValidationOptions: RegisterOptions = { required: { value: true, message: 'Required.', }, pattern: timeValidationPattern, validate: (value: string) => { const duration = parseDuration(value); if (Object.keys(duration).length) { const from = new Date(); const to = addDurationToDate(from, duration); const diff = to.getTime() - from.getTime(); if (diff < MIN_TIME_RANGE_STEP_S * 1000) { return `Cannot be less than ${MIN_TIME_RANGE_STEP_S} seconds.`; } if (diff % (MIN_TIME_RANGE_STEP_S * 1000) !== 0) { return `Must be a multiple of ${MIN_TIME_RANGE_STEP_S} seconds.`; } } return true; }, }; export const ConditionsStep: FC = () => { const styles = useStyles(getStyles); const [showErrorHandling, setShowErrorHandling] = useState(false); const { register, control, watch, formState: { errors }, } = useFormContext(); const type = watch('type'); return ( {type === RuleFormType.grafana && ( <>
Evaluate every for
setShowErrorHandling(!showErrorHandling)} /> {showErrorHandling && ( <> ( onChange(value?.value)} /> )} name="noDataState" /> ( onChange(value?.value)} /> )} name="execErrState" /> )} )} {type === RuleFormType.cloud && ( <>
(