import Alert from '@app/components/Common/Alert'; import Button from '@app/components/Common/Button'; import LoadingSpinner from '@app/components/Common/LoadingSpinner'; import globalMessages from '@app/i18n/globalMessages'; import { ArrowDownOnSquareIcon, BeakerIcon } from '@heroicons/react/24/outline'; import axios from 'axios'; import { Field, Form, Formik } from 'formik'; import { useEffect, useState } from 'react'; import { defineMessages, useIntl } from 'react-intl'; import { useToasts } from 'react-toast-notifications'; import useSWR, { mutate } from 'swr'; const messages = defineMessages({ agentenabled: 'Enable Agent', webpushsettingssaved: 'Web push notification settings saved successfully!', webpushsettingsfailed: 'Web push notification settings failed to save.', toastWebPushTestSending: 'Sending web push test notification…', toastWebPushTestSuccess: 'Web push test notification sent!', toastWebPushTestFailed: 'Web push test notification failed to send.', httpsRequirement: 'In order to receive web push notifications, Overseerr must be served over HTTPS.', }); const NotificationsWebPush = () => { const intl = useIntl(); const { addToast, removeToast } = useToasts(); const [isTesting, setIsTesting] = useState(false); const [isHttps, setIsHttps] = useState(false); const { data, error, mutate: revalidate, } = useSWR('/api/v1/settings/notifications/webpush'); useEffect(() => { setIsHttps(window.location.protocol.startsWith('https')); }, []); if (!data && !error) { return ; } return ( <> {!isHttps && ( )} { try { await axios.post('/api/v1/settings/notifications/webpush', { enabled: values.enabled, options: {}, }); mutate('/api/v1/settings/public'); addToast(intl.formatMessage(messages.webpushsettingssaved), { appearance: 'success', autoDismiss: true, }); } catch (e) { addToast(intl.formatMessage(messages.webpushsettingsfailed), { appearance: 'error', autoDismiss: true, }); } finally { revalidate(); } }} > {({ isSubmitting }) => { const testSettings = async () => { setIsTesting(true); let toastId: string | undefined; try { addToast( intl.formatMessage(messages.toastWebPushTestSending), { autoDismiss: false, appearance: 'info', }, (id) => { toastId = id; } ); await axios.post('/api/v1/settings/notifications/webpush/test', { enabled: true, options: {}, }); if (toastId) { removeToast(toastId); } addToast(intl.formatMessage(messages.toastWebPushTestSuccess), { autoDismiss: true, appearance: 'success', }); } catch (e) { if (toastId) { removeToast(toastId); } addToast(intl.formatMessage(messages.toastWebPushTestFailed), { autoDismiss: true, appearance: 'error', }); } finally { setIsTesting(false); } }; return (
); }}
); }; export default NotificationsWebPush;