import { useMemo, useState } from 'react' import { CheckIcon, DocumentDuplicateIcon, HeartIcon, PlusCircleIcon, UserPlusIcon, } from '@heroicons/react/20/solid' import { HealthType, MangoAccount, toUiDecimalsForQuote, } from '@blockworks-foundation/mango-v4' import mangoStore from '@store/mangoStore' import { IconButton, LinkButton } from '../shared/Button' import useLocalStorageState from '../../hooks/useLocalStorageState' import { LAST_ACCOUNT_KEY } from '../../utils/constants' import { useTranslation } from 'next-i18next' import { retryFn } from '../../utils' import Loading from '../shared/Loading' import Modal from '@components/shared/Modal' import CreateAccountForm from '@components/account/CreateAccountForm' import { EnterRightExitLeft } from '@components/shared/Transitions' import { useRouter } from 'next/router' import useMangoAccount from 'hooks/useMangoAccount' import useMangoGroup from 'hooks/useMangoGroup' import { notify } from 'utils/notifications' import { DEFAULT_DELEGATE } from './DelegateModal' import Tooltip from '@components/shared/Tooltip' import { abbreviateAddress } from 'utils/formatting' import { handleCopyAddress } from '@components/account/AccountActions' import useUnownedAccount from 'hooks/useUnownedAccount' import { floorToDecimal, numberCompacter } from 'utils/numbers' const MangoAccountsListModal = ({ isOpen, onClose, }: { isOpen: boolean onClose: () => void }) => { const { t } = useTranslation('common') const { isDelegatedAccount } = useUnownedAccount() const { mangoAccount, initialLoad: loading } = useMangoAccount() const mangoAccounts = mangoStore((s) => s.mangoAccounts) const actions = mangoStore.getState().actions const { group } = useMangoGroup() const [showNewAccountForm, setShowNewAccountForm] = useState(false) const [, setLastAccountViewed] = useLocalStorageState(LAST_ACCOUNT_KEY) const router = useRouter() const { asPath } = useRouter() const [submitting, setSubmitting] = useState('') const sortedMangoAccounts = useMemo(() => { if (!group) return mangoAccounts return [...mangoAccounts].sort((a, b) => { // keeps the current selected mango account at the top of the list // if (b.publicKey.toString() === mangoAccount?.publicKey.toString()) // return 1 // if (a.publicKey.toString() === mangoAccount?.publicKey.toString()) // return -1 return b.getEquity(group).toNumber() - a.getEquity(group).toNumber() }) }, [group, mangoAccounts]) const handleSelectMangoAccount = async (acc: MangoAccount) => { const set = mangoStore.getState().set const client = mangoStore.getState().client if (!group) return set((s) => { s.activityFeed.feed = [] s.activityFeed.loading = true }) setSubmitting(acc.publicKey.toString()) try { const reloadedMangoAccount = await retryFn(() => acc.reload(client)) set((s) => { s.mangoAccount.current = reloadedMangoAccount }) actions.fetchOpenOrders() setLastAccountViewed(acc.publicKey.toString()) } catch (e) { console.warn('Error selecting account', e) notify({ type: 'info', title: 'Unable to load account. Please try again.', description: `${e}`, }) } finally { onClose() setSubmitting('') } } const handleClose = () => { if (asPath !== '/') { router.push('/') } onClose() } return (

{t('accounts')}

{loading ? ( ) : mangoAccounts.length ? (
{sortedMangoAccounts.map((acc) => { if ( mangoAccount && acc.publicKey.equals(mangoAccount.publicKey) ) { acc = mangoAccount } const accountValue = floorToDecimal( toUiDecimalsForQuote(Number(acc.getEquity(group!))), 2, ) const maintHealth = acc.getHealthRatioUi( group!, HealthType.maint, ) return (
handleCopyAddress( acc.publicKey.toString(), t('copy-address-success', { pk: abbreviateAddress(acc.publicKey), }), ) } hideBg size="medium" >
) })}
) : (
😎

Create your first account

)}
setShowNewAccountForm(true)} > {t('add-new-account')}
setShowNewAccountForm(false)} />
) } export default MangoAccountsListModal