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

167 lines
5.3 KiB
TypeScript
Raw Normal View History

2022-11-16 04:14:53 -08:00
import { useMemo, useState } from 'react'
2022-07-15 04:09:23 -07:00
import Button, { LinkButton } from '../shared/Button'
2022-07-14 22:20:20 -07:00
import DepositModal from '../modals/DepositModal'
import WithdrawModal from '../modals/WithdrawModal'
2022-07-28 05:13:42 -07:00
import {
2022-09-07 17:47:59 -07:00
ArrowDownTrayIcon,
ArrowUpTrayIcon,
2022-11-16 04:14:53 -08:00
BanknotesIcon,
2022-11-02 08:50:03 -07:00
DocumentDuplicateIcon,
2022-09-06 21:36:35 -07:00
EllipsisHorizontalIcon,
2022-07-28 05:13:42 -07:00
PencilIcon,
TrashIcon,
2022-11-16 08:59:34 -08:00
UsersIcon,
2022-09-06 21:36:35 -07:00
} from '@heroicons/react/20/solid'
2022-07-15 04:09:23 -07:00
import { useTranslation } from 'next-i18next'
2022-07-24 04:48:55 -07:00
import IconDropMenu from '../shared/IconDropMenu'
2022-07-28 03:51:36 -07:00
import CloseAccountModal from '../modals/CloseAccountModal'
2022-07-28 05:13:42 -07:00
import AccountNameModal from '../modals/AccountNameModal'
import mangoStore from '@store/mangoStore'
2022-11-02 08:50:03 -07:00
import { copyToClipboard } from 'utils'
import { notify } from 'utils/notifications'
2022-11-09 18:35:12 -08:00
import { abbreviateAddress } from 'utils/formatting'
2022-11-16 04:14:53 -08:00
import { HealthType, ZERO_I80F48 } from '@blockworks-foundation/mango-v4'
import RepayModal from '@components/modals/RepayModal'
2022-11-16 08:59:34 -08:00
import DelegateModal from '@components/modals/DelegateModal'
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-11-16 04:14:53 -08:00
const group = mangoStore((s) => s.group)
const mangoAccount = mangoStore((s) => s.mangoAccount.current)
2022-07-28 03:51:36 -07:00
const [showCloseAccountModal, setShowCloseAccountModal] = useState(false)
2022-06-21 03:58:57 -07:00
const [showDepositModal, setShowDepositModal] = useState(false)
2022-07-28 05:13:42 -07:00
const [showEditAccountModal, setShowEditAccountModal] = useState(false)
2022-06-21 03:58:57 -07:00
const [showWithdrawModal, setShowWithdrawModal] = useState(false)
2022-11-16 04:14:53 -08:00
const [showRepayModal, setShowRepayModal] = useState(false)
2022-11-16 08:59:34 -08:00
const [showDelegateModal, setShowDelegateModal] = useState(false)
2022-06-21 03:58:57 -07:00
2022-11-02 08:50:03 -07:00
const handleCopyAddress = (address: string) => {
copyToClipboard(address)
notify({
2022-11-09 18:35:12 -08:00
title: t('copy-address-success', {
pk: abbreviateAddress(mangoAccount!.publicKey),
}),
2022-11-02 08:50:03 -07:00
type: 'success',
})
}
2022-11-16 04:14:53 -08:00
const hasBorrows = useMemo(() => {
return mangoAccount && group
? !mangoAccount.getLiabsValue(group, HealthType.init).eq(ZERO_I80F48())
: false
}, [mangoAccount, group])
2022-06-21 03:58:57 -07:00
return (
2022-07-12 19:02:36 -07:00
<>
2022-11-02 08:50:03 -07:00
<div className="flex items-center space-x-2 md:space-x-3">
2022-11-16 04:14:53 -08:00
{hasBorrows ? (
<Button
className="flex items-center"
disabled={!mangoAccount}
onClick={() => setShowRepayModal(true)}
>
<BanknotesIcon className="mr-2 h-5 w-5" />
{t('repay')}
</Button>
) : null}
2022-07-20 21:50:56 -07:00
<Button
2022-09-07 17:47:59 -07:00
className="flex items-center"
disabled={!mangoAccount}
2022-07-20 21:50:56 -07:00
onClick={() => setShowDepositModal(true)}
2022-11-16 04:14:53 -08:00
secondary={hasBorrows}
2022-07-20 21:50:56 -07:00
>
2022-09-07 17:47:59 -07:00
<ArrowDownTrayIcon className="mr-2 h-5 w-5" />
2022-07-15 04:09:23 -07:00
{t('deposit')}
2022-07-12 20:58:13 -07:00
</Button>
<Button
2022-09-07 17:47:59 -07:00
className="flex items-center"
disabled={!mangoAccount}
2022-07-12 20:58:13 -07:00
onClick={() => setShowWithdrawModal(true)}
2022-07-15 04:09:23 -07:00
secondary
2022-07-12 20:58:13 -07:00
>
2022-09-07 17:47:59 -07:00
<ArrowUpTrayIcon className="mr-2 h-5 w-5" />
2022-07-15 04:09:23 -07:00
{t('withdraw')}
2022-07-12 20:58:13 -07:00
</Button>
2022-09-06 21:36:35 -07:00
<IconDropMenu
icon={<EllipsisHorizontalIcon className="h-5 w-5" />}
2022-11-16 04:14:53 -08:00
size="medium"
2022-09-06 21:36:35 -07:00
>
2022-11-02 08:50:03 -07:00
<LinkButton
className="whitespace-nowrap"
disabled={!mangoAccount}
onClick={() =>
handleCopyAddress(mangoAccount!.publicKey.toString())
}
>
<DocumentDuplicateIcon className="h-4 w-4" />
<span className="ml-2">{t('copy-address')}</span>
</LinkButton>
2022-07-28 05:13:42 -07:00
<LinkButton
2022-08-02 12:20:27 -07:00
className="whitespace-nowrap"
disabled={!mangoAccount}
2022-07-28 05:13:42 -07:00
onClick={() => setShowEditAccountModal(true)}
>
2022-11-02 08:50:03 -07:00
<PencilIcon className="h-4 w-4" />
2022-08-02 12:20:27 -07:00
<span className="ml-2">{t('edit-account')}</span>
2022-07-28 05:13:42 -07:00
</LinkButton>
2022-11-16 08:59:34 -08:00
<LinkButton
className="whitespace-nowrap"
disabled={!mangoAccount}
onClick={() => setShowDelegateModal(true)}
>
<UsersIcon className="h-4 w-4" />
<span className="ml-2">{t('delegate-account')}</span>
</LinkButton>
2022-07-24 04:48:55 -07:00
<LinkButton
2022-08-02 12:20:27 -07:00
className="whitespace-nowrap"
disabled={!mangoAccount}
2022-07-28 03:51:36 -07:00
onClick={() => setShowCloseAccountModal(true)}
2022-07-24 04:48:55 -07:00
>
2022-11-02 08:50:03 -07:00
<TrashIcon className="h-4 w-4" />
2022-08-02 12:20:27 -07:00
<span className="ml-2">{t('close-account')}</span>
2022-07-24 04:48:55 -07:00
</LinkButton>
</IconDropMenu>
2022-06-21 03:58:57 -07:00
</div>
2022-07-28 03:51:36 -07:00
{showCloseAccountModal ? (
<CloseAccountModal
isOpen={showCloseAccountModal}
onClose={() => setShowCloseAccountModal(false)}
/>
) : null}
2022-06-21 03:58:57 -07:00
{showDepositModal ? (
<DepositModal
isOpen={showDepositModal}
onClose={() => setShowDepositModal(false)}
/>
) : null}
2022-07-28 05:13:42 -07:00
{showEditAccountModal ? (
<AccountNameModal
isOpen={showEditAccountModal}
onClose={() => setShowEditAccountModal(false)}
/>
) : null}
2022-06-21 03:58:57 -07:00
{showWithdrawModal ? (
<WithdrawModal
isOpen={showWithdrawModal}
onClose={() => setShowWithdrawModal(false)}
/>
) : null}
2022-11-17 13:47:27 -08:00
2022-11-16 04:14:53 -08:00
{showRepayModal ? (
<RepayModal
isOpen={showRepayModal}
onClose={() => setShowRepayModal(false)}
2022-11-17 14:08:45 -08:00
/>
) : null}
2022-11-16 08:59:34 -08:00
{showDelegateModal ? (
<DelegateModal
isOpen={showDelegateModal}
onClose={() => setShowDelegateModal(false)}
2022-11-16 04:14:53 -08:00
/>
) : null}
2022-07-12 19:02:36 -07:00
</>
2022-06-21 03:58:57 -07:00
)
}
export default AccountActions