2022-08-05 15:11:07 -07:00
|
|
|
import { Fragment, useState } from 'react'
|
|
|
|
import {
|
|
|
|
CheckCircleIcon,
|
|
|
|
ChevronDownIcon,
|
|
|
|
PlusCircleIcon,
|
2022-09-06 21:36:35 -07:00
|
|
|
} from '@heroicons/react/20/solid'
|
2022-08-05 15:11:07 -07:00
|
|
|
import { Popover, Transition } from '@headlessui/react'
|
|
|
|
import { MangoAccount } from '@blockworks-foundation/mango-v4'
|
2022-09-12 08:53:57 -07:00
|
|
|
import mangoStore from '@store/mangoStore'
|
2022-08-05 15:11:07 -07:00
|
|
|
import { LinkButton } from './shared/Button'
|
2022-08-11 21:20:17 -07:00
|
|
|
import CreateAccountModal from './modals/CreateAccountModal'
|
2022-08-19 14:17:30 -07:00
|
|
|
import { useLocalStorageStringState } from '../hooks/useLocalStorageState'
|
|
|
|
import { LAST_ACCOUNT_KEY } from '../utils/constants'
|
2022-08-24 04:30:10 -07:00
|
|
|
import { useTranslation } from 'next-i18next'
|
2022-08-31 07:54:37 -07:00
|
|
|
import { retryFn } from '../utils'
|
2022-09-11 17:22:37 -07:00
|
|
|
import Loading from './shared/Loading'
|
2022-08-05 15:11:07 -07:00
|
|
|
|
|
|
|
const MangoAccountsList = ({
|
|
|
|
mangoAccount,
|
|
|
|
}: {
|
2022-09-11 17:22:37 -07:00
|
|
|
mangoAccount: MangoAccount | undefined
|
2022-08-05 15:11:07 -07:00
|
|
|
}) => {
|
2022-08-24 04:30:10 -07:00
|
|
|
const { t } = useTranslation('common')
|
2022-10-07 04:47:15 -07:00
|
|
|
const mangoAccounts = mangoStore((s) => s.mangoAccounts)
|
2022-10-02 21:38:49 -07:00
|
|
|
const actions = mangoStore((s) => s.actions)
|
2022-09-11 17:22:37 -07:00
|
|
|
const loading = mangoStore((s) => s.mangoAccount.initialLoad)
|
2022-08-05 15:11:07 -07:00
|
|
|
const [showNewAccountModal, setShowNewAccountModal] = useState(false)
|
2022-08-19 14:17:30 -07:00
|
|
|
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
|
2022-09-29 20:51:22 -07:00
|
|
|
set((s) => {
|
|
|
|
s.activityFeed.feed = []
|
|
|
|
s.activityFeed.loading = true
|
|
|
|
})
|
2022-08-27 00:56:59 -07:00
|
|
|
try {
|
2022-10-07 05:22:18 -07:00
|
|
|
const reloadedMangoAccount = await retryFn(() => acc.reload(client))
|
2022-08-27 00:56:59 -07:00
|
|
|
set((s) => {
|
2022-09-02 16:51:35 -07:00
|
|
|
s.mangoAccount.current = reloadedMangoAccount
|
2022-08-27 00:56:59 -07:00
|
|
|
})
|
|
|
|
setLastAccountViewed(acc.publicKey.toString())
|
2022-10-31 11:26:17 -07:00
|
|
|
actions.fetchOpenOrders(acc)
|
2022-08-27 00:56:59 -07:00
|
|
|
} catch (e) {
|
|
|
|
console.warn('Error selecting account', e)
|
|
|
|
}
|
2022-08-19 14:17:30 -07:00
|
|
|
}
|
2022-08-05 15:11:07 -07:00
|
|
|
|
|
|
|
return (
|
2022-09-21 21:25:24 -07:00
|
|
|
<div id="account-step-two">
|
2022-08-05 15:11:07 -07:00
|
|
|
<Popover>
|
|
|
|
{({ open }) => (
|
|
|
|
<>
|
2022-08-24 04:30:10 -07:00
|
|
|
<Popover.Button className="flex w-full items-center rounded-none text-th-fgd-1 hover:text-th-primary">
|
|
|
|
<div className="mr-2">
|
|
|
|
<p className="text-right text-xs">{t('accounts')}</p>
|
|
|
|
<p className="text-left text-sm font-bold text-th-fgd-1">
|
2022-09-11 17:22:37 -07:00
|
|
|
{mangoAccount ? mangoAccount.name : 'No Accounts'}
|
2022-08-24 04:30:10 -07:00
|
|
|
</p>
|
|
|
|
</div>
|
2022-08-05 15:11:07 -07:00
|
|
|
<ChevronDownIcon
|
|
|
|
className={`${
|
2022-08-16 15:58:03 -07:00
|
|
|
open ? 'rotate-180' : 'rotate-360'
|
2022-08-05 15:11:07 -07:00
|
|
|
} mt-0.5 h-6 w-6 flex-shrink-0 text-th-fgd-3`}
|
|
|
|
/>
|
|
|
|
</Popover.Button>
|
2022-08-24 04:30:10 -07:00
|
|
|
<div className="relative">
|
|
|
|
<Transition
|
|
|
|
appear={true}
|
|
|
|
show={open}
|
|
|
|
as={Fragment}
|
|
|
|
enter="transition-all ease-in duration-200"
|
|
|
|
enterFrom="opacity-0 scale-75"
|
|
|
|
enterTo="opacity-100 scale-100"
|
|
|
|
leave="transition ease-out duration-200"
|
|
|
|
leaveFrom="opacity-100"
|
|
|
|
leaveTo="opacity-0"
|
|
|
|
>
|
2022-10-05 17:04:16 -07:00
|
|
|
<Popover.Panel className="absolute top-[13.5px] -right-7 z-20 mr-4 w-56 rounded-md rounded-t-none border border-th-bkg-2 bg-th-bkg-2 p-4 text-th-fgd-3">
|
2022-09-11 17:22:37 -07:00
|
|
|
{loading ? (
|
|
|
|
<Loading />
|
|
|
|
) : mangoAccounts.length ? (
|
2022-08-24 04:30:10 -07:00
|
|
|
mangoAccounts.map((acc) => (
|
|
|
|
<div key={acc.publicKey.toString()}>
|
|
|
|
<button
|
|
|
|
onClick={() => handleSelectMangoAccount(acc)}
|
2022-10-02 22:42:28 -07:00
|
|
|
className="default-transition mb-3 flex w-full items-center justify-between border-b border-th-bkg-4 pb-3 hover:text-th-fgd-1"
|
2022-08-24 04:30:10 -07:00
|
|
|
>
|
|
|
|
{acc.name}
|
|
|
|
{acc.publicKey.toString() ===
|
2022-09-22 03:23:34 -07:00
|
|
|
mangoAccount?.publicKey.toString() ? (
|
2022-08-24 04:30:10 -07:00
|
|
|
<CheckCircleIcon className="h-5 w-5 text-th-green" />
|
|
|
|
) : null}
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
))
|
|
|
|
) : (
|
2022-09-11 17:22:37 -07:00
|
|
|
<p className="mb-4 text-center text-sm">
|
|
|
|
Create your first account 😎
|
|
|
|
</p>
|
2022-08-24 04:30:10 -07:00
|
|
|
)}
|
|
|
|
<div>
|
|
|
|
<LinkButton
|
|
|
|
className="w-full justify-center"
|
|
|
|
onClick={() => setShowNewAccountModal(true)}
|
|
|
|
>
|
|
|
|
<PlusCircleIcon className="h-5 w-5" />
|
2022-10-29 18:46:56 -07:00
|
|
|
<span className="ml-2">New subaccount</span>
|
2022-08-24 04:30:10 -07:00
|
|
|
</LinkButton>
|
|
|
|
</div>
|
|
|
|
</Popover.Panel>
|
|
|
|
</Transition>
|
|
|
|
</div>
|
2022-08-05 15:11:07 -07:00
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</Popover>
|
|
|
|
{showNewAccountModal ? (
|
2022-08-11 21:20:17 -07:00
|
|
|
<CreateAccountModal
|
2022-08-05 15:11:07 -07:00
|
|
|
isOpen={showNewAccountModal}
|
|
|
|
onClose={() => setShowNewAccountModal(false)}
|
|
|
|
/>
|
|
|
|
) : null}
|
2022-09-11 17:22:37 -07:00
|
|
|
</div>
|
2022-08-05 15:11:07 -07:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default MangoAccountsList
|