1
0

MainLayout.tsx 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import loadable from '@loadable/component'
  2. import { FC, useEffect, useRef, useState } from 'react'
  3. import { Route, Routes, useLocation, useNavigationType } from 'react-router-dom'
  4. import { StudioLoading } from '@/components/_loaders/StudioLoading'
  5. import { CookiePopover } from '@/components/_overlays/CookiePopover'
  6. import { BASE_PATHS, absoluteRoutes } from '@/config/routes'
  7. import { transitions } from '@/styles'
  8. import { RoutingState } from '@/types/routing'
  9. import { isBrowserOutdated } from '@/utils/browser'
  10. import { AppLogo } from './components/AppLogo'
  11. import { TopbarBase } from './components/_navigation/TopbarBase'
  12. import { useConfirmationModal } from './providers/confirmationModal'
  13. import { LegalLayout } from './views/legal/LegalLayout'
  14. import { ViewerLayout } from './views/viewer/ViewerLayout'
  15. history.scrollRestoration = 'manual'
  16. const ROUTING_ANIMATION_OFFSET = 100
  17. const LoadableStudioLayout = loadable(() => import('./views/studio/StudioLayout'), {
  18. fallback: (
  19. <>
  20. <TopbarBase
  21. fullLogoNode={<AppLogo variant="studio" height={32} width={undefined} />}
  22. logoLinkUrl={absoluteRoutes.studio.index()}
  23. />
  24. <StudioLoading />
  25. </>
  26. ),
  27. })
  28. const LoadablePlaygroundLayout = loadable(() => import('./views/playground/PlaygroundLayout'), {
  29. fallback: <h1>Loading Playground...</h1>,
  30. })
  31. export const MainLayout: FC = () => {
  32. const scrollPosition = useRef<number>(0)
  33. const location = useLocation()
  34. const navigationType = useNavigationType()
  35. const [cachedLocation, setCachedLocation] = useState(location)
  36. const locationState = location.state as RoutingState
  37. const [openDialog, closeDialog] = useConfirmationModal({
  38. title: 'Outdated browser detected',
  39. description:
  40. '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.',
  41. type: 'warning',
  42. primaryButton: {
  43. text: 'Click here to see instructions',
  44. onClick: () => window.open('https://www.whatismybrowser.com/guides/how-to-update-your-browser/auto'),
  45. },
  46. onExitClick: () => closeDialog(),
  47. })
  48. useEffect(() => {
  49. if (isBrowserOutdated) {
  50. openDialog()
  51. }
  52. }, [openDialog])
  53. useEffect(() => {
  54. if (location.pathname === cachedLocation.pathname) {
  55. return
  56. }
  57. setCachedLocation(location)
  58. if (locationState?.overlaidLocation?.pathname === location.pathname) {
  59. // if exiting routing overlay, skip scroll to top
  60. return
  61. }
  62. if (navigationType !== 'POP') {
  63. scrollPosition.current = window.scrollY
  64. }
  65. // delay scroll to allow transition to finish first
  66. setTimeout(() => {
  67. window.scrollTo(0, navigationType !== 'POP' ? 0 : scrollPosition.current)
  68. }, parseInt(transitions.timings.routing) + ROUTING_ANIMATION_OFFSET)
  69. }, [location, cachedLocation, locationState, navigationType])
  70. return (
  71. <>
  72. <CookiePopover />
  73. <Routes>
  74. <Route path={BASE_PATHS.viewer + '/*'} element={<ViewerLayout />} />
  75. <Route path={BASE_PATHS.legal + '/*'} element={<LegalLayout />} />
  76. <Route path={BASE_PATHS.studio + '/*'} element={<LoadableStudioLayout />} />
  77. <Route path={BASE_PATHS.playground + '/*'} element={<LoadablePlaygroundLayout />} />
  78. </Routes>
  79. </>
  80. )
  81. }