import { FunctionComponent, useState } from 'react' import useMangoStore from '../stores/useMangoStore' import { ExclamationCircleIcon, InformationCircleIcon, } from '@heroicons/react/outline' import Input, { Label } from './Input' import Button from './Button' import Modal from './Modal' import { ElementTitle } from './styles' import Tooltip from './Tooltip' import { notify } from '../utils/notifications' import { useTranslation } from 'next-i18next' import { useWallet } from '@solana/wallet-adapter-react' interface AccountNameModalProps { accountName?: string isOpen: boolean onClose?: (x?) => void } const AccountNameModal: FunctionComponent = ({ accountName, isOpen, onClose, }) => { const { t } = useTranslation('common') const { wallet } = useWallet() const [name, setName] = useState(accountName || '') const [invalidNameMessage, setInvalidNameMessage] = useState('') const mangoGroup = useMangoStore((s) => s.selectedMangoGroup.current) const mangoAccount = useMangoStore((s) => s.selectedMangoAccount.current) const actions = useMangoStore((s) => s.actions) const submitName = async () => { const mangoClient = useMangoStore.getState().connection.client if (!wallet || !mangoAccount || !mangoGroup) return try { const txid = await mangoClient.addMangoAccountInfo( mangoGroup, mangoAccount, wallet?.adapter, name ) actions.fetchAllMangoAccounts(wallet) actions.reloadMangoAccount() onClose?.() notify({ title: t('name-updated'), txid, }) } catch (err) { console.warn('Error setting account name:', err) notify({ title: t('name-error'), description: `${err}`, txid: err.txid, type: 'error', }) } } const validateNameInput = () => { if (name.length >= 33) { setInvalidNameMessage(t('character-limit')) } if (name.length === 0) { setInvalidNameMessage(t('enter-name')) } } const onChangeNameInput = (name) => { setName(name) if (invalidNameMessage) { setInvalidNameMessage('') } } return ( {t('name-your-account')}

{t('edit-nickname')}

onChangeNameInput(e.target.value)} /> {invalidNameMessage ? (
{invalidNameMessage}
) : null}
) } export default AccountNameModal