import React, { FunctionComponent, useEffect, useState } from 'react' import { RadioGroup } from '@headlessui/react' import { CheckCircleIcon } from '@heroicons/react/solid' import { ChevronLeftIcon, CurrencyDollarIcon, PlusCircleIcon, } from '@heroicons/react/outline' import useMangoStore from '../stores/useMangoStore' import { MarginAccount } from '@blockworks-foundation/mango-client' import { abbreviateAddress } from '../utils' import useLocalStorageState from '../hooks/useLocalStorageState' import Modal from './Modal' import { ElementTitle } from './styles' import Button, { LinkButton } from './Button' import NewAccount from './NewAccount' interface AccountsModalProps { onClose: () => void isOpen: boolean } const AccountsModal: FunctionComponent = ({ isOpen, onClose, }) => { const [showNewAccountForm, setShowNewAccountForm] = useState(false) const [newAccPublicKey, setNewAccPublicKey] = useState(null) const marginAccounts = useMangoStore((s) => s.marginAccounts) const selectedMarginAccount = useMangoStore( (s) => s.selectedMarginAccount.current ) const selectedMangoGroup = useMangoStore((s) => s.selectedMangoGroup.current) const prices = useMangoStore((s) => s.selectedMangoGroup.prices) const setMangoStore = useMangoStore((s) => s.set) const actions = useMangoStore((s) => s.actions) const [, setLastAccountViewed] = useLocalStorageState('lastAccountViewed') const handleMarginAccountChange = (marginAccount: MarginAccount) => { setLastAccountViewed(marginAccount.publicKey.toString()) setMangoStore((state) => { state.selectedMarginAccount.current = marginAccount }) actions.fetchTradeHistory() onClose() } useEffect(() => { if (newAccPublicKey) { setMangoStore((state) => { state.selectedMarginAccount.current = marginAccounts.find((ma) => ma.publicKey.equals(newAccPublicKey) ) }) } }, [marginAccounts, newAccPublicKey]) const handleNewAccountCreation = (newAccPublicKey) => { if (newAccPublicKey) { setNewAccPublicKey(newAccPublicKey) } setShowNewAccountForm(false) } const handleShowNewAccountForm = () => { setNewAccPublicKey(null) setShowNewAccountForm(true) } const getAccountInfo = (acc) => { const accountEquity = acc .computeValue(selectedMangoGroup, prices) .toFixed(2) const collRatio = acc.getCollateralRatio(selectedMangoGroup, prices) const leverage = accountEquity && collRatio ? (1 / (collRatio - 1)).toFixed(2) : '0.00' return (
${accountEquity} | 4 ? 'text-th-red' : parseFloat(leverage) > 2 ? 'text-th-orange' : 'text-th-green' } > {leverage}x
) } return ( {marginAccounts.length > 0 ? ( !showNewAccountForm ? ( <> Margin Accounts
{marginAccounts.length > 1 ? 'Select an account' : 'Your Account'}
handleMarginAccountChange(acc)} > Select a Margin Account
{marginAccounts.map((account) => ( `${ checked ? 'bg-th-bkg-3 ring-1 ring-th-green ring-inset' : 'bg-th-bkg-1' } relative rounded-md w-full px-3 py-3 cursor-pointer default-transition flex hover:bg-th-bkg-3 focus:outline-none` } > {({ checked }) => ( <>
{abbreviateAddress(account.publicKey)}
{prices && selectedMangoGroup ? (
{getAccountInfo(account)}
) : null}
{checked && (
)}
)}
))}
) : ( <> setShowNewAccountForm(false)} > Back ) ) : ( )}
) } export default React.memo(AccountsModal)