import loadable from '@loadable/component' import { FC, useEffect, useRef, useState } from 'react' import { Route, Routes, useLocation, useNavigationType, useSearchParams } from 'react-router-dom' import { StudioLoading } from '@/components/_loaders/StudioLoading' import { CookiePopover } from '@/components/_overlays/CookiePopover' import { atlasConfig } from '@/config' import { BASE_PATHS, absoluteRoutes } from '@/config/routes' import { useSegmentAnalytics } from '@/hooks/useSegmentAnalytics' import { transitions } from '@/styles' import { RoutingState } from '@/types/routing' import { isBrowserOutdated } from '@/utils/browser' import { SentryLogger } from '@/utils/logs' import { AppLogo } from './components/AppLogo' import { TopbarBase } from './components/_navigation/TopbarBase' import { useConfirmationModal } from './providers/confirmationModal' import { useOverlayManager } from './providers/overlayManager' import { LegalLayout } from './views/legal/LegalLayout' import { ViewerLayout } from './views/viewer/ViewerLayout' history.scrollRestoration = 'manual' const ROUTING_ANIMATION_OFFSET = 100 const LoadableStudioLayout = loadable(() => import('./views/studio/StudioLayout'), { fallback: ( <> } logoLinkUrl={absoluteRoutes.studio.index()} /> ), }) const LoadablePlaygroundLayout = loadable(() => import('./views/playground/PlaygroundLayout'), { fallback:

Loading Playground...

, }) export const MainLayout: FC = () => { const scrollPosition = useRef(0) const location = useLocation() const [searchParams] = useSearchParams() const navigationType = useNavigationType() const [cachedLocation, setCachedLocation] = useState(location) const locationState = location.state as RoutingState const [openDialog, closeDialog] = useConfirmationModal({ title: 'Outdated browser detected', description: 'It seems the browser version you are using is not fully supported by Joystream. Some of the features may be broken or not accessible. For the best experience, please upgrade your browser to the latest version.', type: 'warning', primaryButton: { text: 'Click here to see instructions', onClick: () => window.open('https://www.whatismybrowser.com/guides/how-to-update-your-browser/auto'), }, onExitClick: () => closeDialog(), }) const { trackPageView } = useSegmentAnalytics() useEffect(() => { // had to include this timeout to make sure the page title is updated const trackRequestTimeout = setTimeout( () => trackPageView( document.title, 'viewer', (location.pathname === absoluteRoutes.viewer.ypp() && searchParams.get('referrer')) || undefined ), 1000 ) if (!atlasConfig.analytics.sentry?.dsn) { return } const stopReplay = async () => await SentryLogger.replay?.stop() if (location.pathname === absoluteRoutes.viewer.ypp()) { if (SentryLogger.initialized && !SentryLogger.replay?.getReplayId()) { SentryLogger.replay?.start() } } else { if (SentryLogger.replay?.getReplayId()) { stopReplay() } } return () => { clearTimeout(trackRequestTimeout) } }, [location.pathname, trackPageView, searchParams]) const { clearOverlays } = useOverlayManager() useEffect(() => { if (isBrowserOutdated) { openDialog() } }, [openDialog]) useEffect(() => { if (location.pathname === cachedLocation.pathname) { return } clearOverlays() setCachedLocation(location) if (locationState?.overlaidLocation?.pathname === location.pathname) { // if exiting routing overlay, skip scroll to top return } if (navigationType !== 'POP') { scrollPosition.current = window.scrollY } // delay scroll to allow transition to finish first setTimeout(() => { window.scrollTo(0, navigationType !== 'POP' ? 0 : scrollPosition.current) }, parseInt(transitions.timings.routing) + ROUTING_ANIMATION_OFFSET) }, [location, cachedLocation, locationState, navigationType, clearOverlays]) return ( <> } /> } /> } /> } /> ) }