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

202 lines
7.2 KiB
TypeScript
Raw Normal View History

2023-01-05 01:04:09 -08:00
import { Fragment, useState } from 'react'
2023-05-29 22:49:26 -07:00
import Button, { IconButton } from '../shared/Button'
2022-07-28 05:13:42 -07:00
import {
2022-12-18 04:30:49 -08:00
ArrowDownRightIcon,
ArrowUpLeftIcon,
2022-11-02 08:50:03 -07:00
DocumentDuplicateIcon,
PencilIcon,
TrashIcon,
2023-01-19 19:48:14 -08:00
UserPlusIcon,
2022-12-31 01:46:26 -08:00
WrenchIcon,
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'
import CloseAccountModal from '../modals/CloseAccountModal'
import AccountNameModal from '../modals/AccountNameModal'
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-12-31 01:46:26 -08:00
import { MangoAccount } from '@blockworks-foundation/mango-v4'
import DelegateModal from '@components/modals/DelegateModal'
2022-11-18 09:09:39 -08:00
import useMangoAccount from 'hooks/useMangoAccount'
2022-12-18 04:30:49 -08:00
import BorrowRepayModal from '@components/modals/BorrowRepayModal'
import { useWallet } from '@solana/wallet-adapter-react'
import CreateAccountModal from '@components/modals/CreateAccountModal'
2023-01-29 02:05:32 -08:00
import { Popover, Transition } from '@headlessui/react'
2023-01-05 01:04:09 -08:00
import ActionsLinkButton from './ActionsLinkButton'
import useUnownedAccount from 'hooks/useUnownedAccount'
2023-05-29 22:49:26 -07:00
import { useViewport } from 'hooks/useViewport'
import { breakpoints } from 'utils/theme'
2022-06-21 03:58:57 -07:00
export const handleCopyAddress = (
mangoAccount: MangoAccount,
successMessage: string
) => {
copyToClipboard(mangoAccount.publicKey.toString())
notify({
title: successMessage,
type: 'success',
})
}
2022-06-21 03:58:57 -07:00
const AccountActions = () => {
2022-07-15 04:09:23 -07:00
const { t } = useTranslation(['common', 'close-account'])
const { mangoAccount, mangoAccountAddress } = useMangoAccount()
const [showCloseAccountModal, setShowCloseAccountModal] = useState(false)
const [showEditAccountModal, setShowEditAccountModal] = useState(false)
const [showBorrowModal, setShowBorrowModal] = useState(false)
2022-11-16 04:14:53 -08:00
const [showRepayModal, setShowRepayModal] = useState(false)
const [showDelegateModal, setShowDelegateModal] = useState(false)
const [showCreateAccountModal, setShowCreateAccountModal] = useState(false)
const { connected } = useWallet()
const { isUnownedAccount } = useUnownedAccount()
2023-05-29 22:49:26 -07:00
const { width } = useViewport()
const isMobile = width ? width < breakpoints.sm : false
2022-11-16 04:14:53 -08:00
const handleBorrowModal = () => {
if (mangoAccountAddress || !connected) {
setShowBorrowModal(true)
} else {
setShowCreateAccountModal(true)
}
}
2022-06-21 03:58:57 -07:00
return (
2022-07-12 19:02:36 -07:00
<>
{isUnownedAccount ? null : (
<div className="flex items-center space-x-2">
<Button
2023-05-29 22:49:26 -07:00
className="flex w-full items-center justify-center sm:w-1/3 md:w-auto"
disabled={!mangoAccountAddress}
onClick={() => setShowRepayModal(true)}
secondary
>
2023-05-29 22:49:26 -07:00
<ArrowDownRightIcon className="mr-2 h-5 w-5 flex-shrink-0" />
{t('repay')}
</Button>
<Button
2023-05-29 22:49:26 -07:00
className="flex w-full items-center justify-center sm:w-1/3 md:w-auto"
onClick={handleBorrowModal}
secondary
>
2023-05-29 22:49:26 -07:00
<ArrowUpLeftIcon className="mr-2 h-5 w-5 flex-shrink-0" />
{t('borrow')}
</Button>
2023-05-29 22:49:26 -07:00
<Popover className="relative sm:w-1/3 md:w-auto">
{({ open }) => (
2023-02-01 15:15:01 -08:00
<>
2023-01-29 02:05:32 -08:00
<Popover.Button
2023-04-19 18:12:45 -07:00
className={`w-full focus:outline-none`}
2023-01-07 12:52:22 -08:00
as="div"
2022-12-29 16:03:32 -08:00
>
2023-05-29 22:49:26 -07:00
{!isMobile ? (
<Button
className="flex w-full items-center justify-center"
secondary
>
<WrenchIcon className="mr-2 h-4 w-4 flex-shrink-0" />
{t('actions')}
</Button>
) : (
<IconButton size="medium">
<WrenchIcon className="h-5 w-5" />
</IconButton>
)}
2023-01-29 02:05:32 -08:00
</Popover.Button>
<Transition
appear={true}
show={open}
as={Fragment}
enter="transition ease-in duration-75"
enterFrom="opacity-0 nice scale-75"
enterTo="opacity-100 scale-100"
leave="transition ease-out duration-100"
leaveFrom="opacity-100"
leaveTo="opacity-0"
>
<Popover.Panel className="absolute right-0 top-10 mt-1 space-y-2 rounded-md bg-th-bkg-2 px-4 py-2.5">
2023-01-29 02:05:32 -08:00
<ActionsLinkButton
mangoAccount={mangoAccount!}
onClick={() =>
handleCopyAddress(
mangoAccount!,
t('copy-address-success', {
pk: abbreviateAddress(mangoAccount!.publicKey),
})
)
}
>
<DocumentDuplicateIcon className="h-4 w-4" />
<span className="ml-2">{t('copy-address')}</span>
</ActionsLinkButton>
<ActionsLinkButton
mangoAccount={mangoAccount!}
onClick={() => setShowEditAccountModal(true)}
>
<PencilIcon className="h-4 w-4" />
<span className="ml-2">{t('edit-account')}</span>
</ActionsLinkButton>
<ActionsLinkButton
mangoAccount={mangoAccount!}
onClick={() => setShowDelegateModal(true)}
>
<UserPlusIcon className="h-4 w-4" />
<span className="ml-2">{t('delegate-account')}</span>
</ActionsLinkButton>
<ActionsLinkButton
mangoAccount={mangoAccount!}
onClick={() => setShowCloseAccountModal(true)}
>
<TrashIcon className="h-4 w-4" />
<span className="ml-2">{t('close-account')}</span>
</ActionsLinkButton>
</Popover.Panel>
</Transition>
2023-02-01 15:15:01 -08:00
</>
)}
2023-01-29 02:05:32 -08:00
</Popover>
</div>
)}
{showCloseAccountModal ? (
<CloseAccountModal
isOpen={showCloseAccountModal}
onClose={() => setShowCloseAccountModal(false)}
/>
) : null}
{showEditAccountModal ? (
<AccountNameModal
isOpen={showEditAccountModal}
onClose={() => setShowEditAccountModal(false)}
/>
) : null}
{showBorrowModal ? (
2022-12-18 04:30:49 -08:00
<BorrowRepayModal
action="borrow"
isOpen={showBorrowModal}
onClose={() => setShowBorrowModal(false)}
2022-06-21 03:58:57 -07:00
/>
) : null}
2022-11-16 04:14:53 -08:00
{showRepayModal ? (
2022-12-18 04:30:49 -08:00
<BorrowRepayModal
action="repay"
2022-11-16 04:14:53 -08:00
isOpen={showRepayModal}
onClose={() => setShowRepayModal(false)}
2022-11-17 14:08:45 -08:00
/>
) : null}
{showDelegateModal ? (
<DelegateModal
isOpen={showDelegateModal}
onClose={() => setShowDelegateModal(false)}
/>
) : null}
{showCreateAccountModal ? (
<CreateAccountModal
isOpen={showCreateAccountModal}
onClose={() => setShowCreateAccountModal(false)}
/>
) : null}
2022-07-12 19:02:36 -07:00
</>
2022-06-21 03:58:57 -07:00
)
}
export default AccountActions