mango-v4-ui/components/Layout.tsx

105 lines
3.3 KiB
TypeScript
Raw Normal View History

2022-08-02 19:15:17 -07:00
import SideNav from './SideNav'
2022-11-21 19:23:54 -08:00
import { ReactNode, useCallback, useEffect } from 'react'
2022-09-06 21:36:35 -07:00
import { 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'
2022-09-21 21:25:24 -07:00
import { SIDEBAR_COLLAPSE_KEY } from '../utils/constants'
2022-11-15 20:12:51 -08:00
import { useWallet } from '@solana/wallet-adapter-react'
2022-11-21 19:23:54 -08:00
import SwapSuccessParticles from './swap/SwapSuccessParticles'
import { tsParticles } from 'tsparticles-engine'
import { loadFull } from 'tsparticles'
2022-07-26 19:45:27 -07:00
2022-09-21 18:23:29 -07:00
const sideBarAnimationDuration = 500
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
)
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()
}, [])
2022-07-26 19:45:27 -07:00
return (
2022-07-28 04:41:55 -07:00
<>
2022-11-21 19:23:54 -08:00
<div className="fixed z-30">
<SwapSuccessParticles />
</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-07-28 04:41:55 -07:00
<div className="flex">
<div className="fixed bottom-0 left-0 z-20 w-full md:hidden">
<BottomBar />
</div>
<div className="fixed z-20 hidden h-screen md:block">
<button
2022-10-25 20:23:46 -07:00
className="default-transition absolute right-0 top-1/2 z-20 hidden h-8 w-3 -translate-y-1/2 rounded-none rounded-l bg-th-bkg-4 hover:bg-th-bkg-4 focus:outline-none lg:block"
onClick={handleToggleSidebar}
>
<ChevronRightIcon
2022-09-16 05:26:30 -07:00
className={`absolute bottom-2 -right-[2px] h-4 w-4 flex-shrink-0 ${
!isCollapsed ? 'rotate-180' : 'rotate-360'
}`}
/>
</button>
<div className={`h-full ${!isCollapsed ? 'overflow-y-auto' : ''}`}>
<SideNav collapsed={isCollapsed} />
2022-07-28 04:41:55 -07:00
</div>
</div>
2022-07-28 04:41:55 -07:00
<div
2022-09-21 18:23:29 -07:00
className={`w-full transition-all duration-${sideBarAnimationDuration} ease-in-out ${
isCollapsed ? 'md:pl-[64px]' : 'md:pl-44 lg:pl-48 xl:pl-52'
2022-07-28 04:41:55 -07:00
}`}
>
2022-10-25 04:09:36 -07:00
<div className="flex h-16 items-center justify-between border-b border-th-bkg-3 bg-th-bkg-1 pl-4 md:pl-6">
<TopBar />
2022-07-26 19:45:27 -07:00
</div>
2022-09-15 18:58:35 -07:00
{children}
2022-07-26 19:45:27 -07:00
</div>
</div>
</div>
2022-07-28 04:41:55 -07:00
</>
2022-07-26 19:45:27 -07:00
)
}
export default Layout