mango-v4-ui/components/Layout.tsx

173 lines
5.5 KiB
TypeScript
Raw Normal View History

2022-08-02 19:15:17 -07:00
import SideNav from './SideNav'
2023-03-26 17:42:50 -07:00
import {
Fragment,
ReactNode,
useCallback,
useEffect,
useMemo,
useState,
} from 'react'
2023-03-06 03:48:24 -08:00
import { ArrowPathIcon, ChevronRightIcon } from '@heroicons/react/20/solid'
2022-07-26 19:45:27 -07:00
import { useViewport } from '../hooks/useViewport'
import { breakpoints } from '../utils/theme'
2022-09-12 08:53:57 -07:00
import mangoStore from '@store/mangoStore'
2022-07-26 19:45:27 -07:00
import BottomBar from './mobile/BottomBar'
2022-07-28 04:41:55 -07:00
import BounceLoader from './shared/BounceLoader'
import TopBar from './TopBar'
import useLocalStorageState from '../hooks/useLocalStorageState'
import { ACCEPT_TERMS_KEY, SIDEBAR_COLLAPSE_KEY } from '../utils/constants'
2022-11-15 20:12:51 -08:00
import { useWallet } from '@solana/wallet-adapter-react'
2023-01-02 04:19:30 -08:00
import SuccessParticles from './shared/SuccessParticles'
2022-11-21 19:23:54 -08:00
import { tsParticles } from 'tsparticles-engine'
import { loadFull } from 'tsparticles'
2023-03-06 03:48:24 -08:00
import useInterval from './shared/useInterval'
import { Transition } from '@headlessui/react'
import { useTranslation } from 'next-i18next'
import TermsOfUseModal from './modals/TermsOfUseModal'
2023-06-07 19:30:56 -07:00
import { ttCommons, ttCommonsExpanded, ttCommonsMono } from 'utils/fonts'
2022-07-26 19:45:27 -07:00
2023-06-06 05:47:42 -07:00
export const sideBarAnimationDuration = 300
const termsLastUpdated = 1679441610978
2022-09-21 18:23:29 -07:00
2022-07-26 19:45:27 -07:00
const Layout = ({ children }: { children: ReactNode }) => {
2022-11-15 20:12:51 -08:00
const { connected } = useWallet()
const loadingMangoAccount = mangoStore((s) => s.mangoAccount.initialLoad)
const [isCollapsed, setIsCollapsed] = useLocalStorageState(
SIDEBAR_COLLAPSE_KEY,
false
)
const [acceptTerms, setAcceptTerms] = useLocalStorageState(
ACCEPT_TERMS_KEY,
''
)
2022-07-26 19:45:27 -07:00
const { width } = useViewport()
2022-08-13 04:29:48 -07:00
2022-07-26 19:45:27 -07:00
useEffect(() => {
if (width < breakpoints.lg) {
setIsCollapsed(true)
}
}, [width])
2022-09-21 18:23:29 -07:00
useEffect(() => {
2022-09-29 21:21:23 -07:00
const animationFrames = 15
2022-09-21 18:23:29 -07:00
for (let x = 1; x <= animationFrames; x++) {
setTimeout(() => {
window.dispatchEvent(new Event('resize'))
}, (sideBarAnimationDuration / animationFrames) * x)
}
}, [isCollapsed])
2022-07-26 19:45:27 -07:00
const handleToggleSidebar = () => {
setIsCollapsed(!isCollapsed)
}
2022-11-21 19:23:54 -08:00
const particlesInit = useCallback(async () => {
await loadFull(tsParticles)
}, [])
useEffect(() => {
particlesInit()
}, [])
2023-03-26 17:42:50 -07:00
const showTermsOfUse = useMemo(() => {
return (!acceptTerms || acceptTerms < termsLastUpdated) && connected
}, [acceptTerms, connected])
2022-07-26 19:45:27 -07:00
return (
2023-06-07 19:30:56 -07:00
<main
className={`${ttCommons.variable} ${ttCommonsExpanded.variable} ${ttCommonsMono.variable} font-sans`}
>
2022-11-21 19:23:54 -08:00
<div className="fixed z-30">
2023-01-02 04:19:30 -08:00
<SuccessParticles />
2022-11-21 19:23:54 -08:00
</div>
{connected && loadingMangoAccount ? (
2022-07-28 04:41:55 -07:00
<div className="fixed z-30 flex h-screen w-full items-center justify-center bg-[rgba(0,0,0,0.7)]">
<BounceLoader />
</div>
) : null}
2022-09-29 21:21:23 -07:00
<div className="flex-grow bg-th-bkg-1 text-th-fgd-2 transition-all">
2022-12-19 23:00:54 -08:00
<div className="fixed bottom-0 left-0 z-20 w-full md:hidden">
<BottomBar />
</div>
2023-01-11 20:45:04 -08:00
<div className="fixed z-20 hidden h-screen md:block">
2022-12-19 23:00:54 -08:00
<button
2023-04-19 18:01:31 -07:00
className="absolute right-0 top-1/2 z-20 hidden h-8 w-3 -translate-y-1/2 rounded-none rounded-l bg-th-bkg-3 hover:bg-th-bkg-4 focus:outline-none focus-visible:bg-th-bkg-4 lg:block"
2022-12-19 23:00:54 -08:00
onClick={handleToggleSidebar}
>
<ChevronRightIcon
className={`absolute bottom-2 -right-[2px] h-4 w-4 flex-shrink-0 ${
!isCollapsed ? 'rotate-180' : 'rotate-360'
}`}
/>
</button>
<div
className={`thin-scroll h-full ${
!isCollapsed ? 'overflow-y-auto' : ''
}`}
>
2022-12-19 23:00:54 -08:00
<SideNav collapsed={isCollapsed} />
</div>
2022-12-19 23:00:54 -08:00
</div>
{/* note: overflow-x-hidden below prevents position sticky from working in activity feed */}
2022-12-19 23:00:54 -08:00
<div
className={`w-full overflow-x-hidden transition-all duration-${sideBarAnimationDuration} ease-in-out ${
isCollapsed ? 'md:pl-[64px]' : 'md:pl-44 lg:pl-48 xl:pl-52'
}`}
>
2023-05-14 21:30:00 -07:00
<TopBar />
2022-12-19 23:00:54 -08:00
{children}
2022-07-26 19:45:27 -07:00
</div>
2023-03-06 03:48:24 -08:00
<DeployRefreshManager />
2022-07-26 19:45:27 -07:00
</div>
{showTermsOfUse ? (
<TermsOfUseModal
isOpen={showTermsOfUse}
onClose={() => setAcceptTerms(Date.now())}
/>
) : null}
2023-06-07 19:30:56 -07:00
</main>
2022-07-26 19:45:27 -07:00
)
}
export default Layout
2023-03-06 03:48:24 -08:00
function DeployRefreshManager(): JSX.Element | null {
const { t } = useTranslation('common')
const [newBuildAvailable, setNewBuildAvailable] = useState(false)
useInterval(async () => {
const response = await fetch('/api/build-id')
const { buildId } = await response.json()
if (buildId && process.env.BUILD_ID && buildId !== process.env.BUILD_ID) {
// There's a new version deployed that we need to load
setNewBuildAvailable(true)
}
}, 30000)
return (
<Transition
appear={true}
show={newBuildAvailable}
as={Fragment}
enter="transition ease-in duration-300"
enterFrom="translate-y-0"
enterTo="-translate-y-[130px] md:-translate-y-20"
leave="transition ease-out"
leaveFrom="opacity-100"
leaveTo="opacity-0"
>
<button
2023-04-19 18:01:31 -07:00
className="fixed -bottom-[46px] left-1/2 z-50 flex -translate-x-1/2 items-center rounded-full border border-th-bkg-4 bg-th-bkg-3 py-3 px-4 shadow-md focus:outline-none md:hover:bg-th-bkg-4 md:hover:shadow-none"
2023-03-06 03:48:24 -08:00
onClick={() => window.location.reload()}
>
<p className="mr-2 whitespace-nowrap text-th-fgd-1">
{t('new-version')}
</p>
<ArrowPathIcon className="h-5 w-5" />
</button>
</Transition>
)
}