mango-v4-ui/components/MangoAccountsList.tsx

128 lines
4.7 KiB
TypeScript
Raw Normal View History

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'
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'
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-08-16 05:48:27 -07:00
const mangoAccounts = mangoStore((s) => s.mangoAccounts.accounts)
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)
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)
}
}
2022-08-05 15:11:07 -07:00
return (
2022-09-11 17:22:37 -07:00
<div id="step-one">
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={`${
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"
>
<Popover.Panel className="absolute top-[13.5px] -right-5 z-10 mr-4 w-56 rounded-md rounded-t-none border border-th-bkg-3 bg-th-bkg-1 p-4">
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)}
className="mb-3 flex w-full items-center justify-between border-b border-th-bkg-3 pb-3"
>
{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-09-04 23:32:10 -07:00
<span className="ml-2">New Sub-account</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