import classNames from 'classnames' import { useEffect, useState } from 'react' import { useNavigate, useParams } from 'react-router-dom' import AccountList from 'components/Account/AccountList' import CreateAccount from 'components/Account/CreateAccount' import FundAccount from 'components/Account/FundAccount' import Button from 'components/Button' import { CircularProgress } from 'components/CircularProgress' import { Account, Plus, PlusCircled } from 'components/Icons' import Overlay from 'components/Overlay' import Text from 'components/Text' import useToggle from 'hooks/useToggle' import useStore from 'store' import { hardcodedFee } from 'utils/contants' import { isNumber } from 'utils/parsers' const menuClasses = 'absolute isolate flex w-full flex-wrap scrollbar-hide' interface Props { accounts: Account[] } export default function AccountMenuContent(props: Props) { const navigate = useNavigate() const { accountId, address } = useParams() const createAccount = useStore((s) => s.createAccount) const [showMenu, setShowMenu] = useToggle() const [isCreating, setIsCreating] = useToggle() const hasCreditAccounts = !!props.accounts.length const isAccountSelected = isNumber(accountId) const selectedAccountDetails = props.accounts.find((account) => account.id === accountId) const [showFundAccount, setShowFundAccount] = useState( isAccountSelected && !selectedAccountDetails?.deposits?.length, ) const isLoadingAccount = isAccountSelected && selectedAccountDetails?.id !== accountId const showCreateAccount = !hasCreditAccounts || isCreating async function createAccountHandler() { setShowMenu(true) setIsCreating(true) const accountId = await createAccount({ fee: hardcodedFee }) setIsCreating(false) if (!accountId) return navigate(`/wallets/${address}/accounts/${accountId}`) } useEffect(() => { useStore.setState({ accounts: props.accounts }) }, [props.accounts]) if (!address) return null return (
{!showFundAccount && !showCreateAccount ? ( <>
Accounts
{isAccountSelected && isLoadingAccount && (
)} {hasCreditAccounts && !showFundAccount && !isLoadingAccount && ( )}
) : (
{showCreateAccount ? ( ) : showFundAccount ? ( ) : null}
)}
) }