import { ChevronDownIcon } from '@heroicons/react/solid' import { useTranslation } from 'next-i18next' import Image from 'next/image' import { ChangeEvent, useCallback, useMemo, useState } from 'react' import mangoStore from '../../store/state' import { ModalProps } from '../../types/modal' import { notify } from '../../utils/notifications' import { floorToDecimal } from '../../utils/numbers' import ButtonGroup from '../forms/ButtonGroup' import Input from '../forms/Input' import Label from '../forms/Label' import Button, { LinkButton } from '../shared/Button' import Loading from '../shared/Loading' import Modal from '../shared/Modal' import { EnterBottomExitBottom, FadeInFadeOut } from '../shared/Transitions' import WithdrawTokenList from '../shared/WithdrawTokenList' interface WithdrawModalProps { token?: string } type ModalCombinedProps = WithdrawModalProps & ModalProps function WithdrawModal({ isOpen, onClose, token }: ModalCombinedProps) { const { t } = useTranslation('common') const [inputAmount, setInputAmount] = useState('') const [submitting, setSubmitting] = useState(false) const [selectedToken, setSelectedToken] = useState(token || 'USDC') const [showTokenList, setShowTokenList] = useState(false) const [sizePercentage, setSizePercentage] = useState('') const mangoAccount = mangoStore((s) => s.mangoAccount.current) const tokenMax = useMemo(() => { const group = mangoStore.getState().group const bank = group?.banksMap.get(selectedToken) console.log(group, bank) if (!group || !bank) return 0 const amount = mangoAccount?.getUi(bank) return amount ? floorToDecimal(amount, bank.mintDecimals) : 0 }, [mangoAccount, selectedToken]) const handleSizePercentage = useCallback( (percentage: string) => { setSizePercentage(percentage) const amount = (Number(percentage) / 100) * (tokenMax || 0) setInputAmount(amount.toString()) }, [tokenMax] ) const handleWithdraw = async () => { const client = mangoStore.getState().client const group = mangoStore.getState().group const mangoAccount = mangoStore.getState().mangoAccount.current const actions = mangoStore.getState().actions if (!mangoAccount || !group) return setSubmitting(true) try { const tx = await client.tokenWithdraw( group, mangoAccount, selectedToken, parseFloat(inputAmount), true ) notify({ title: 'Transaction confirmed', type: 'success', txid: tx, }) actions.reloadAccount() } catch (e: any) { console.log(e) notify({ title: 'Transaction failed', description: e.message, txid: e?.txid, type: 'error', }) } finally { setSubmitting(false) onClose() } } const handleSelectToken = (token: string) => { setSelectedToken(token) setShowTokenList(false) } return (

{t('select-token')}

{t('withdraw')}

) => setInputAmount(e.target.value) } />
handleSizePercentage(p)} values={['10', '25', '50', '75', '100']} unit="%" />
{/*

{t('health-impact')}

-12%

{t('withdrawal-value')}

$1,000.00

*/}
) } export default WithdrawModal