import { useState } from 'react' import { CheckIcon, HeartIcon, PlusCircleIcon } from '@heroicons/react/20/solid' import { HealthType, MangoAccount, toUiDecimalsForQuote, } from '@blockworks-foundation/mango-v4' import mangoStore from '@store/mangoStore' import { LinkButton } from '../shared/Button' import { useLocalStorageStringState } 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 { formatFixedDecimals } from 'utils/numbers' import CreateAccountForm from '@components/account/CreateAccountForm' import { EnterRightExitLeft } from '@components/shared/Transitions' const MangoAccountsListModal = ({ mangoAccount, isOpen, onClose, }: { mangoAccount: MangoAccount | undefined isOpen: boolean onClose: () => void }) => { const { t } = useTranslation('common') const mangoAccounts = mangoStore((s) => s.mangoAccounts) const actions = mangoStore((s) => s.actions) const group = mangoStore((s) => s.group) const loading = mangoStore((s) => s.mangoAccount.initialLoad) const [showNewAccountForm, setShowNewAccountForm] = useState(false) const [, setLastAccountViewed] = useLocalStorageStringState(LAST_ACCOUNT_KEY) 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 }) try { const reloadedMangoAccount = await retryFn(() => acc.reload(client)) set((s) => { s.mangoAccount.current = reloadedMangoAccount s.mangoAccount.lastUpdatedAt = new Date().toISOString() }) setLastAccountViewed(acc.publicKey.toString()) actions.fetchSerumOpenOrders(acc) } catch (e) { console.warn('Error selecting account', e) } finally { onClose() } } return (

{t('accounts')}

{loading ? ( ) : mangoAccounts.length ? (
{mangoAccounts.map((acc) => { const accountValue = formatFixedDecimals( toUiDecimalsForQuote(Number(acc.getEquity(group!))), true ) const maintHealth = acc.getHealthRatioUi( group!, HealthType.maint ) return (
) })}
) : (
😎

Create your first account

)}
setShowNewAccountForm(true)} > New Sub-account
setShowNewAccountForm(false)} handleBack={() => setShowNewAccountForm(false)} />
) } export default MangoAccountsListModal