2022-08-02 19:15:17 -07:00
|
|
|
import SideNav from './SideNav'
|
2022-08-05 15:11:07 -07:00
|
|
|
import { ReactNode, useCallback, useEffect, useState } from 'react'
|
|
|
|
import { ArrowRightIcon, ChevronRightIcon } from '@heroicons/react/solid'
|
2022-07-26 19:45:27 -07:00
|
|
|
import { useViewport } from '../hooks/useViewport'
|
|
|
|
import { breakpoints } from '../utils/theme'
|
|
|
|
import { useTranslation } from 'next-i18next'
|
2022-08-20 11:17:57 -07:00
|
|
|
import mangoStore from '../store/mangoStore'
|
2022-07-26 19:45:27 -07:00
|
|
|
import BottomBar from './mobile/BottomBar'
|
|
|
|
import useLocalStorageState from '../hooks/useLocalStorageState'
|
|
|
|
import UserSetupModal from './modals/UserSetupModal'
|
|
|
|
import { ConnectWalletButton } from './wallet/ConnectWalletButton'
|
2022-07-26 21:40:17 -07:00
|
|
|
import ConnectedMenu from './wallet/ConnectedMenu'
|
2022-07-26 23:06:30 -07:00
|
|
|
import WalletIcon from './icons/WalletIcon'
|
2022-07-28 04:41:55 -07:00
|
|
|
import BounceLoader from './shared/BounceLoader'
|
2022-08-05 15:11:07 -07:00
|
|
|
import MangoAccountsList from './MangoAccountsList'
|
2022-08-11 21:20:17 -07:00
|
|
|
import CreateAccountModal from './modals/CreateAccountModal'
|
2022-08-16 05:20:53 -07:00
|
|
|
import { LinkButton } from './shared/Button'
|
2022-08-20 19:57:07 -07:00
|
|
|
import { IS_ONBOARDED_KEY } from '../utils/constants'
|
2022-07-26 19:45:27 -07:00
|
|
|
|
|
|
|
const Layout = ({ children }: { children: ReactNode }) => {
|
2022-08-19 23:06:00 -07:00
|
|
|
const connected = mangoStore((s) => s.connected)
|
2022-08-13 04:29:48 -07:00
|
|
|
const actions = mangoStore((s) => s.actions)
|
2022-07-27 23:35:18 -07:00
|
|
|
const mangoAccount = mangoStore((s) => s.mangoAccount.current)
|
2022-08-19 14:17:30 -07:00
|
|
|
const loadingMangoAccount = mangoStore((s) => s.mangoAccount.initialLoad)
|
2022-07-26 19:45:27 -07:00
|
|
|
const { t } = useTranslation('common')
|
|
|
|
const [isCollapsed, setIsCollapsed] = useState(false)
|
|
|
|
const { width } = useViewport()
|
|
|
|
const [isOnboarded] = useLocalStorageState(IS_ONBOARDED_KEY)
|
|
|
|
const [showUserSetupModal, setShowUserSetupModal] = useState(false)
|
2022-08-11 21:20:17 -07:00
|
|
|
const [showFirstAccountModal, setShowFirstAccountModal] = useState(false)
|
2022-07-26 19:45:27 -07:00
|
|
|
|
2022-08-13 04:29:48 -07:00
|
|
|
useEffect(() => {
|
|
|
|
if (mangoAccount) {
|
|
|
|
const pubKey = mangoAccount.publicKey.toString()
|
|
|
|
actions.fetchAccountPerformance(pubKey, 1)
|
|
|
|
actions.fetchAccountInterestTotals(pubKey)
|
|
|
|
}
|
|
|
|
}, [actions, mangoAccount])
|
|
|
|
|
2022-07-26 19:45:27 -07:00
|
|
|
useEffect(() => {
|
|
|
|
const collapsed = width ? width < breakpoints.xl : false
|
|
|
|
setIsCollapsed(collapsed)
|
|
|
|
}, [width])
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (width < breakpoints.lg) {
|
|
|
|
setIsCollapsed(true)
|
|
|
|
}
|
|
|
|
}, [width])
|
|
|
|
|
2022-08-11 21:20:17 -07:00
|
|
|
useEffect(() => {
|
|
|
|
if (connected && isOnboarded && !loadingMangoAccount && !mangoAccount) {
|
|
|
|
setShowFirstAccountModal(true)
|
2022-08-15 22:10:17 -07:00
|
|
|
} else {
|
|
|
|
setShowFirstAccountModal(false)
|
2022-08-11 21:20:17 -07:00
|
|
|
}
|
|
|
|
}, [connected, isOnboarded, loadingMangoAccount, mangoAccount])
|
|
|
|
|
2022-07-26 19:45:27 -07:00
|
|
|
const handleCloseModal = useCallback(() => {
|
|
|
|
setShowUserSetupModal(false)
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
const handleShowModal = useCallback(() => {
|
|
|
|
setShowUserSetupModal(true)
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
const handleToggleSidebar = () => {
|
|
|
|
setIsCollapsed(!isCollapsed)
|
|
|
|
setTimeout(() => {
|
|
|
|
window.dispatchEvent(new Event('resize'))
|
|
|
|
}, 100)
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2022-07-28 04:41:55 -07:00
|
|
|
<>
|
2022-08-22 18:47:08 -07:00
|
|
|
{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-08-22 10:10:35 -07:00
|
|
|
<div className="flex-grow bg-th-bkg-1 text-th-fgd-1 transition-all">
|
2022-07-28 04:41:55 -07:00
|
|
|
<div className="flex">
|
2022-08-22 10:10:35 -07:00
|
|
|
<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
|
|
|
|
className="absolute -right-4 top-1/2 z-20 hidden h-10 w-4 -translate-y-1/2 rounded-none rounded-r bg-th-bkg-4 focus:outline-none lg:block"
|
|
|
|
onClick={handleToggleSidebar}
|
|
|
|
>
|
|
|
|
<ChevronRightIcon
|
|
|
|
className={`h-full w-full ${
|
|
|
|
!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>
|
2022-08-22 10:10:35 -07:00
|
|
|
</div>
|
|
|
|
|
2022-07-28 04:41:55 -07:00
|
|
|
<div
|
|
|
|
className={`w-full overflow-hidden transition-all duration-500 ease-in-out ${
|
2022-08-22 10:10:35 -07:00
|
|
|
isCollapsed ? 'md:pl-[64px]' : 'md:pl-44 lg:pl-56'
|
2022-07-28 04:41:55 -07:00
|
|
|
}`}
|
|
|
|
>
|
2022-08-03 04:16:30 -07:00
|
|
|
<div className="flex h-16 items-center justify-between border-b border-th-bkg-3 bg-th-bkg-1 px-6 md:px-8">
|
2022-07-28 04:41:55 -07:00
|
|
|
<div className="flex items-center text-th-fgd-3">
|
2022-08-22 10:10:35 -07:00
|
|
|
<img
|
|
|
|
className="mr-4 h-8 w-auto md:hidden"
|
|
|
|
src="/logos/logo-mark.svg"
|
|
|
|
alt="next"
|
|
|
|
/>
|
2022-07-28 04:41:55 -07:00
|
|
|
<span className="mb-0 mr-2">
|
|
|
|
{mangoAccount ? (
|
2022-08-01 18:08:08 -07:00
|
|
|
<MangoAccountsList mangoAccount={mangoAccount} />
|
2022-08-22 10:10:35 -07:00
|
|
|
) : !connected ? (
|
|
|
|
<span className="hidden items-center md:flex">
|
|
|
|
🔗<span className="ml-2">{t('connect-helper')}</span>
|
|
|
|
<ArrowRightIcon className="sideways-bounce ml-2 h-5 w-5 text-th-fgd-1" />
|
|
|
|
</span>
|
|
|
|
) : (
|
|
|
|
<div className="hidden items-center md:flex">
|
|
|
|
🥭
|
|
|
|
<LinkButton
|
|
|
|
onClick={() => setShowFirstAccountModal(true)}
|
|
|
|
>
|
|
|
|
<span className="ml-2">{t('create-account')}</span>
|
|
|
|
</LinkButton>
|
|
|
|
</div>
|
|
|
|
)}
|
2022-07-28 04:41:55 -07:00
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
<div className="flex items-center space-x-4">
|
2022-08-16 05:20:53 -07:00
|
|
|
{connected ? (
|
2022-07-28 04:41:55 -07:00
|
|
|
<ConnectedMenu />
|
|
|
|
) : isOnboarded ? (
|
|
|
|
<ConnectWalletButton />
|
2022-07-26 19:45:27 -07:00
|
|
|
) : (
|
2022-07-28 04:41:55 -07:00
|
|
|
<button
|
|
|
|
className="relative flex h-16 items-center justify-center rounded-none bg-gradient-to-bl from-mango-theme-yellow to-mango-theme-red-dark px-6 text-base font-bold text-white before:absolute before:inset-0 before:bg-gradient-to-r before:from-transparent before:via-[rgba(255,255,255,0.25)] before:to-transparent before:opacity-0 hover:cursor-pointer hover:overflow-hidden hover:before:-translate-x-full hover:before:animate-[shimmer_0.75s_normal] hover:before:opacity-100"
|
|
|
|
onClick={handleShowModal}
|
|
|
|
>
|
|
|
|
<WalletIcon className="mr-2 h-5 w-5 flex-shrink-0" />
|
|
|
|
{t('connect')}
|
|
|
|
</button>
|
2022-07-26 19:45:27 -07:00
|
|
|
)}
|
2022-07-28 04:41:55 -07:00
|
|
|
</div>
|
2022-07-26 19:45:27 -07:00
|
|
|
</div>
|
2022-08-22 10:10:35 -07:00
|
|
|
<div className="min-h-screen p-6 pb-20 md:p-8">{children}</div>
|
2022-07-26 19:45:27 -07:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
{showUserSetupModal ? (
|
|
|
|
<UserSetupModal
|
|
|
|
isOpen={showUserSetupModal}
|
|
|
|
onClose={handleCloseModal}
|
|
|
|
/>
|
|
|
|
) : null}
|
2022-08-11 21:20:17 -07:00
|
|
|
{showFirstAccountModal ? (
|
|
|
|
<CreateAccountModal
|
|
|
|
isOpen={showFirstAccountModal}
|
|
|
|
onClose={() => setShowFirstAccountModal(false)}
|
|
|
|
isFirstAccount
|
|
|
|
/>
|
|
|
|
) : null}
|
2022-07-28 04:41:55 -07:00
|
|
|
</>
|
2022-07-26 19:45:27 -07:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Layout
|