mango-v4-ui/components/account/AccountActions.tsx

99 lines
3.3 KiB
TypeScript
Raw Normal View History

2022-06-21 03:58:57 -07:00
import { useWallet } from '@solana/wallet-adapter-react'
2022-07-15 04:09:23 -07:00
import { Fragment, useState } from 'react'
import Button, { LinkButton } from '../shared/Button'
2022-07-14 22:20:20 -07:00
import DepositModal from '../modals/DepositModal'
import WithdrawModal from '../modals/WithdrawModal'
import mangoStore from '../../store/state'
2022-07-15 04:09:23 -07:00
import { Popover, Transition } from '@headlessui/react'
import { DotsHorizontalIcon, TrashIcon, XIcon } from '@heroicons/react/solid'
import { useTranslation } from 'next-i18next'
2022-06-21 03:58:57 -07:00
const AccountActions = () => {
2022-07-15 04:09:23 -07:00
const { t } = useTranslation(['common', 'close-account'])
2022-06-21 03:58:57 -07:00
const { connected } = useWallet()
const [showDepositModal, setShowDepositModal] = useState(false)
const [showWithdrawModal, setShowWithdrawModal] = useState(false)
2022-07-05 20:37:49 -07:00
const handleCloseMangoAccount = async () => {
const client = mangoStore.getState().client
const mangoAccount = mangoStore.getState().mangoAccount
2022-07-06 06:46:16 -07:00
const group = mangoStore.getState().group
if (!mangoAccount || !group) return
2022-07-05 20:37:49 -07:00
try {
2022-07-06 06:46:16 -07:00
const tx = await client.closeMangoAccount(group, mangoAccount)
2022-07-05 20:37:49 -07:00
console.log('success:', tx)
} catch (e) {
console.log(e)
}
}
2022-06-21 03:58:57 -07:00
return (
2022-07-12 19:02:36 -07:00
<>
2022-07-15 04:09:23 -07:00
<div className="flex space-x-3">
<Button disabled={!connected} onClick={() => setShowDepositModal(true)}>
{t('deposit')}
2022-07-12 20:58:13 -07:00
</Button>
<Button
disabled={!connected}
onClick={() => setShowWithdrawModal(true)}
2022-07-15 04:09:23 -07:00
secondary
2022-07-12 20:58:13 -07:00
>
2022-07-15 04:09:23 -07:00
{t('withdraw')}
2022-07-12 20:58:13 -07:00
</Button>
2022-07-15 04:09:23 -07:00
<Popover>
{({ open }) => (
<div className="relative">
<Popover.Button className="flex h-12 w-12 items-center justify-center rounded-full border border-th-fgd-4 hover:text-th-primary">
{open ? (
<XIcon className="h-5 w-5" />
) : (
<DotsHorizontalIcon className="h-5 w-5" />
)}
</Popover.Button>
<Transition
appear={true}
show={open}
as={Fragment}
enter="transition-all ease-in duration-300"
enterFrom="opacity-0 transform scale-90"
enterTo="opacity-100 transform scale-100"
leave="transition ease-out duration-300"
leaveFrom="opacity-100"
leaveTo="opacity-0"
>
<Popover.Panel
className={`absolute right-0 top-14 z-20 space-y-2 rounded-md border border-th-bkg-3 bg-th-bkg-2 p-4`}
>
<LinkButton
className="flex items-center whitespace-nowrap"
disabled={!connected}
onClick={handleCloseMangoAccount}
>
<TrashIcon className="mr-2 h-5 w-5" />
{t('close-account:close-account')}
</LinkButton>
</Popover.Panel>
</Transition>
</div>
)}
</Popover>
2022-06-21 03:58:57 -07:00
</div>
{showDepositModal ? (
<DepositModal
isOpen={showDepositModal}
onClose={() => setShowDepositModal(false)}
/>
) : null}
{showWithdrawModal ? (
<WithdrawModal
isOpen={showWithdrawModal}
onClose={() => setShowWithdrawModal(false)}
/>
) : null}
2022-07-12 19:02:36 -07:00
</>
2022-06-21 03:58:57 -07:00
)
}
export default AccountActions