import { Row, Spin, Typography, Button, Alert } from 'antd';
import React, { FC, useState } from 'react';
import UploadOutlined from '@ant-design/icons/lib/icons/UploadOutlined';
import PlusSquareOutlined from '@ant-design/icons/lib/icons/PlusSquareOutlined';
import { useRecoilValue } from 'recoil';
import { ErrorBoundary } from 'react-error-boundary';
import { useTranslation } from 'next-export-i18n';
import { accessTokenAtom, clientConfigStateAtom } from '../../stores/ClientConfigStore';
import {
registerWebPushNotifications,
saveNotificationRegistration,
} from '../../../services/notifications-service';
import styles from './BrowserNotifyModal.module.scss';
import { ComponentError } from '../../ui/ComponentError/ComponentError';
import { Translation } from '../../ui/Translation/Translation';
import { Localization } from '../../../types/localization';
import { isMobileSafariHomeScreenApp, isMobileSafariIos } from '../../../utils/helpers';
import { arePushNotificationSupported } from '../../../utils/browserPushNotifications';
const { Title } = Typography;
const NotificationsNotSupported = () => (
);
const NotificationsNotSupportedLocal = () => (
);
const MobileSafariInstructions = () => (
-
Tap the{' '}
{' '}
button in Safari.
-
Scroll down and tap{' '}
“
”
{' '}
.
-
Tap{' '}
“
”
.
-
-
-
Tap{' '}
“
”
{' '}
when prompted.
);
export type PermissionPopupPreviewProps = {
start: () => void;
};
const PermissionPopupPreview: FC = ({ start }) => (
);
const NotificationsEnabled = () => (
);
const NotificationsDenied = () => (
);
export const BrowserNotifyModal = () => {
const { t } = useTranslation();
const [error, setError] = useState(null);
const accessToken = useRecoilValue(accessTokenAtom);
const config = useRecoilValue(clientConfigStateAtom);
const [browserPushPermissionsPending, setBrowserPushPermissionsPending] =
useState(false);
const notificationsPermitted =
arePushNotificationSupported() && Notification.permission === 'granted';
const notificationsDenied =
arePushNotificationSupported() && Notification.permission === 'denied';
const { notifications } = config;
const { browser } = notifications;
const { publicKey } = browser;
const browserPushSupported =
browser.enabled && (arePushNotificationSupported() || isMobileSafariHomeScreenApp());
// If notification permissions are granted, show user info how to disable them
if (notificationsPermitted) {
return ;
}
if (notificationsDenied) {
return ;
}
if (isMobileSafariIos() && !isMobileSafariHomeScreenApp()) {
return ;
}
const startBrowserPushRegistration = async () => {
// If notification permissions are already denied or granted, don't do anything.
if (arePushNotificationSupported() && Notification.permission !== 'default') {
return;
}
setBrowserPushPermissionsPending(true);
try {
const subscription = await registerWebPushNotifications(publicKey);
saveNotificationRegistration('BROWSER_PUSH_NOTIFICATION', subscription, accessToken);
setError(null);
} catch (e) {
setError(
t(Localization.Frontend.BrowserNotifyModal.errorMessage, {
message: e.message,
}),
);
}
setBrowserPushPermissionsPending(false);
};
if (window.location.hostname === 'localhost') {
return ;
}
if (!browserPushSupported) {
return ;
}
return (
(
)}
>
about Owncast browser notifications.
{error && (
}
description={error}
type="error"
closable
className={styles.errorAlert}
onClose={() => setError(null)}
/>
)}
startBrowserPushRegistration()} />
);
};