import { Form, InputNumber, InputNumberProps, Select, SelectProps, Space, Typography, } from 'antd'; import { DefaultOptionType } from 'antd/es/select'; import { getCategoryByOptionId, getCategorySelectOptionByName, } from 'container/NewWidget/RightContainer/alertFomatCategories'; import { useQueryBuilder } from 'hooks/queryBuilder/useQueryBuilder'; import { useTranslation } from 'react-i18next'; import { AlertDef, defaultCompareOp, defaultEvalWindow, defaultMatchType, } from 'types/api/alerts/def'; import { EQueryType } from 'types/common/dashboard'; import { popupContainer } from 'utils/selectPopupContainer'; import { FormContainer, InlineSelect, StepHeading } from './styles'; function RuleOptions({ alertDef, setAlertDef, queryCategory, queryOptions, }: RuleOptionsProps): JSX.Element { // init namespace for translations const { t } = useTranslation('alerts'); const { currentQuery } = useQueryBuilder(); const handleMatchOptChange = (value: string | unknown): void => { const m = (value as string) || alertDef.condition?.matchType; setAlertDef({ ...alertDef, condition: { ...alertDef.condition, matchType: m, }, }); }; const onChangeSelectedQueryName = (value: string | unknown): void => { if (typeof value !== 'string') return; setAlertDef({ ...alertDef, condition: { ...alertDef.condition, selectedQueryName: value, }, }); }; const renderCompareOps = (): JSX.Element => ( { const newOp = (value as string) || ''; setAlertDef({ ...alertDef, condition: { ...alertDef.condition, op: newOp, }, }); }} > {t('option_above')} {t('option_below')} {t('option_equal')} {t('option_notequal')} ); const renderMatchOpts = (): JSX.Element => ( handleMatchOptChange(value)} > {t('option_atleastonce')} {t('option_allthetimes')} {t('option_onaverage')} {t('option_intotal')} ); const onChangeEvalWindow = (value: string | unknown): void => { const ew = (value as string) || alertDef.evalWindow; setAlertDef({ ...alertDef, evalWindow: ew, }); }; const renderEvalWindows = (): JSX.Element => ( {t('option_5min')} {t('option_10min')} {t('option_15min')} {t('option_60min')} {t('option_4hours')} {t('option_24hours')} ); const renderPromEvalWindows = (): JSX.Element => ( {t('option_5min')} {t('option_10min')} {t('option_15min')} ); const renderThresholdRuleOpts = (): JSX.Element => ( {t('text_condition1')} is {renderCompareOps()} {t('text_condition2')} {renderMatchOpts()}{' '} {t('text_condition3')} {renderEvalWindows()} ); const renderPromRuleOptions = (): JSX.Element => ( {t('text_condition1')} is {renderCompareOps()} {t('text_condition2')} {renderMatchOpts()} {t('text_condition3')} {renderPromEvalWindows()} ); const onChange: InputNumberProps['onChange'] = (value): void => { setAlertDef({ ...alertDef, condition: { ...alertDef.condition, op: alertDef.condition?.op || defaultCompareOp, matchType: alertDef.condition?.matchType || defaultMatchType, target: Number(value) || 0, }, }); }; const onChangeAlertUnit: SelectProps['onChange'] = (value) => { setAlertDef({ ...alertDef, condition: { ...alertDef.condition, targetUnit: value as string, }, }); }; const selectedCategory = getCategoryByOptionId(currentQuery?.unit || ''); const categorySelectOptions = getCategorySelectOptionByName( selectedCategory?.name, ); return ( <> {t('alert_form_step2')} {queryCategory === EQueryType.PROM ? renderPromRuleOptions() : renderThresholdRuleOpts()} e.currentTarget.blur()} />