Skip to content

SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.

7.1 KB
Raw Blame
import { useCallback, useEffect, useRef, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { Plus } from '@signozhq/icons';
import { Button, Flex, Form, Select, Tooltip } from 'antd';
import { Switch } from '@signozhq/ui/switch';
import logEvent from 'api/common/logEvent';
import { ALERTS_DATA_SOURCE_MAP } from 'constants/alerts';
import ROUTES from 'constants/routes';
import { useChannelOptions } from 'hooks/notificationChannels/useChannelOptions';
import { useNotificationChannelCollectionPermissions } from 'hooks/notificationChannels/useNotificationChannelCollectionPermissions';
import { AlertTypes } from 'types/api/alerts/alertTypes';
import { AlertDef, Labels } from 'types/api/alerts/def';
import APIError from 'types/api/error';
import { requireErrorMessage } from 'utils/form/requireErrorMessage';
import { openInNewTab } from 'utils/navigation';
import { popupContainer } from 'utils/selectPopupContainer';

import ChannelSelect from './ChannelSelect';
import LabelSelect from './labels';
import {
	FormContainer,
	FormItemMedium,
	InputSmall,
	SeveritySelect,
	StepHeading,
	TextareaMedium,
} from './styles';

import './FormAlertRules.styles.scss';

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 {
		isLoading,
		data: channels,
		error,
		isError,
		refetch,
	} = useChannelOptions();

	const { canCreate: addNewChannelPermission } =
		useNotificationChannelCollectionPermissions();

	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.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,
		});
		openInNewTab(ROUTES.CHANNELS_NEW);
		// eslint-disable-next-line react-hooks/exhaustive-deps
	}, []);
	const hasLoggedEvent = useRef(false);

	useEffect(() => {
		if (!isLoading && isNewRule && !hasLoggedEvent.current) {
			logEvent('Alert: New alert creation page visited', {
				dataSource: ALERTS_DATA_SOURCE_MAP[alertDef?.alertType as AlertTypes],
				numberOfChannels: channels.length,
			});
			hasLoggedEvent.current = true;
		}
		// eslint-disable-next-line react-hooks/exhaustive-deps
	}, [isLoading]);

	const refetchChannels = async (): Promise<void> => {
		await refetch();
	};

	return (
		<>
			<StepHeading> {t('alert_form_step4')} </StepHeading>
			<FormContainer>
				<Form.Item
					label={t('field_severity')}
					labelAlign="left"
					name={['labels', 'severity']}
				>
					<SeveritySelect
						getPopupContainer={popupContainer}
						defaultValue="critical"
						data-testid="alert-severity-select"
						onChange={(value: unknown | string): void => {
							const s = (value as string) || 'critical';
							setAlertDef({
								...alertDef,
								labels: {
									...alertDef.labels,
									severity: s,
								},
							});
						}}
					>
						<Option value="critical">{t('option_critical')}</Option>
						<Option value="error">{t('option_error')}</Option>
						<Option value="warning">{t('option_warning')}</Option>
						<Option value="info">{t('option_info')}</Option>
					</SeveritySelect>
				</Form.Item>

				<Form.Item
					required
					name="alert"
					labelAlign="left"
					label={t('field_alert_name')}
					rules={[
						{ required: true, message: requireErrorMessage(t('field_alert_name')) },
					]}
				>
					<InputSmall
						data-testid="alert-name-input-v1"
						onChange={(e): void => {
							setAlertDef({
								...alertDef,
								alert: e.target.value,
							});
						}}
					/>
				</Form.Item>
				<Form.Item
					label={t('field_alert_desc')}
					labelAlign="left"
					name={['annotations', 'description']}
				>
					<TextareaMedium
						data-testid="alert-description-input"
						onChange={(e): void => {
							setAlertDef({
								...alertDef,
								annotations: {
									...alertDef.annotations,
									description: e.target.value,
								},
							});
						}}
					/>
				</Form.Item>
				<FormItemMedium label={t('field_labels')}>
					<LabelSelect
						onSetLabels={(l: Labels): void => {
							setAlertDef({
								...alertDef,
								labels: {
									...l,
								},
							});
						}}
						initialValues={alertDef.labels}
					/>
				</FormItemMedium>

				<FormItemMedium
					name="alert_all_configured_channels"
					label="Alert all the configured channels"
				>
					<Switch
						color="primary"
						textPlacement="right"
						disabledTooltip={
							isLoading
								? 'Wait for the channels to load'
								: 'No channels. Ask an admin to create a notification channel'
						}
						value={shouldBroadCastToAllChannels}
						onChange={handleBroadcastToAllChannels}
						disabled={noChannels || !!isLoading}
						testId="alert-broadcast-to-all-channels"
					/>
				</FormItemMedium>

				{!shouldBroadCastToAllChannels && (
					<Tooltip
						title={
							noChannels && !addNewChannelPermission
								? 'No channels. Ask an admin to create a notification channel'
								: undefined
						}
						placement="right"
					>
						<FormItemMedium
							label="Notification Channels"
							name="notification_channels"
							required
							rules={[
								{ required: true, message: requireErrorMessage(t('field_alert_name')) },
							]}
						>
							<ChannelSelect
								onDropdownOpen={refetchChannels}
								disabled={shouldBroadCastToAllChannels}
								currentValue={alertDef.preferredChannels}
								handleCreateNewChannels={handleCreateNewChannels}
								channels={channels}
								isLoading={isLoading}
								hasError={isError}
								error={error as APIError}
								onSelectChannels={(preferredChannels): void => {
									setAlertDef({
										...alertDef,
										preferredChannels,
									});
								}}
							/>
						</FormItemMedium>
					</Tooltip>
				)}

				{noChannels && (
					<Tooltip
						title={
							!addNewChannelPermission
								? 'Ask an admin to create a notification channel'
								: undefined
						}
						placement="right"
					>
						<Button
							onClick={handleCreateNewChannels}
							className="create-notification-btn"
							disabled={!addNewChannelPermission}
						>
							<Flex align="center" justify="center">
								<Plus size="md" />
								Create a notification channel
							</Flex>
						</Button>
					</Tooltip>
				)}
			</FormContainer>
		</>
	);
}

export default BasicInfo;