import { css } from '@emotion/css'; import React, { useEffect, useState, useMemo } from 'react'; import { useDispatch } from 'react-redux'; import { GrafanaTheme2 } from '@grafana/data'; import { Alert, Button, ConfirmModal, TextArea, HorizontalGroup, Field, Form, useStyles2 } from '@grafana/ui'; import { useAlertManagerSourceName } from '../../hooks/useAlertManagerSourceName'; import { useAlertManagersByPermission } from '../../hooks/useAlertManagerSources'; import { useUnifiedAlertingSelector } from '../../hooks/useUnifiedAlertingSelector'; import { deleteAlertManagerConfigAction, fetchAlertManagerConfigAction, updateAlertManagerConfigAction, } from '../../state/actions'; import { GRAFANA_RULES_SOURCE_NAME, isVanillaPrometheusAlertManagerDataSource } from '../../utils/datasource'; import { initialAsyncRequestState } from '../../utils/redux'; import { AlertManagerPicker } from '../AlertManagerPicker'; interface FormValues { configJSON: string; } export default function AlertmanagerConfig(): JSX.Element { const dispatch = useDispatch(); const alertManagers = useAlertManagersByPermission('notification'); const [alertManagerSourceName, setAlertManagerSourceName] = useAlertManagerSourceName(alertManagers); const [showConfirmDeleteAMConfig, setShowConfirmDeleteAMConfig] = useState(false); const { loading: isDeleting } = useUnifiedAlertingSelector((state) => state.deleteAMConfig); const { loading: isSaving } = useUnifiedAlertingSelector((state) => state.saveAMConfig); const readOnly = alertManagerSourceName ? isVanillaPrometheusAlertManagerDataSource(alertManagerSourceName) : false; const styles = useStyles2(getStyles); const configRequests = useUnifiedAlertingSelector((state) => state.amConfigs); const { result: config, loading: isLoadingConfig, error: loadingError, } = (alertManagerSourceName && configRequests[alertManagerSourceName]) || initialAsyncRequestState; useEffect(() => { if (alertManagerSourceName) { dispatch(fetchAlertManagerConfigAction(alertManagerSourceName)); } }, [alertManagerSourceName, dispatch]); const resetConfig = () => { if (alertManagerSourceName) { dispatch(deleteAlertManagerConfigAction(alertManagerSourceName)); } setShowConfirmDeleteAMConfig(false); }; const defaultValues = useMemo( (): FormValues => ({ configJSON: config ? JSON.stringify(config, null, 2) : '', }), [config] ); const loading = isDeleting || isLoadingConfig || isSaving; const onSubmit = (values: FormValues) => { if (alertManagerSourceName && config) { dispatch( updateAlertManagerConfigAction({ newConfig: JSON.parse(values.configJSON), oldConfig: config, alertManagerSourceName, successMessage: 'Alertmanager configuration updated.', refetch: true, }) ); } }; return (