import './FormAlertRules.styles.scss'; import { PlusOutlined } from '@ant-design/icons'; import { Button, Form, Select, Switch, Tooltip } from 'antd'; import getChannels from 'api/channels/getAll'; import logEvent from 'api/common/logEvent'; import { ALERTS_DATA_SOURCE_MAP } from 'constants/alerts'; import ROUTES from 'constants/routes'; import useComponentPermission from 'hooks/useComponentPermission'; import useFetch from 'hooks/useFetch'; import { useCallback, useEffect, useState } from 'react'; import { useTranslation } from 'react-i18next'; import { useSelector } from 'react-redux'; import { AppState } from 'store/reducers'; import { AlertTypes } from 'types/api/alerts/alertTypes'; import { AlertDef, Labels } from 'types/api/alerts/def'; import AppReducer from 'types/reducer/app'; import { requireErrorMessage } from 'utils/form/requireErrorMessage'; import { popupContainer } from 'utils/selectPopupContainer'; import ChannelSelect from './ChannelSelect'; import LabelSelect from './labels'; import { FormContainer, FormItemMedium, InputSmall, SeveritySelect, StepHeading, TextareaMedium, } from './styles'; const { Option } = Select; interface BasicInfoProps { isNewRule: boolean; alertDef: AlertDef; setAlertDef: (a: AlertDef) => void; } function BasicInfo({ isNewRule, alertDef, setAlertDef, }: BasicInfoProps): JSX.Element { const { t } = useTranslation('alerts'); const channels = useFetch(getChannels); const { role } = useSelector((state) => state.app); const [addNewChannelPermission] = useComponentPermission( ['add_new_channel'], role, ); const [ shouldBroadCastToAllChannels, setShouldBroadCastToAllChannels, ] = useState(false); useEffect(() => { const hasPreferredChannels = (alertDef.preferredChannels && alertDef.preferredChannels.length > 0) || isNewRule; setShouldBroadCastToAllChannels(!hasPreferredChannels); // eslint-disable-next-line react-hooks/exhaustive-deps }, []); const handleBroadcastToAllChannels = (shouldBroadcast: boolean): void => { setShouldBroadCastToAllChannels(shouldBroadcast); setAlertDef({ ...alertDef, broadcastToAll: shouldBroadcast, }); }; const noChannels = channels.payload?.length === 0; const handleCreateNewChannels = useCallback(() => { logEvent('Alert: Create notification channel button clicked', { dataSource: ALERTS_DATA_SOURCE_MAP[alertDef?.alertType as AlertTypes], ruleId: isNewRule ? 0 : alertDef?.id, }); window.open(ROUTES.CHANNELS_NEW, '_blank'); // eslint-disable-next-line react-hooks/exhaustive-deps }, []); useEffect(() => { if (!channels.loading && isNewRule) { logEvent('Alert: New alert creation page visited', { dataSource: ALERTS_DATA_SOURCE_MAP[alertDef?.alertType as AlertTypes], numberOfChannels: channels?.payload?.length, }); } // eslint-disable-next-line react-hooks/exhaustive-deps }, [channels.payload, channels.loading]); return ( <> {t('alert_form_step4')} { const s = (value as string) || 'critical'; setAlertDef({ ...alertDef, labels: { ...alertDef.labels, severity: s, }, }); }} > { setAlertDef({ ...alertDef, alert: e.target.value, }); }} /> { setAlertDef({ ...alertDef, annotations: { ...alertDef.annotations, description: e.target.value, }, }); }} /> { setAlertDef({ ...alertDef, labels: { ...l, }, }); }} initialValues={alertDef.labels} /> {!shouldBroadCastToAllChannels && ( { setAlertDef({ ...alertDef, preferredChannels, }); }} /> )} {noChannels && ( )} ); } export default BasicInfo;