import { Row, Spin, Typography, Button } 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 { 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 { isMobileSafariHomeScreenApp, isMobileSafariIos } from '../../../utils/helpers';
import { arePushNotificationSupported } from '../../../utils/browserPushNotifications';
const { Title } = Typography;
const NotificationsNotSupported = () => (
Browser notifications are not supported in your browser.
);
const NotificationsNotSupportedLocal = () => (
Browser notifications are not supported for local servers.
);
const MobileSafariInstructions = () => (
Get notified on iOS
It takes a couple extra steps to make sure you get notified when your favorite streams go live.
Tap the share button in Safari.
Scroll down and tap “Add to Home Screen”
.
Tap “Add”.
Give this link a name and tap the new icon on your home screen
Come back to this screen and enable notifications.
Tap “Allow” when prompted.
);
export type PermissionPopupPreviewProps = {
start: () => void;
};
const PermissionPopupPreview: FC = ({ start }) => (
{window.location.toString()} wants to
Show notifications
);
const NotificationsEnabled = () => (
Notifications are enabled
To disable push notifications from {window.location.hostname.toString()} access your browser
permissions for this site and turn off notifications.
Learn more.
);
export const BrowserNotifyModal = () => {
const [error, setError] = useState(null);
const accessToken = useRecoilValue(accessTokenAtom);
const config = useRecoilValue(clientConfigStateAtom);
const [browserPushPermissionsPending, setBrowserPushPermissionsPending] =
useState(false);
const notificationsPermitted =
arePushNotificationSupported() && Notification.permission !== 'default';
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 (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(
`Error registering for live notifications: ${e.message}. Make sure you're not inside a private browser environment or have previously disabled notifications for this stream.`,
);
}
setBrowserPushPermissionsPending(false);
};
if (window.location.hostname === 'localhost') {
return ;
}
if (!browserPushSupported) {
return ;
}
return (
(
)}
>
Get notified right in the browser each time this stream goes live.
Learn more
about Owncast browser notifications.
{error} startBrowserPushRegistration()} />
);
};