import React, { FunctionComponent, useEffect, useState } from 'react' import { ExclamationCircleIcon, InformationCircleIcon, } from '@heroicons/react/outline' import Input from './Input' import AccountSelect from './AccountSelect' import { ElementTitle } from './styles' import useMangoStore from '../stores/useMangoStore' import { getSymbolForTokenMintAddress, DECIMALS, trimDecimals, sleep, } from '../utils/index' import Loading from './Loading' import Button from './Button' import Slider from './Slider' import Tooltip from './Tooltip' import { notify } from '../utils/notifications' import { deposit } from '../utils/mango' interface NewAccountProps { onAccountCreation?: (x?) => void } const NewAccount: FunctionComponent = ({ onAccountCreation, }) => { const [inputAmount, setInputAmount] = useState(null) const [submitting, setSubmitting] = useState(false) const [invalidAmountMessage, setInvalidAmountMessage] = useState('') const [sliderPercentage, setSliderPercentage] = useState(0) const [maxButtonTransition, setMaxButtonTransition] = useState(false) const [invalidNameMessage, setInvalidNameMessage] = useState('') const [name, setName] = useState('') const walletTokens = useMangoStore((s) => s.wallet.tokens) const actions = useMangoStore((s) => s.actions) const [selectedAccount, setSelectedAccount] = useState(walletTokens[0]) const symbol = getSymbolForTokenMintAddress( selectedAccount?.account?.mint.toString() ) const handleAccountSelect = (account) => { setInputAmount(0) setSliderPercentage(0) setInvalidAmountMessage('') setSelectedAccount(account) } const setMaxForSelectedAccount = () => { const max = selectedAccount.uiBalance setInputAmount(max) setSliderPercentage(100) setInvalidAmountMessage('') setMaxButtonTransition(true) } const handleNewAccountDeposit = () => { setSubmitting(true) deposit({ amount: inputAmount, fromTokenAcc: selectedAccount.account, accountName: name, }) .then(async (response) => { await sleep(1000) actions.fetchWalletTokens() actions.fetchMangoAccounts() setSubmitting(false) console.log('response', response) onAccountCreation(response) }) .catch((err) => { setSubmitting(false) console.error(err) notify({ title: 'Could not perform init margin account and deposit operation', type: 'error', }) onAccountCreation() }) } const validateAmountInput = (amount) => { if (Number(amount) <= 0) { setInvalidAmountMessage('Enter an amount to deposit') } if (Number(amount) > selectedAccount.uiBalance) { setInvalidAmountMessage( 'Insufficient balance. Reduce the amount to deposit' ) } } const onChangeAmountInput = (amount) => { const max = selectedAccount.uiBalance setInputAmount(amount) setSliderPercentage((amount / max) * 100) setInvalidAmountMessage('') } const onChangeSlider = async (percentage) => { const max = selectedAccount.uiBalance const amount = (percentage / 100) * max if (percentage === 100) { setInputAmount(amount) } else { setInputAmount(trimDecimals(amount, DECIMALS[symbol])) } setSliderPercentage(percentage) setInvalidAmountMessage('') validateAmountInput(amount) } const validateNameInput = () => { if (name.length >= 33) { setInvalidNameMessage('Account name must be 32 characters or less') } if (name.length === 0) { setInvalidNameMessage('Enter an account name') } } const onChangeNameInput = (name) => { setName(name) if (invalidNameMessage) { setInvalidNameMessage('') } } // turn off slider transition for dragging slider handle interaction useEffect(() => { if (maxButtonTransition) { setMaxButtonTransition(false) } }, [maxButtonTransition]) return ( <> New Account
Account Name (Optional)
onChangeNameInput(e.target.value)} /> {invalidNameMessage ? (
{invalidNameMessage}
) : null}
Amount
Max
validateAmountInput(e.target.value)} value={inputAmount || ''} onChange={(e) => onChangeAmountInput(e.target.value)} suffix={symbol} />
{invalidAmountMessage ? (
{invalidAmountMessage}
) : null}
onChangeSlider(v)} step={1} maxButtonTransition={maxButtonTransition} />
) } export default NewAccount