import { css, cx } from '@emotion/css'; import React, { FC, useMemo } from 'react'; import { GrafanaTheme2 } from '@grafana/data'; import { Button, Field, FieldArray, Form, Icon, Input, Modal, useStyles2 } from '@grafana/ui'; import { AlertmanagerUrl } from 'app/plugins/datasource/alertmanager/types'; interface Props { onClose: () => void; alertmanagers: AlertmanagerUrl[]; onChangeAlertmanagerConfig: (alertmanagers: string[]) => void; } export const AddAlertManagerModal: FC = ({ alertmanagers, onChangeAlertmanagerConfig, onClose }) => { const styles = useStyles2(getStyles); const defaultValues: Record = useMemo( () => ({ alertmanagers: alertmanagers, }), [alertmanagers] ); const modalTitle = (

Add Alertmanager

); const onSubmit = (values: Record) => { onChangeAlertmanagerConfig(values.alertmanagers.map((am) => cleanAlertmanagerUrl(am.url))); onClose(); }; return (
We use a service discovery method to find existing Alertmanagers for a given URL.
{({ register, control, errors }) => (
{({ fields, append, remove }) => (
Source url
Authentication can be done via URL (e.g. user:password@myalertmanager.com) and only the Alertmanager v2 API is supported. The suffix is added internally, there is no need to specify it.
{fields.map((field, index) => { return ( remove(index)} variant="destructive" className={styles.destroyInputRow} > } /> ); })}
)}
)}
); }; function cleanAlertmanagerUrl(url: string): string { return url.replace(/\/$/, '').replace(/\/api\/v[1|2]\/alerts/i, ''); } const getStyles = (theme: GrafanaTheme2) => { const muted = css` color: ${theme.colors.text.secondary}; `; return { description: cx( css` margin-bottom: ${theme.spacing(2)}; `, muted ), muted: muted, bold: css` font-weight: ${theme.typography.fontWeightBold}; `, modal: css``, modalIcon: cx( muted, css` margin-right: ${theme.spacing(1)}; ` ), modalTitle: css` display: flex; `, input: css` margin-bottom: ${theme.spacing(1)}; margin-right: ${theme.spacing(1)}; `, inputRow: css` display: flex; `, destroyInputRow: css` padding: ${theme.spacing(1)}; `, fieldArray: css` margin-bottom: ${theme.spacing(4)}; `, }; };