import React, { useCallback, useEffect, useState } from 'react' import { BellIcon, CurrencyDollarIcon, ExclamationCircleIcon, ExternalLinkIcon, LinkIcon, PencilIcon, TrashIcon, } from '@heroicons/react/outline' import useMangoStore, { serumProgramId } from '../stores/useMangoStore' import PageBodyContainer from '../components/PageBodyContainer' import TopBar from '../components/TopBar' import AccountOrders from '../components/account_page/AccountOrders' import AccountHistory from '../components/account_page/AccountHistory' import AccountsModal from '../components/AccountsModal' import AccountOverview from '../components/account_page/AccountOverview' import AccountInterest from '../components/account_page/AccountInterest' import AccountFunding from '../components/account_page/AccountFunding' import AccountPerformance from '../components/account_page/AccountPerformance' import AccountNameModal from '../components/AccountNameModal' import Button, { IconButton } from '../components/Button' import EmptyState from '../components/EmptyState' import Loading from '../components/Loading' import Swipeable from '../components/mobile/Swipeable' import Tabs from '../components/Tabs' import { useViewport } from '../hooks/useViewport' import { breakpoints } from '../components/TradePageGrid' import { serverSideTranslations } from 'next-i18next/serverSideTranslations' import { useTranslation } from 'next-i18next' import Select from '../components/Select' import { useRouter } from 'next/router' import { PublicKey } from '@solana/web3.js' import CloseAccountModal from '../components/CloseAccountModal' import { actionsSelector, mangoAccountSelector, mangoGroupSelector, walletConnectedSelector, } from '../stores/selectors' import CreateAlertModal from '../components/CreateAlertModal' export async function getStaticProps({ locale }) { return { props: { ...(await serverSideTranslations(locale, ['common', 'close-account'])), // Will be passed to the page component as props }, } } const TABS = [ 'Portfolio', 'Orders', 'History', 'Interest', 'Funding', 'Performance', ] export default function Account() { const { t } = useTranslation(['common', 'close-account']) const [showAccountsModal, setShowAccountsModal] = useState(false) const [showNameModal, setShowNameModal] = useState(false) const [showCloseAccountModal, setShowCloseAccountModal] = useState(false) const [showAlertsModal, setShowAlertsModal] = useState(false) const [isCopied, setIsCopied] = useState(false) const [resetOnLeave, setResetOnLeave] = useState(false) const connected = useMangoStore(walletConnectedSelector) const mangoAccount = useMangoStore(mangoAccountSelector) const mangoClient = useMangoStore((s) => s.connection.client) const mangoGroup = useMangoStore(mangoGroupSelector) const wallet = useMangoStore((s) => s.wallet.current) const isLoading = useMangoStore((s) => s.selectedMangoAccount.initialLoad) const actions = useMangoStore(actionsSelector) const setMangoStore = useMangoStore((s) => s.set) const [viewIndex, setViewIndex] = useState(0) const [activeTab, setActiveTab] = useState(TABS[0]) const { width } = useViewport() const isMobile = width ? width < breakpoints.sm : false const router = useRouter() const { pubkey } = router.query const handleCloseAlertModal = useCallback(() => { setShowAlertsModal(false) }, []) const handleCloseAccounts = useCallback(() => { setShowAccountsModal(false) }, []) const handleCloseNameModal = useCallback(() => { setShowNameModal(false) }, []) const handleCloseCloseAccountModal = useCallback(() => { setShowCloseAccountModal(false) }, []) useEffect(() => { async function loadUnownedMangoAccount() { try { const unownedMangoAccountPubkey = new PublicKey(pubkey) if (mangoGroup) { const unOwnedMangoAccount = await mangoClient.getMangoAccount( unownedMangoAccountPubkey, serumProgramId ) setMangoStore((state) => { state.selectedMangoAccount.current = unOwnedMangoAccount }) actions.fetchTradeHistory() setResetOnLeave(true) } } catch (error) { router.push('/account') } } if (pubkey) { loadUnownedMangoAccount() } }, [pubkey, mangoGroup]) useEffect(() => { if (connected) { router.push('/account') } }, [connected]) useEffect(() => { const handleRouteChange = () => { if (resetOnLeave) { setMangoStore((state) => { state.selectedMangoAccount.current = undefined }) } } router.events.on('routeChangeStart', handleRouteChange) return () => { router.events.off('routeChangeStart', handleRouteChange) } }, [resetOnLeave]) useEffect(() => { if (isCopied) { const timer = setTimeout(() => { setIsCopied(false) }, 1500) return () => clearTimeout(timer) } }, [isCopied]) const handleChangeViewIndex = (index) => { setViewIndex(index) } const handleTabChange = (tabName) => { setActiveTab(tabName) } return (
{mangoAccount ? ( <>

{mangoAccount?.name || t('account')}

setShowNameModal(true)}>
{mangoAccount.publicKey.toString()}
{t('account-address-warning')}
) : null}
{mangoAccount ? ( !isMobile ? ( ) : (
) ) : null}
{mangoAccount ? ( !isMobile ? ( ) : (
) ) : connected ? ( isLoading ? (
) : ( } onClickButton={() => setShowAccountsModal(true)} title={t('no-account-found')} /> ) ) : ( } onClickButton={() => wallet.connect()} title={t('connect-wallet')} /> )}
{showAccountsModal ? ( ) : null} {showNameModal ? ( ) : null} {showCloseAccountModal ? ( ) : null} {showAlertsModal ? ( ) : null}
) } const TabContent = ({ activeTab }) => { switch (activeTab) { case 'Portfolio': return case 'Orders': return case 'History': return case 'Interest': return case 'Funding': return case 'Performance': return default: return } }