import { FunctionComponent, useState } from 'react' import useMangoStore from '../stores/useMangoStore' import { ExclamationCircleIcon, InformationCircleIcon, } from '@heroicons/react/outline' import Input 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' interface AccountNameModalProps { accountName?: string isOpen: boolean onClose?: (x?) => void } const AccountNameModal: FunctionComponent = ({ accountName, isOpen, onClose, }) => { const { t } = useTranslation('common') const [name, setName] = useState(accountName || '') const [invalidNameMessage, setInvalidNameMessage] = useState('') const mangoGroup = useMangoStore((s) => s.selectedMangoGroup.current) const mangoAccount = useMangoStore((s) => s.selectedMangoAccount.current) const mangoClient = useMangoStore((s) => s.connection.client) const actions = useMangoStore((s) => s.actions) const submitName = async () => { const wallet = useMangoStore.getState().wallet.current try { const txid = await mangoClient.addMangoAccountInfo( mangoGroup, mangoAccount, wallet, name ) actions.fetchAllMangoAccounts() 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')}
{t('account-name')}
onChangeNameInput(e.target.value)} /> {invalidNameMessage ? (
{invalidNameMessage}
) : null}
) } export default AccountNameModal