import { ModalProps } from '../../types/modal' import Modal from '../shared/Modal' import mangoStore from '@store/mangoStore' import { notify } from '../../utils/notifications' import Button from '../shared/Button' import { useTranslation } from 'next-i18next' import { useState } from 'react' import BounceLoader from '../shared/BounceLoader' import { MangoAccount } from '@blockworks-foundation/mango-v4' import { TrashIcon } from '@heroicons/react/20/solid' const CloseAccountModal = ({ isOpen, onClose }: ModalProps) => { const { t } = useTranslation('common') const [loading, setLoading] = useState(false) const set = mangoStore((s) => s.set) const handleCloseMangoAccount = async () => { const client = mangoStore.getState().client const mangoAccount = mangoStore.getState().mangoAccount.current const mangoAccounts = mangoStore.getState().mangoAccounts const group = mangoStore.getState().group if (!mangoAccount || !group) return setLoading(true) try { const tx = await client.closeMangoAccount(group, mangoAccount) if (tx) { const newMangoAccounts = mangoAccounts.filter( (ma) => !ma.publicKey.equals(mangoAccount.publicKey) ) let newCurrentAccount: MangoAccount if (newMangoAccounts[0]) { newCurrentAccount = await newMangoAccounts[0].reload(client) } setLoading(false) onClose() notify({ title: t('account-closed'), type: 'success', txid: tx, }) set((state) => { state.mangoAccounts = newMangoAccounts state.mangoAccount.current = newCurrentAccount state.mangoAccount.lastUpdatedAt = new Date().toISOString() }) } } catch (e) { setLoading(false) console.error(e) } } return (
{loading ? ( ) : (

{t('close-account')}

{t('close-account-desc')}

)}
) } export default CloseAccountModal