import { BeakerIcon, SaveIcon } from '@heroicons/react/outline'; import axios from 'axios'; import { Field, Form, Formik } from 'formik'; import React, { useState } from 'react'; import { defineMessages, useIntl } from 'react-intl'; import { useToasts } from 'react-toast-notifications'; import useSWR from 'swr'; import * as Yup from 'yup'; import useSettings from '../../../hooks/useSettings'; import globalMessages from '../../../i18n/globalMessages'; import Button from '../../Common/Button'; import LoadingSpinner from '../../Common/LoadingSpinner'; import NotificationTypeSelector from '../../NotificationTypeSelector'; const messages = defineMessages({ agentenabled: 'Enable Agent', botUsername: 'Bot Username', botAvatarUrl: 'Bot Avatar URL', webhookUrl: 'Webhook URL', webhookUrlTip: 'Create a webhook integration in your server', discordsettingssaved: 'Discord notification settings saved successfully!', discordsettingsfailed: 'Discord notification settings failed to save.', toastDiscordTestSending: 'Sending Discord test notification…', toastDiscordTestSuccess: 'Discord test notification sent!', toastDiscordTestFailed: 'Discord test notification failed to send.', validationUrl: 'You must provide a valid URL', validationTypes: 'You must select at least one notification type', enableMentions: 'Enable Mentions', }); const NotificationsDiscord: React.FC = () => { const intl = useIntl(); const settings = useSettings(); const { addToast, removeToast } = useToasts(); const [isTesting, setIsTesting] = useState(false); const { data, error, mutate: revalidate, } = useSWR('/api/v1/settings/notifications/discord'); const NotificationsDiscordSchema = Yup.object().shape({ botAvatarUrl: Yup.string() .nullable() .url(intl.formatMessage(messages.validationUrl)), webhookUrl: Yup.string() .when('enabled', { is: true, then: Yup.string() .nullable() .required(intl.formatMessage(messages.validationUrl)), otherwise: Yup.string().nullable(), }) .url(intl.formatMessage(messages.validationUrl)), }); if (!data && !error) { return ; } return ( { try { await axios.post('/api/v1/settings/notifications/discord', { enabled: values.enabled, types: values.types, options: { botUsername: values.botUsername, botAvatarUrl: values.botAvatarUrl, webhookUrl: values.webhookUrl, enableMentions: values.enableMentions, }, }); addToast(intl.formatMessage(messages.discordsettingssaved), { appearance: 'success', autoDismiss: true, }); } catch (e) { addToast(intl.formatMessage(messages.discordsettingsfailed), { appearance: 'error', autoDismiss: true, }); } finally { revalidate(); } }} > {({ errors, touched, isSubmitting, values, isValid, setFieldValue, setFieldTouched, }) => { const testSettings = async () => { setIsTesting(true); let toastId: string | undefined; try { addToast( intl.formatMessage(messages.toastDiscordTestSending), { autoDismiss: false, appearance: 'info', }, (id) => { toastId = id; } ); await axios.post('/api/v1/settings/notifications/discord/test', { enabled: true, types: values.types, options: { botUsername: values.botUsername, botAvatarUrl: values.botAvatarUrl, webhookUrl: values.webhookUrl, enableMentions: values.enableMentions, }, }); if (toastId) { removeToast(toastId); } addToast(intl.formatMessage(messages.toastDiscordTestSuccess), { autoDismiss: true, appearance: 'success', }); } catch (e) { if (toastId) { removeToast(toastId); } addToast(intl.formatMessage(messages.toastDiscordTestFailed), { autoDismiss: true, appearance: 'error', }); } finally { setIsTesting(false); } }; return (
{errors.webhookUrl && touched.webhookUrl && (
{errors.webhookUrl}
)}
{errors.botUsername && touched.botUsername && (
{errors.botUsername}
)}
{errors.botAvatarUrl && touched.botAvatarUrl && (
{errors.botAvatarUrl}
)}
{ setFieldValue('types', newTypes); setFieldTouched('types'); if (newTypes) { setFieldValue('enabled', true); } }} error={ values.enabled && !values.types && touched.types ? intl.formatMessage(messages.validationTypes) : undefined } />
); }}
); }; export default NotificationsDiscord;