import Link from 'next/link'; import { useRouter } from 'next/router'; import React from 'react'; import { defineMessages, useIntl } from 'react-intl'; import DiscordLogo from '../../assets/extlogos/discord.svg'; import SlackLogo from '../../assets/extlogos/slack.svg'; import TelegramLogo from '../../assets/extlogos/telegram.svg'; import PushbulletLogo from '../../assets/extlogos/pushbullet.svg'; import PushoverLogo from '../../assets/extlogos/pushover.svg'; import Bolt from '../../assets/bolt.svg'; import { Field, Form, Formik } from 'formik'; import useSWR from 'swr'; import Error from '../../pages/_error'; import LoadingSpinner from '../Common/LoadingSpinner'; import axios from 'axios'; import { useToasts } from 'react-toast-notifications'; import Button from '../Common/Button'; import PageTitle from '../Common/PageTitle'; import globalMessages from '../../i18n/globalMessages'; const messages = defineMessages({ notifications: 'Notifications', save: 'Save Changes', saving: 'Saving…', notificationsettings: 'Notification Settings', notificationsettingsDescription: 'Configure global notification settings. The options below will apply to all notification agents.', notificationAgentsSettings: 'Notification Agents', notificationAgentSettingsDescription: 'Choose the types of notifications to send, and which notification agents to use.', notificationsettingssaved: 'Notification settings saved successfully!', notificationsettingsfailed: 'Notification settings failed to save.', enablenotifications: 'Enable Notifications', email: 'Email', webhook: 'Webhook', }); interface SettingsRoute { text: string; content: React.ReactNode; route: string; regex: RegExp; } const SettingsNotifications: React.FC = ({ children }) => { const router = useRouter(); const intl = useIntl(); const { addToast } = useToasts(); const { data, error, revalidate } = useSWR('/api/v1/settings/notifications'); const settingsRoutes: SettingsRoute[] = [ { text: intl.formatMessage(messages.email), content: ( {intl.formatMessage(messages.email)} ), route: '/settings/notifications/email', regex: /^\/settings\/notifications\/email/, }, { text: 'Discord', content: ( Discord ), route: '/settings/notifications/discord', regex: /^\/settings\/notifications\/discord/, }, { text: 'Pushbullet', content: ( Pushbullet ), route: '/settings/notifications/pushbullet', regex: /^\/settings\/notifications\/pushbullet/, }, { text: 'Pushover', content: ( Pushover ), route: '/settings/notifications/pushover', regex: /^\/settings\/notifications\/pushover/, }, { text: 'Slack', content: ( Slack ), route: '/settings/notifications/slack', regex: /^\/settings\/notifications\/slack/, }, { text: 'Telegram', content: ( Telegram ), route: '/settings/notifications/telegram', regex: /^\/settings\/notifications\/telegram/, }, { text: intl.formatMessage(messages.webhook), content: ( {intl.formatMessage(messages.webhook)} ), route: '/settings/notifications/webhook', regex: /^\/settings\/notifications\/webhook/, }, ]; const activeLinkColor = 'bg-indigo-700'; const inactiveLinkColor = 'bg-gray-800'; const SettingsLink: React.FC<{ route: string; regex: RegExp; isMobile?: boolean; }> = ({ children, route, regex, isMobile = false }) => { if (isMobile) { return ; } return ( {children} ); }; if (!data && !error) { return ; } if (!data) { return ; } return ( <>

{intl.formatMessage(messages.notificationsettings)}

{intl.formatMessage(messages.notificationsettingsDescription)}

{ try { await axios.post('/api/v1/settings/notifications', { enabled: values.enabled, }); addToast(intl.formatMessage(messages.notificationsettingssaved), { appearance: 'success', autoDismiss: true, }); } catch (e) { addToast( intl.formatMessage(messages.notificationsettingsfailed), { appearance: 'error', autoDismiss: true, } ); } finally { revalidate(); } }} > {({ isSubmitting, values, setFieldValue }) => { return (
{ setFieldValue('enabled', !values.enabled); }} />
); }}

{intl.formatMessage(messages.notificationAgentsSettings)}

{intl.formatMessage(messages.notificationAgentSettingsDescription)}

{children}
); }; export default SettingsNotifications;