import { useRecoilState, useRecoilValue } from 'recoil'; import { Skeleton, Row, Button, Spin } from 'antd'; import MessageFilled from '@ant-design/icons/MessageFilled'; import { FC, useEffect, useState } from 'react'; import dynamic from 'next/dynamic'; import classnames from 'classnames'; import ActionButtons from './ActionButtons'; import { LOCAL_STORAGE_KEYS, getLocalStorage, setLocalStorage } from '../../../utils/localStorage'; import { canPushNotificationsBeSupported } from '../../../utils/browserPushNotifications'; import { clientConfigStateAtom, chatMessagesAtom, currentUserAtom, ChatState, chatStateAtom, appStateAtom, isOnlineSelector, isMobileAtom, serverStatusState, isChatAvailableSelector, } from '../../stores/ClientConfigStore'; import { ClientConfig } from '../../../interfaces/client-config.model'; import styles from './Content.module.scss'; import desktopStyles from './DesktopContent.module.scss'; import { OfflineBanner } from '../OfflineBanner/OfflineBanner'; import { AppStateOptions } from '../../stores/application-state'; import { ServerStatus } from '../../../interfaces/server-status.model'; import { Statusbar } from '../Statusbar/Statusbar'; import { ChatMessage } from '../../../interfaces/chat-message.model'; import { ExternalAction } from '../../../interfaces/external-action'; import { Modal } from '../Modal/Modal'; import { DesktopContent } from './DesktopContent'; import { MobileContent } from './MobileContent'; import { ChatModal } from '../../modals/ChatModal/ChatModal'; import { Footer } from '../Footer/Footer'; // Lazy loaded components const ChatContainer = dynamic( () => import('../../chat/ChatContainer/ChatContainer').then(mod => mod.ChatContainer), { ssr: false, }, ); const FollowModal = dynamic( () => import('../../modals/FollowModal/FollowModal').then(mod => mod.FollowModal), { ssr: false, loading: () => , }, ); const BrowserNotifyModal = dynamic( () => import('../../modals/BrowserNotifyModal/BrowserNotifyModal').then( mod => mod.BrowserNotifyModal, ), { ssr: false, loading: () => , }, ); const OwncastPlayer = dynamic( () => import('../../video/OwncastPlayer/OwncastPlayer').then(mod => mod.OwncastPlayer), { ssr: false, loading: () => , }, ); const ExternalModal = ({ externalActionToDisplay, setExternalActionToDisplay }) => { const { title, description, url, html } = externalActionToDisplay; return ( setExternalActionToDisplay(null)} > {html ? (
) : null} ); }; export const Content: FC = () => { const appState = useRecoilValue(appStateAtom); const clientConfig = useRecoilValue(clientConfigStateAtom); const chatState = useRecoilValue(chatStateAtom); const currentUser = useRecoilValue(currentUserAtom); const serverStatus = useRecoilValue(serverStatusState); const [isMobile, setIsMobile] = useRecoilState(isMobileAtom); const messages = useRecoilValue(chatMessagesAtom); const online = useRecoilValue(isOnlineSelector); const isChatAvailable = useRecoilValue(isChatAvailableSelector); const { viewerCount, lastConnectTime, lastDisconnectTime, streamTitle } = useRecoilValue(serverStatusState); const { extraPageContent, name, summary, socialHandles, tags, externalActions, offlineMessage, chatDisabled, federation, notifications, } = clientConfig; const [showNotifyReminder, setShowNotifyReminder] = useState(false); const [showNotifyModal, setShowNotifyModal] = useState(false); const [showFollowModal, setShowFollowModal] = useState(false); const { account: fediverseAccount, enabled: fediverseEnabled } = federation; const { browser: browserNotifications } = notifications; const { enabled: browserNotificationsEnabled } = browserNotifications; const { online: isStreamLive } = serverStatus; const [externalActionToDisplay, setExternalActionToDisplay] = useState(null); const [currentBrowserWindowUrl, setCurrentBrowserWindowUrl] = useState(''); const [supportsBrowserNotifications, setSupportsBrowserNotifications] = useState(false); const supportFediverseFeatures = fediverseEnabled; const [showChatModal, setShowChatModal] = useState(false); const externalActionSelected = (action: ExternalAction) => { const { openExternally, url } = action; const updatedUrl = new URL(url); updatedUrl.searchParams.append('instance', currentBrowserWindowUrl); if (currentUser) { const { displayName } = currentUser; // Append url and username to params so the link knows where we came from and who we are. updatedUrl.searchParams.append('username', displayName); } const fullUrl = updatedUrl.toString(); // Overwrite URL with the updated one that includes the params. const updatedAction = { ...action, url: fullUrl, }; // apply openExternally only if we don't have an HTML embed if (openExternally) { window.open(fullUrl, '_blank'); } else { setExternalActionToDisplay(updatedAction); } }; const incrementVisitCounter = () => { let visits = parseInt(getLocalStorage(LOCAL_STORAGE_KEYS.userVisitCount), 10); if (Number.isNaN(visits)) { visits = 0; } setLocalStorage(LOCAL_STORAGE_KEYS.userVisitCount, visits + 1); if (visits > 2 && !getLocalStorage(LOCAL_STORAGE_KEYS.hasDisplayedNotificationModal)) { setShowNotifyReminder(true); } }; const disableNotifyReminderPopup = () => { setShowNotifyModal(false); setShowNotifyReminder(false); setLocalStorage(LOCAL_STORAGE_KEYS.hasDisplayedNotificationModal, true); }; const checkIfMobile = () => { const w = window.innerWidth; if (isMobile === undefined) { if (w <= 768) setIsMobile(true); else setIsMobile(false); } if (!isMobile && w <= 768) setIsMobile(true); if (isMobile && w > 768) setIsMobile(false); }; useEffect(() => { incrementVisitCounter(); checkIfMobile(); window.addEventListener('resize', checkIfMobile); return () => { window.removeEventListener('resize', checkIfMobile); }; }, []); useEffect(() => { // isPushNotificationSupported relies on `navigator` so that needs to be // fired from this useEffect. setSupportsBrowserNotifications( canPushNotificationsBeSupported() && browserNotificationsEnabled, ); }, [browserNotificationsEnabled]); useEffect(() => { setCurrentBrowserWindowUrl(window.location.href); }, []); const showChat = isChatAvailable && !chatDisabled && chatState === ChatState.VISIBLE; return (
{appState.appLoading && (
)} {online && ( )} {!online && !appState.appLoading && (
setShowNotifyModal(true)} onFollowClick={() => setShowFollowModal(true)} className={classnames([styles.topSectionElement, styles.offlineBanner])} />
)}
{isStreamLive && ( )} disableNotifyReminderPopup()} handleCancel={() => disableNotifyReminderPopup()} > {!name && } {isMobile ? ( ) : (
)}
{showChat && !isMobile && currentUser && ( )} {externalActionToDisplay && ( )} setShowFollowModal(false)} width="550px" > setShowFollowModal(false)} /> {isMobile && showChatModal && chatState === ChatState.VISIBLE && ( setShowChatModal(false)} /> )} {isMobile && isChatAvailable && ( )}
); };