import React, { FunctionComponent, useEffect, useState } from 'react' import { RadioGroup } from '@headlessui/react' import { CheckCircleIcon } from '@heroicons/react/solid' import { PlusCircleIcon, UsersIcon } from '@heroicons/react/outline' import useMangoStore from '../stores/useMangoStore' import { MangoAccount, MangoGroup } from '@blockworks-foundation/mango-client' import { abbreviateAddress, formatUsdValue } from '../utils' import useLocalStorageState from '../hooks/useLocalStorageState' import Modal from './Modal' import { ElementTitle } from './styles' import Button, { LinkButton } from './Button' import NewAccount from './NewAccount' import { useTranslation } from 'next-i18next' import Tooltip from './Tooltip' export const LAST_ACCOUNT_KEY = 'lastAccountViewed-3.0' interface AccountsModalProps { onClose: () => void isOpen: boolean } const AccountsModal: FunctionComponent = ({ isOpen, onClose, }) => { const { t } = useTranslation('common') const [showNewAccountForm, setShowNewAccountForm] = useState(false) const [newAccPublicKey, setNewAccPublicKey] = useState(null) const mangoAccounts = useMangoStore((s) => s.mangoAccounts) const selectedMangoAccount = useMangoStore( (s) => s.selectedMangoAccount.current ) const mangoGroup = useMangoStore((s) => s.selectedMangoGroup.current) const setMangoStore = useMangoStore((s) => s.set) const actions = useMangoStore((s) => s.actions) const [, setLastAccountViewed] = useLocalStorageState(LAST_ACCOUNT_KEY) const wallet = useMangoStore.getState().wallet.current const handleMangoAccountChange = (mangoAccount: MangoAccount) => { setLastAccountViewed(mangoAccount.publicKey.toString()) setMangoStore((state) => { state.selectedMangoAccount.current = mangoAccount }) actions.fetchTradeHistory() onClose() } useEffect(() => { if (newAccPublicKey) { setMangoStore((state) => { state.selectedMangoAccount.current = mangoAccounts.find( (ma) => ma.publicKey.toString() === newAccPublicKey ) }) } }, [mangoAccounts, newAccPublicKey]) const handleNewAccountCreation = (newAccPublicKey) => { if (newAccPublicKey) { setNewAccPublicKey(newAccPublicKey) } setShowNewAccountForm(false) } const handleShowNewAccountForm = () => { setNewAccPublicKey(null) setShowNewAccountForm(true) } return ( {mangoAccounts.length > 0 ? ( !showNewAccountForm ? ( <> {t('mango-accounts')}
{mangoAccounts.length > 1 ? t('select-account') : t('your-account')}
handleMangoAccountChange(acc)} > {t('select-account')}
{mangoAccounts.map((account) => ( `${ checked ? 'bg-th-bkg-3 ring-1 ring-inset ring-th-green' : 'bg-th-bkg-1' } default-transition relative flex w-full cursor-pointer rounded-md px-3 py-3 hover:bg-th-bkg-3 focus:outline-none` } > {({ checked }) => ( <>
{account?.name || abbreviateAddress(account.publicKey)} {!account?.owner.equals( wallet?.publicKey ) ? ( ) : ( '' )}
{mangoGroup ? (
) : null}
{checked && (
)}
)}
))}
) : ( <> setShowNewAccountForm(false)} > {t('cancel')} ) ) : ( )}
) } const AccountInfo = ({ mangoGroup, mangoAccount, }: { mangoGroup: MangoGroup mangoAccount: MangoAccount }) => { const mangoCache = useMangoStore((s) => s.selectedMangoGroup.cache) const accountEquity = mangoAccount.computeValue(mangoGroup, mangoCache) const leverage = mangoAccount.getLeverage(mangoGroup, mangoCache).toFixed(2) return (
{formatUsdValue(accountEquity.toNumber())} | 4 ? 'text-th-red' : parseFloat(leverage) > 2 ? 'text-th-orange' : 'text-th-green' } > {leverage}x
) } export default React.memo(AccountsModal)