import { css } from '@emotion/css'; import React, { FC, useMemo, useState } from 'react'; import { useDispatch } from 'react-redux'; import { GrafanaTheme2 } from '@grafana/data'; import { Button, ConfirmModal, Modal, useStyles2 } from '@grafana/ui'; import { contextSrv } from 'app/core/services/context_srv'; import { AlertManagerCortexConfig } from 'app/plugins/datasource/alertmanager/types'; import { Authorize } from '../../components/Authorize'; import { useUnifiedAlertingSelector } from '../../hooks/useUnifiedAlertingSelector'; import { deleteReceiverAction } from '../../state/actions'; import { getAlertTableStyles } from '../../styles/table'; import { getNotificationsPermissions } from '../../utils/access-control'; import { isReceiverUsed } from '../../utils/alertmanager'; import { isVanillaPrometheusAlertManagerDataSource } from '../../utils/datasource'; import { makeAMLink } from '../../utils/misc'; import { extractNotifierTypeCounts } from '../../utils/receivers'; import { ActionIcon } from '../rules/ActionIcon'; import { ReceiversSection } from './ReceiversSection'; interface Props { config: AlertManagerCortexConfig; alertManagerName: string; } export const ReceiversTable: FC = ({ config, alertManagerName }) => { const dispatch = useDispatch(); const tableStyles = useStyles2(getAlertTableStyles); const styles = useStyles2(getStyles); const isVanillaAM = isVanillaPrometheusAlertManagerDataSource(alertManagerName); const permissions = getNotificationsPermissions(alertManagerName); const grafanaNotifiers = useUnifiedAlertingSelector((state) => state.grafanaNotifiers); // receiver name slated for deletion. If this is set, a confirmation modal is shown. If user approves, this receiver is deleted const [receiverToDelete, setReceiverToDelete] = useState(); const [showCannotDeleteReceiverModal, setShowCannotDeleteReceiverModal] = useState(false); const onClickDeleteReceiver = (receiverName: string): void => { if (isReceiverUsed(receiverName, config)) { setShowCannotDeleteReceiverModal(true); } else { setReceiverToDelete(receiverName); } }; const deleteReceiver = () => { if (receiverToDelete) { dispatch(deleteReceiverAction(receiverToDelete, alertManagerName)); } setReceiverToDelete(undefined); }; const rows = useMemo( () => config.alertmanager_config.receivers?.map((receiver) => ({ name: receiver.name, types: Object.entries(extractNotifierTypeCounts(receiver, grafanaNotifiers.result ?? [])).map( ([type, count]) => { if (count > 1) { return `${type} (${count})`; } return type; } ), })) ?? [], [config, grafanaNotifiers.result] ); return ( {!rows.length && ( )} {rows.map((receiver, idx) => ( ))}
Contact point name Type Actions
No receivers defined.
{receiver.name} {receiver.types.join(', ')} {!isVanillaAM && ( <> onClickDeleteReceiver(receiver.name)} tooltip="Delete contact point" icon="trash-alt" /> )} {isVanillaAM && ( )}
{!!showCannotDeleteReceiverModal && ( setShowCannotDeleteReceiverModal(false)} >

Contact point cannot be deleted because it is used in more policies. Please update or delete these policies first.

)} {!!receiverToDelete && ( setReceiverToDelete(undefined)} /> )}
); }; const getStyles = (theme: GrafanaTheme2) => ({ section: css` margin-top: ${theme.spacing(4)}; `, });