import { css, cx } from '@emotion/css'; import { useEffect } from 'react'; import { Controller, useFieldArray, useFormContext } from 'react-hook-form'; import { GrafanaTheme2 } from '@grafana/data'; import { Trans, useTranslate } from '@grafana/i18n'; import { Button, Divider, Field, IconButton, Input, Select, useStyles2 } from '@grafana/ui'; import { alertRuleApi } from 'app/features/alerting/unified/api/alertRuleApi'; import { MatcherOperator } from 'app/plugins/datasource/alertmanager/types'; import { SilenceFormFields } from '../../types/silence-form'; import { matcherFieldOptions } from '../../utils/alertmanager'; interface Props { className?: string; required: boolean; ruleUid?: string; } const MatchersField = ({ className, required, ruleUid }: Props) => { const styles = useStyles2(getStyles); const formApi = useFormContext(); const { control, register, formState: { errors }, } = formApi; const { fields: matchers = [], append, remove } = useFieldArray({ name: 'matchers' }); const [getAlertRule, { data: alertRule }] = alertRuleApi.endpoints.getAlertRule.useLazyQuery(); useEffect(() => { // If we have a UID, fetch the alert rule details so we can display the rule name if (ruleUid) { getAlertRule({ uid: ruleUid }); } }, [getAlertRule, ruleUid]); const { t } = useTranslate(); return (
{alertRule && (
)} {matchers.map((matcher, index) => { return (
( {(matchers.length > 1 || !required) && ( remove(index)} > Remove )}
); })}
); }; const getStyles = (theme: GrafanaTheme2) => { return { wrapper: css({ marginTop: theme.spacing(2), }), row: css({ marginTop: theme.spacing(1), display: 'flex', alignItems: 'flex-start', flexDirection: 'row', backgroundColor: theme.colors.background.secondary, padding: `${theme.spacing(1)} ${theme.spacing(1)} 0 ${theme.spacing(1)}`, '& > * + *': { marginLeft: theme.spacing(2), }, }), removeButton: css({ marginLeft: theme.spacing(1), marginTop: theme.spacing(2.5), }), matcherOptions: css({ minWidth: '140px', }), matchers: css({ maxWidth: `${theme.breakpoints.values.sm}px`, margin: `${theme.spacing(1)} 0`, paddingTop: theme.spacing(0.5), }), indent: css({ marginLeft: theme.spacing(2), }), }; }; export default MatchersField;