mango-ui-v3/components/DepositWithdraw.tsx

101 lines
3.7 KiB
TypeScript
Raw Normal View History

2021-06-17 11:03:47 -07:00
import { useCallback, useState } from 'react'
import Link from 'next/link'
import { Menu } from '@headlessui/react'
import { DotsHorizontalIcon } from '@heroicons/react/outline'
2021-04-09 07:27:49 -07:00
import FloatingElement from './FloatingElement'
2021-04-10 12:21:02 -07:00
import { ElementTitle } from './styles'
2021-04-09 07:27:49 -07:00
import useMangoStore from '../stores/useMangoStore'
2021-06-17 11:03:47 -07:00
import DepositModal from './DepositModal'
import WithdrawModal from './WithdrawModal'
2021-07-29 06:19:32 -07:00
import Button, { IconButton } from './Button'
import AccountsModal from './AccountsModal'
2021-04-09 07:27:49 -07:00
2021-04-14 23:16:36 -07:00
export default function MarginBalances() {
2021-07-29 12:05:16 -07:00
const mangoAccount = useMangoStore((s) => s.selectedMangoAccount.current)
2021-06-23 08:32:33 -07:00
const loadingMangoAccount = useMangoStore(
(s) => s.selectedMangoAccount.initialLoad
2021-06-17 11:03:47 -07:00
)
2021-04-10 12:21:02 -07:00
const connected = useMangoStore((s) => s.wallet.connected)
2021-06-17 11:03:47 -07:00
const [showDepositModal, setShowDepositModal] = useState(false)
const [showWithdrawModal, setShowWithdrawModal] = useState(false)
const [showAccountsModal, setShowAccountsModal] = useState(false)
2021-04-25 22:20:05 -07:00
2021-04-09 07:27:49 -07:00
return (
<>
<FloatingElement>
<div className="flex justify-center">
2021-07-29 12:05:16 -07:00
<ElementTitle noMarignBottom>
2021-07-30 13:37:00 -07:00
{mangoAccount?.name || 'Mango Account'}
2021-07-29 12:05:16 -07:00
</ElementTitle>
<div className="absolute right-0 pr-4">
<Menu>
2021-07-29 06:19:32 -07:00
<Menu.Button disabled={!connected}>
2021-07-30 03:30:58 -07:00
<div className="bg-th-bkg-4 flex items-center justify-center rounded-full w-8 h-8 text-th-fgd-1 focus:outline-none hover:text-th-primary">
2021-07-29 06:19:32 -07:00
<DotsHorizontalIcon className="w-5 h-5" />
2021-07-30 03:30:58 -07:00
</div>
</Menu.Button>
<Menu.Items className="bg-th-bkg-1 mt-2 p-1 absolute right-0 shadow-lg outline-none rounded-md w-48 z-20">
<Menu.Item>
<button
className="flex flex-row font-normal items-center rounded-none w-full p-2 hover:bg-th-bkg-2 hover:cursor-pointer focus:outline-none"
onClick={() => setShowAccountsModal(true)}
>
<div className="pl-2 text-left">Change Account</div>
</button>
</Menu.Item>
</Menu.Items>
</Menu>
</div>
</div>
<div className="flex justify-center mt-2">
2021-07-29 12:05:16 -07:00
{mangoAccount ? (
<Link href={'/account'}>
<a className="pt-1 text-th-fgd-3 text-xs underline hover:no-underline">
2021-07-29 12:05:16 -07:00
{mangoAccount?.publicKey.toString()}
</a>
</Link>
) : connected ? (
<div className="pt-1 text-th-fgd-3">
Deposit funds to get started
2021-04-16 11:16:31 -07:00
</div>
) : null}
2021-04-25 22:20:05 -07:00
</div>
<div className="flex justify-center items-center mt-2">
2021-06-17 11:03:47 -07:00
<Button
2021-06-05 09:14:34 -07:00
onClick={() => setShowDepositModal(true)}
className="w-1/2"
2021-06-23 08:32:33 -07:00
disabled={!connected || loadingMangoAccount}
2021-06-05 09:14:34 -07:00
>
<span>Deposit</span>
</Button>
<Button
onClick={() => setShowWithdrawModal(true)}
className="ml-4 w-1/2"
2021-07-29 12:05:16 -07:00
disabled={!connected || !mangoAccount || loadingMangoAccount}
2021-06-05 09:14:34 -07:00
>
<span>Withdraw</span>
2021-06-17 11:03:47 -07:00
</Button>
2021-06-05 09:14:34 -07:00
</div>
2021-04-09 07:27:49 -07:00
</FloatingElement>
2021-06-17 11:03:47 -07:00
{showDepositModal && (
2021-07-29 12:05:16 -07:00
<DepositModal
isOpen={showDepositModal}
onClose={() => setShowDepositModal(false)}
/>
2021-04-09 07:27:49 -07:00
)}
{showWithdrawModal && (
2021-04-10 14:12:15 -07:00
<WithdrawModal
2021-04-09 07:27:49 -07:00
isOpen={showWithdrawModal}
2021-07-29 12:05:16 -07:00
onClose={() => setShowWithdrawModal(false)}
2021-04-09 07:27:49 -07:00
/>
)}
{showAccountsModal ? (
<AccountsModal
2021-07-29 12:05:16 -07:00
onClose={() => setShowAccountsModal(false)}
isOpen={showAccountsModal}
/>
) : null}
2021-04-16 11:16:31 -07:00
</>
)
}