import { useMemo, useState } from 'react' import { Listbox } from '@headlessui/react' import { ChevronDownIcon, ChevronUpIcon } from '@heroicons/react/solid' import { abbreviateAddress } from '../utils' import useMangoStore, { WalletToken } from '../stores/useMangoStore' import { RefreshClockwiseIcon } from './icons' import useMangoGroupConfig from '../hooks/useMangoGroupConfig' import { useTranslation } from 'next-i18next' type AccountSelectProps = { accounts: WalletToken[] selectedAccount: WalletToken onSelectAccount: (WalletToken) => any hideAddress?: boolean } const AccountSelect = ({ accounts, selectedAccount, onSelectAccount, hideAddress = false, }: AccountSelectProps) => { const { t } = useTranslation('common') const groupConfig = useMangoGroupConfig() const tokenSymbols = useMemo( () => groupConfig.tokens.map((t) => t.symbol), [groupConfig] ) const missingTokenSymbols = useMemo(() => { const symbolsForAccounts = accounts.map((a) => a.config.symbol) return tokenSymbols.filter((sym) => !symbolsForAccounts.includes(sym)) }, [accounts, tokenSymbols]) const actions = useMangoStore((s) => s.actions) const [loading, setLoading] = useState(false) const handleChange = (value: string) => { const newAccount = accounts.find( (a) => a.account.publicKey.toBase58() === value ) onSelectAccount(newAccount) } const handleRefreshBalances = async () => { setLoading(true) await actions.fetchWalletTokens() setLoading(false) } return (
{t('asset')}
{missingTokenSymbols.length > 0 ? ( ) : null}
{({ open }) => ( <>
{selectedAccount ? (
{selectedAccount.config.symbol} {!hideAddress ? (
{abbreviateAddress( selectedAccount.account.publicKey )}
) : null}
{selectedAccount.uiBalance}
) : ( t('select-asset') )} {open ? ( ) : ( )}
{accounts.map((account) => { const symbolForAccount = account.config.symbol return ( {({ disabled, selected }) => (
{symbolForAccount} {!hideAddress ? (
{abbreviateAddress(account.account.publicKey)}
) : null}
{!hideAddress ? (
{account.uiBalance} {symbolForAccount}
) : null}
)}
) })} {missingTokenSymbols.map((token) => (
{token}
{t('no-wallet')}
))}
)}
) } export default AccountSelect