import { Transition } from '@headlessui/react' import { ArrowDownTrayIcon, ArrowTopRightOnSquareIcon, CheckCircleIcon, ExclamationCircleIcon, PencilIcon, } from '@heroicons/react/20/solid' import { useWallet } from '@solana/wallet-adapter-react' import mangoStore from '@store/mangoStore' import Decimal from 'decimal.js' import useMangoAccount from 'hooks/useMangoAccount' import useSolBalance from 'hooks/useSolBalance' import { useTranslation } from 'next-i18next' import Image from 'next/image' import { ChangeEvent, ReactNode, useCallback, useEffect, useMemo, useState, } from 'react' import { notify } from 'utils/notifications' import ActionTokenList from '../account/ActionTokenList' import ButtonGroup from '../forms/ButtonGroup' import Input from '../forms/Input' import Label from '../forms/Label' import WalletIcon from '../icons/WalletIcon' // import ParticlesBackground from '../ParticlesBackground' // import EditNftProfilePic from '../profile/EditNftProfilePic' // import EditProfileForm from '../profile/EditProfileForm' import Button, { LinkButton } from '../shared/Button' import InlineNotification from '../shared/InlineNotification' import Loading from '../shared/Loading' import MaxAmountButton from '../shared/MaxAmountButton' import SolBalanceWarnings from '../shared/SolBalanceWarnings' import { useEnhancedWallet } from '../wallet/EnhancedWalletProvider' import Modal from '../shared/Modal' import NumberFormat, { NumberFormatValues } from 'react-number-format' import { withValueLimit } from '@components/swap/SwapForm' import useBanksWithBalances from 'hooks/useBanksWithBalances' import BankAmountWithValue from '@components/shared/BankAmountWithValue' import { isMangoError } from 'types' import ColorBlur from '@components/ColorBlur' import useLocalStorageState from 'hooks/useLocalStorageState' import { ACCEPT_TERMS_KEY } from 'utils/constants' import { ACCOUNT_ACTIONS_NUMBER_FORMAT_CLASSES } from '@components/BorrowForm' const UserSetupModal = ({ isOpen, onClose, }: { isOpen: boolean onClose: () => void }) => { const { t } = useTranslation(['common', 'onboarding', 'swap']) const { connected, select, wallet, wallets, publicKey } = useWallet() const { mangoAccount } = useMangoAccount() const mangoAccountLoading = mangoStore((s) => s.mangoAccount.initialLoad) const [accountName, setAccountName] = useState('') const [loadingAccount, setLoadingAccount] = useState(false) const [showSetupStep, setShowSetupStep] = useState(0) const [depositToken, setDepositToken] = useState('USDC') const [depositAmount, setDepositAmount] = useState('') const [submitDeposit, setSubmitDeposit] = useState(false) const [sizePercentage, setSizePercentage] = useState('') // const [showEditProfilePic, setShowEditProfilePic] = useState(false) const { handleConnect } = useEnhancedWallet() const { maxSolDeposit } = useSolBalance() const banks = useBanksWithBalances('walletBalance') const [, setAcceptTerms] = useLocalStorageState(ACCEPT_TERMS_KEY, '') useEffect(() => { if (connected) { setShowSetupStep(2) } }, [connected]) const handleCreateAccount = useCallback(async () => { const client = mangoStore.getState().client const group = mangoStore.getState().group const actions = mangoStore.getState().actions if (!group || !publicKey) return setLoadingAccount(true) try { const tx = await client.createMangoAccount( group, 0, accountName || 'Account 1', 16 // tokenCount ) actions.fetchMangoAccounts(publicKey) if (tx) { actions.fetchWalletTokens(publicKey) // need to update sol balance after account rent setShowSetupStep(3) notify({ title: t('new-account-success'), type: 'success', txid: tx, }) } } catch (e) { if (isMangoError(e)) { notify({ title: t('new-account-failed'), txid: e?.txid, type: 'error', }) } console.error(e) } finally { setLoadingAccount(false) } }, [accountName, publicKey, t]) const handleDeposit = useCallback(async () => { const client = mangoStore.getState().client const group = mangoStore.getState().group const actions = mangoStore.getState().actions const mangoAccount = mangoStore.getState().mangoAccount.current const bank = group?.banksMapByName.get(depositToken)?.[0] if (!mangoAccount || !group || !bank) return try { setSubmitDeposit(true) const tx = await client.tokenDeposit( group, mangoAccount, bank.mint, parseFloat(depositAmount) ) notify({ title: 'Transaction confirmed', type: 'success', txid: tx, }) await actions.reloadMangoAccount() setSubmitDeposit(false) onClose() // setShowSetupStep(4) } catch (e) { setSubmitDeposit(false) console.error(e) if (!isMangoError(e)) return notify({ title: 'Transaction failed', description: e.message, txid: e?.txid, type: 'error', }) } }, [depositAmount, depositToken, onClose]) useEffect(() => { if (mangoAccount && showSetupStep === 2) { onClose() } }, [mangoAccount, showSetupStep, onClose]) const depositBank = useMemo(() => { return banks.find((b) => b.bank.name === depositToken)?.bank }, [depositToken, banks]) const tokenMax = useMemo(() => { const bank = banks.find((b) => b.bank.name === depositToken) if (bank) { return { amount: bank.walletBalance, decimals: bank.bank.mintDecimals } } return { amount: 0, decimals: 0 } }, [banks, depositToken]) const showInsufficientBalance = tokenMax.amount < Number(depositAmount) || (depositToken === 'SOL' && maxSolDeposit <= 0) const setMax = useCallback(() => { const max = new Decimal(tokenMax.amount).toDecimalPlaces( tokenMax.decimals, Decimal.ROUND_FLOOR ) setDepositAmount(max.toString()) setSizePercentage('100') }, [tokenMax]) const handleSizePercentage = useCallback( (percentage: string) => { setSizePercentage(percentage) const amount = new Decimal(tokenMax.amount) .mul(percentage) .div(100) .toDecimalPlaces(tokenMax.decimals, Decimal.ROUND_FLOOR) setDepositAmount(amount.toString()) }, [tokenMax] ) const handleNextStep = () => { if (showSetupStep === 0) { setAcceptTerms(Date.now()) } setShowSetupStep(showSetupStep + 1) } return (
next

{t('onboarding:intro-heading')}

{t('onboarding:intro-desc')}

{t('accept-terms-desc')} {t('terms-of-use')}

{showSetupStep === 1 ? (

{t('onboarding:connect-wallet')}

{t('onboarding:choose-wallet')}

{wallets?.map((w) => ( ))}
) : null}
{showSetupStep === 2 ? (

{t('onboarding:create-account')}

{t('onboarding:create-account-desc')}

{t('onboarding:skip')}
) : null}
{showSetupStep === 3 ? (

{t('onboarding:fund-account')}

0}>
{ setDepositAmount( !Number.isNaN(Number(e.value)) ? e.value : '' ) }} isAllowed={withValueLimit} />
handleSizePercentage(p)} values={['10', '25', '50', '75', '100']} unit="%" />
{depositBank ? (

{t('deposit-amount')}

{/* {depositAmount ? ( <> {' '} ( ) ) : ( <> 0{' '} ($0.00) )} */}

) : null} {t('onboarding:skip')}

{t('token')}

{t('deposit-rate')}

{t('wallet-balance')}

) : null}
{/* */} next
) } export default UserSetupModal const CheckBullet = ({ text }: { text: string }) => { return (

{text}

) } const UserSetupTransition = ({ show, children, delay = false, }: { show: boolean children: ReactNode delay?: boolean }) => { return ( {children} ) }