import { Fragment, useState } from 'react' import { CheckCircleIcon, ChevronDownIcon, PlusCircleIcon, } from '@heroicons/react/20/solid' import { Popover, Transition } from '@headlessui/react' import { MangoAccount } from '@blockworks-foundation/mango-v4' import mangoStore from '@store/mangoStore' import { LinkButton } from './shared/Button' import CreateAccountModal from './modals/CreateAccountModal' 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' const MangoAccountsList = ({ mangoAccount, }: { mangoAccount: MangoAccount | undefined }) => { const { t } = useTranslation('common') const mangoAccounts = mangoStore((s) => s.mangoAccounts.accounts) const loading = mangoStore((s) => s.mangoAccount.initialLoad) const [showNewAccountModal, setShowNewAccountModal] = useState(false) const [, setLastAccountViewed] = useLocalStorageStringState(LAST_ACCOUNT_KEY) const handleSelectMangoAccount = async (acc: MangoAccount) => { const set = mangoStore.getState().set const client = mangoStore.getState().client const group = mangoStore.getState().group if (!group) return try { const reloadedMangoAccount = await retryFn(() => acc.reload(client, group) ) set((s) => { s.mangoAccount.current = reloadedMangoAccount s.mangoAccount.lastUpdatedAt = new Date().toISOString() }) setLastAccountViewed(acc.publicKey.toString()) } catch (e) { console.warn('Error selecting account', e) } } return (
{({ open }) => ( <>

{t('accounts')}

{mangoAccount ? mangoAccount.name : 'No Accounts'}

{loading ? ( ) : mangoAccounts.length ? ( mangoAccounts.map((acc) => (
)) ) : (

Create your first account 😎

)}
setShowNewAccountModal(true)} > New Sub-account
)}
{showNewAccountModal ? ( setShowNewAccountModal(false)} /> ) : null}
) } export default MangoAccountsList