import { FunctionComponent, useState } from 'react' import useMangoStore from '../stores/useMangoStore' import { ExclamationCircleIcon, XIcon, InformationCircleIcon, } from '@heroicons/react/outline' import Input, { Label } from './Input' import Tooltip from './Tooltip' import Button, { IconButton } from './Button' import Modal from './Modal' import { ElementTitle } from './styles' import { notify } from '../utils/notifications' import { useTranslation } from 'next-i18next' import { PublicKey } from '@solana/web3.js' import { useWallet } from '@solana/wallet-adapter-react' interface DelegateModalProps { delegate?: PublicKey isOpen: boolean onClose?: (x?) => void } const DelegateModal: FunctionComponent = ({ delegate, isOpen, onClose, }) => { const { t } = useTranslation(['common', 'delegate']) const { wallet } = useWallet() const [keyBase58, setKeyBase58] = useState( delegate && delegate.equals(PublicKey.default) ? '' : delegate ? delegate.toBase58() : '' ) const [invalidKeyMessage, setInvalidKeyMessage] = useState('') const mangoGroup = useMangoStore((s) => s.selectedMangoGroup.current) const mangoAccount = useMangoStore((s) => s.selectedMangoAccount.current) const actions = useMangoStore((s) => s.actions) const setDelegate = async () => { const mangoClient = useMangoStore.getState().connection.client if (!mangoGroup || !mangoAccount || !wallet) return try { const key = keyBase58.length ? new PublicKey(keyBase58) : PublicKey.default const txid = await mangoClient.setDelegate( mangoGroup, mangoAccount, wallet.adapter, key ) actions.reloadMangoAccount() onClose?.() notify({ title: t('delegate:delegate-updated'), txid, }) } catch (err) { console.warn('Error setting delegate key:', err) notify({ title: t('delegate:set-error'), description: `${err}`, txid: err.txid, type: 'error', }) } } const validateKeyInput = () => { if (isKeyValid()) { setInvalidKeyMessage('') } else { setInvalidKeyMessage(t('invalid-address')) } } const isKeyValid = () => { try { if (keyBase58.length == 0) { return true } // will throw if key is wrong length new PublicKey(keyBase58) return true } catch (e) { return false } } const onChangeKeyInput = (name) => { setKeyBase58(name) validateKeyInput() } return (
{t('delegate:delegate-your-account')} {t('learn-more')}
} >

{t('delegate:info')}

{ validateKeyInput() onChangeKeyInput(e.target.value) }} suffix={ { onChangeKeyInput('') }} > } /> {invalidKeyMessage ? (
{invalidKeyMessage}
) : null}
) } export default DelegateModal