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';
const messages = defineMessages({
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!',
notificationsettingsfailed: 'Notification settings failed to save.',
enablenotifications: 'Enable Notifications',
autoapprovedrequests: 'Enable Notifications for Automatic Approvals',
});
interface SettingsRoute {
text: string;
content: React.ReactNode;
route: string;
regex: RegExp;
}
const settingsRoutes: SettingsRoute[] = [
{
text: 'Email',
content: (
Email
),
route: '/settings/notifications/email',
regex: /^\/settings\/notifications\/email/,
},
{
text: 'Discord',
content: (
Discord
),
route: '/settings/notifications/discord',
regex: /^\/settings\/notifications\/discord/,
},
{
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: 'Pushbullet',
content: (
Pushbullet
),
route: '/settings/notifications/pushbullet',
regex: /^\/settings\/notifications\/pushbullet/,
},
{
text: 'Pushover',
content: (
Pushover
),
route: '/settings/notifications/pushover',
regex: /^\/settings\/notifications\/pushover/,
},
{
text: 'Webhook',
content: (
Webhook
),
route: '/settings/notifications/webhook',
regex: /^\/settings\/notifications\/webhook/,
},
];
const SettingsNotifications: React.FC = ({ children }) => {
const router = useRouter();
const intl = useIntl();
const { addToast } = useToasts();
const { data, error, revalidate } = useSWR('/api/v1/settings/notifications');
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,
autoapprovalEnabled: values.autoapprovalEnabled,
});
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 (
);
}}
{intl.formatMessage(messages.notificationAgentsSettings)}
{intl.formatMessage(messages.notificationAgentSettingsDescription)}
{children}
>
);
};
export default SettingsNotifications;