import { Fragment, useState } from 'react'; import { logError } from '@grafana/runtime'; import { Badge, ConfirmModal, Tooltip, useStyles2 } from '@grafana/ui'; import { useAppNotification } from 'app/core/copy/appNotification'; import { Trans, t } from 'app/core/internationalization'; import { CodeText } from 'app/features/alerting/unified/components/common/TextVariants'; import { GRAFANA_RULES_SOURCE_NAME } from 'app/features/alerting/unified/utils/datasource'; import { Authorize } from '../../components/Authorize'; import { AlertmanagerAction } from '../../hooks/useAbilities'; import { getAlertTableStyles } from '../../styles/table'; import { makeAMLink, stringifyErrorLike } from '../../utils/misc'; import { CollapseToggle } from '../CollapseToggle'; import { DetailsField } from '../DetailsField'; import { ProvisioningBadge } from '../Provisioning'; import { NotificationTemplate, useDeleteNotificationTemplate, useNotificationTemplateMetadata, } from '../contact-points/useNotificationTemplates'; import { ActionIcon } from '../rules/ActionIcon'; import { TemplateEditor } from './TemplateEditor'; interface Props { alertManagerName: string; templates: NotificationTemplate[]; } export const TemplatesTable = ({ alertManagerName, templates }: Props) => { const appNotification = useAppNotification(); const [deleteTemplate] = useDeleteNotificationTemplate({ alertmanager: alertManagerName }); const tableStyles = useStyles2(getAlertTableStyles); const [templateToDelete, setTemplateToDelete] = useState(); const onDeleteTemplate = async () => { if (templateToDelete) { try { await deleteTemplate.execute({ uid: templateToDelete.uid }); appNotification.success('Template deleted', `Template ${templateToDelete.title} has been deleted`); } catch (error) { appNotification.error('Error deleting template', `Error deleting template ${templateToDelete.title}`); const loggableError = error instanceof Error ? error : new Error(stringifyErrorLike(error)); logError(loggableError); } } setTemplateToDelete(undefined); }; return ( <> {!templates.length && ( )} {templates.map((notificationTemplate, idx) => ( ))}
Template group Actions
No templates defined.
{!!templateToDelete && ( setTemplateToDelete(undefined)} /> )} ); }; interface TemplateRowProps { notificationTemplate: NotificationTemplate; idx: number; alertManagerName: string; onDeleteClick: (template: NotificationTemplate) => void; } function TemplateRow({ notificationTemplate, idx, alertManagerName, onDeleteClick }: TemplateRowProps) { const tableStyles = useStyles2(getAlertTableStyles); const isGrafanaAlertmanager = alertManagerName === GRAFANA_RULES_SOURCE_NAME; const [isExpanded, setIsExpanded] = useState(false); const { isProvisioned } = useNotificationTemplateMetadata(notificationTemplate); const { uid, title: name, content: template, missing } = notificationTemplate; const misconfiguredBadgeText = t('alerting.templates.misconfigured-badge-text', 'Misconfigured'); return ( setIsExpanded(!isExpanded)} /> {name} {isProvisioned && }{' '} {missing && !isGrafanaAlertmanager && ( This template is misconfigured.
Templates must be defined in both the and{' '} sections of your alertmanager configuration. } >
)} {isProvisioned && ( )} {!isProvisioned && ( )} {!isProvisioned && ( onDeleteClick(notificationTemplate)} tooltip="Delete template group" icon="trash-alt" /> )} {isExpanded && ( )}
); }