import SideNav from './SideNav' import { ReactNode, useCallback, useEffect, useState } from 'react' import { ArrowRightIcon, ChevronRightIcon } from '@heroicons/react/solid' import { useViewport } from '../hooks/useViewport' import { breakpoints } from '../utils/theme' import { useTranslation } from 'next-i18next' import mangoStore from '../store/mangoStore' import BottomBar from './mobile/BottomBar' import useLocalStorageState from '../hooks/useLocalStorageState' import UserSetupModal from './modals/UserSetupModal' import { ConnectWalletButton } from './wallet/ConnectWalletButton' import ConnectedMenu from './wallet/ConnectedMenu' import WalletIcon from './icons/WalletIcon' import BounceLoader from './shared/BounceLoader' import MangoAccountsList from './MangoAccountsList' import CreateAccountModal from './modals/CreateAccountModal' import { LinkButton } from './shared/Button' import { IS_ONBOARDED_KEY } from '../utils/constants' const Layout = ({ children }: { children: ReactNode }) => { const connected = mangoStore((s) => s.connected) const actions = mangoStore((s) => s.actions) const mangoAccount = mangoStore((s) => s.mangoAccount.current) const loadingMangoAccount = mangoStore((s) => s.mangoAccount.initialLoad) const { t } = useTranslation('common') const [isCollapsed, setIsCollapsed] = useState(false) const { width } = useViewport() const [isOnboarded] = useLocalStorageState(IS_ONBOARDED_KEY) const [showUserSetupModal, setShowUserSetupModal] = useState(false) const [showFirstAccountModal, setShowFirstAccountModal] = useState(false) useEffect(() => { if (mangoAccount) { const pubKey = mangoAccount.publicKey.toString() actions.fetchAccountPerformance(pubKey, 1) actions.fetchAccountInterestTotals(pubKey) } }, [actions, mangoAccount]) useEffect(() => { const collapsed = width ? width < breakpoints.xl : false setIsCollapsed(collapsed) }, [width]) useEffect(() => { if (width < breakpoints.lg) { setIsCollapsed(true) } }, [width]) useEffect(() => { if (connected && isOnboarded && !loadingMangoAccount && !mangoAccount) { setShowFirstAccountModal(true) } else { setShowFirstAccountModal(false) } }, [connected, isOnboarded, loadingMangoAccount, mangoAccount]) const handleCloseModal = useCallback(() => { setShowUserSetupModal(false) }, []) const handleShowModal = useCallback(() => { setShowUserSetupModal(true) }, []) const handleToggleSidebar = () => { setIsCollapsed(!isCollapsed) setTimeout(() => { window.dispatchEvent(new Event('resize')) }, 100) } return ( <> {connected && loadingMangoAccount ? (
) : null}
next {mangoAccount ? ( ) : !connected ? ( 🔗{t('connect-helper')} ) : (
🥭 setShowFirstAccountModal(true)} > {t('create-account')}
)}
{connected ? ( ) : isOnboarded ? ( ) : ( )}
{children}
{showUserSetupModal ? ( ) : null} {showFirstAccountModal ? ( setShowFirstAccountModal(false)} isFirstAccount /> ) : null} ) } export default Layout