feat: notification framework

This commit is contained in:
sct
2020-11-22 19:11:14 +09:00
parent fb5c791b0b
commit d8e542e5fe
15 changed files with 577 additions and 3 deletions

View File

@@ -186,7 +186,7 @@ const MovieDetails: React.FC<MovieDetailsProps> = ({ movie }) => {
/>
</div>
<div className="text-white flex flex-col mr-4 mt-4 md:mt-0 text-center md:text-left">
<div className="mb-2 md:mb-0">
<div className="mb-2">
{data.mediaInfo?.status === MediaStatus.AVAILABLE && (
<Badge badgeType="success">Available</Badge>
)}

View File

@@ -0,0 +1,116 @@
import React, { useState } from 'react';
import { Field, Form, Formik } from 'formik';
import useSWR from 'swr';
import LoadingSpinner from '../../Common/LoadingSpinner';
import Button from '../../Common/Button';
import { defineMessages, useIntl } from 'react-intl';
import Axios from 'axios';
import * as Yup from 'yup';
const messages = defineMessages({
save: 'Save Changes',
saving: 'Saving...',
});
const NotificationsDiscord: React.FC = () => {
const intl = useIntl();
const { data, error, revalidate } = useSWR(
'/api/v1/settings/notifications/discord'
);
const NotificationsDiscordSchema = Yup.object().shape({
webhookUrl: Yup.string().required('You must provide a webhook URL'),
});
if (!data && !error) {
return <LoadingSpinner />;
}
return (
<Formik
initialValues={{
enabled: data.enabled,
types: data.types,
webhookUrl: data.options.webhookUrl,
}}
validationSchema={NotificationsDiscordSchema}
onSubmit={async (values) => {
try {
await Axios.post('/api/v1/settings/notifications/discord', {
enabled: values.enabled,
types: values.types,
options: {
webhookUrl: values.webhookUrl,
},
});
} catch (e) {
// TODO show error
} finally {
revalidate();
}
}}
>
{({ errors, touched, isSubmitting }) => {
return (
<Form>
<div className="sm:grid sm:grid-cols-3 sm:gap-4 sm:items-start sm:border-t sm:border-gray-200 sm:pt-5">
<label
htmlFor="isDefault"
className="block text-sm font-medium leading-5 text-gray-400 sm:mt-px sm:pt-2"
>
Agent Enabled
</label>
<div className="mt-1 sm:mt-0 sm:col-span-2">
<Field
type="checkbox"
id="enabled"
name="enabled"
className="form-checkbox rounded-md h-6 w-6 text-indigo-600 transition duration-150 ease-in-out"
/>
</div>
</div>
<div className="mt-6 sm:mt-5 sm:grid sm:grid-cols-3 sm:gap-4 sm:items-start sm:border-t sm:border-gray-800 sm:pt-5">
<label
htmlFor="name"
className="block text-sm font-medium leading-5 text-gray-400 sm:mt-px sm:pt-2"
>
Webhook URL
</label>
<div className="mt-1 sm:mt-0 sm:col-span-2">
<div className="max-w-lg flex rounded-md shadow-sm">
<Field
id="webhookUrl"
name="webhookUrl"
type="text"
placeholder="Server Settings -> Integrations -> Webhooks"
className="flex-1 form-input block w-full min-w-0 rounded-md transition duration-150 ease-in-out sm:text-sm sm:leading-5 bg-gray-700 border border-gray-500"
/>
</div>
{errors.webhookUrl && touched.webhookUrl && (
<div className="text-red-500 mt-2">{errors.webhookUrl}</div>
)}
</div>
</div>
<div className="mt-8 border-t border-gray-700 pt-5">
<div className="flex justify-end">
<span className="ml-3 inline-flex rounded-md shadow-sm">
<Button
buttonType="primary"
type="submit"
disabled={isSubmitting}
>
{isSubmitting
? intl.formatMessage(messages.saving)
: intl.formatMessage(messages.save)}
</Button>
</span>
</div>
</div>
</Form>
);
}}
</Formik>
);
};
export default NotificationsDiscord;

View File

@@ -24,6 +24,11 @@ const settingsRoutes: SettingsRoute[] = [
route: '/settings/services',
regex: /^\/settings\/services/,
},
{
text: 'Notifications',
route: '/settings/notifications',
regex: /^\/settings\/notifications/,
},
{
text: 'Logs',
route: '/settings/logs',

View File

@@ -0,0 +1,107 @@
import Link from 'next/link';
import { useRouter } from 'next/router';
import React from 'react';
interface SettingsRoute {
text: string;
route: string;
regex: RegExp;
}
const settingsRoutes: SettingsRoute[] = [
{
text: 'General',
route: '/settings/notifications',
regex: /^\/settings\/notifications$/,
},
{
text: 'Discord',
route: '/settings/notifications/discord',
regex: /^\/settings\/notifications\/discord/,
},
];
const SettingsNotifications: React.FC = ({ children }) => {
const router = useRouter();
const activeLinkColor = 'bg-gray-700';
const inactiveLinkColor = '';
const SettingsLink: React.FC<{
route: string;
regex: RegExp;
isMobile?: boolean;
}> = ({ children, route, regex, isMobile = false }) => {
if (isMobile) {
return <option value={route}>{children}</option>;
}
return (
<Link href={route}>
<a
className={`whitespace-nowrap ml-8 first:ml-0 px-3 py-2 font-medium text-sm rounded-md ${
!!router.pathname.match(regex) ? activeLinkColor : inactiveLinkColor
}`}
aria-current="page"
>
{children}
</a>
</Link>
);
};
return (
<>
<div>
<div className="sm:hidden">
<label htmlFor="tabs" className="sr-only">
Select a tab
</label>
<select
onChange={(e) => {
router.push(e.target.value);
}}
onBlur={(e) => {
router.push(e.target.value);
}}
defaultValue={
settingsRoutes.find(
(route) => !!router.pathname.match(route.regex)
)?.route
}
aria-label="Selected tab"
className="bg-gray-800 text-white mt-1 rounded-md form-select block w-full pl-3 pr-10 py-2 text-base leading-6 border-gray-700 focus:outline-none focus:ring-blue focus:border-blue-300 sm:text-sm sm:leading-5 transition ease-in-out duration-150"
>
{settingsRoutes.map((route, index) => (
<SettingsLink
route={route.route}
regex={route.regex}
isMobile
key={`mobile-settings-link-${index}`}
>
{route.text}
</SettingsLink>
))}
</select>
</div>
<div className="hidden sm:block">
<nav className="flex space-x-4" aria-label="Tabs">
{settingsRoutes.map((route, index) => (
<SettingsLink
route={route.route}
regex={route.regex}
key={`standard-settings-link-${index}`}
>
{route.text}
</SettingsLink>
))}
</nav>
</div>
</div>
<div className="mt-10">{children}</div>
</>
);
};
export default SettingsNotifications;

View File

@@ -189,7 +189,7 @@ const TvDetails: React.FC<TvDetailsProps> = ({ tv }) => {
/>
</div>
<div className="text-white flex flex-col mr-4 mt-4 md:mt-0 text-center md:text-left">
<div className="mb-2 md:mb-0">
<div className="mb-2">
{data.mediaInfo?.status === MediaStatus.AVAILABLE && (
<Badge badgeType="success">Available</Badge>
)}