import { AutoColumn, Box, Button, CloseIcon, Loader, Modal, Text } from '@honeycomb-finance/core'; import { ExternalLink, PNG, getEtherscanLink, useChainId, useENS, usePangolinWeb3, usePngSymbol, useTranslation, } from '@honeycomb-finance/shared'; import { useTokenBalance } from '@honeycomb-finance/state-hooks'; import { isAddress } from 'ethers/lib/utils'; import React, { useContext, useState } from 'react'; import { ThemeContext } from 'styled-components'; import { useDelegateCallback } from 'src/hooks/evm'; import AddressInputPanel from '../AddressInputPanel'; import { ContentWrapper, DelegateModalWrapper, TextButton, Wrapper } from './styleds'; interface VoteModalProps { isOpen: boolean; onDismiss: () => void; title: string; } export default function DelegateModal({ isOpen, onDismiss, title }: VoteModalProps) { const { account } = usePangolinWeb3(); const { t } = useTranslation(); const chainId = useChainId(); const pngSymbol = usePngSymbol(); // get theme for colors const theme = useContext(ThemeContext); // state for delegate input const [usingDelegate, setUsingDelegate] = useState(false); const [typed, setTyped] = useState(''); function handleRecipientType(val: string) { setTyped(val); } // monitor for self delegation or input for third part delegate // default is self delegation const activeDelegate = usingDelegate ? typed : account; const { address: parsedAddress } = useENS(activeDelegate); // get the number of votes available to delegate const pngBalance = useTokenBalance(account ?? undefined, chainId ? PNG[chainId] : undefined); const delegateCallback = useDelegateCallback(); // monitor call to help UI loading state const [hash, setHash] = useState(); const [attempting, setAttempting] = useState(false); // wrapper to reset state on modal close function wrappedOndismiss() { setHash(undefined); setAttempting(false); onDismiss(); } async function onDelegate() { setAttempting(true); // if callback not returned properly ignore if (!delegateCallback) return; // try delegation and store hash const _hash = await delegateCallback(parsedAddress ?? undefined)?.catch((error) => { setAttempting(false); console.log(error); }); if (_hash) { setHash(_hash); } } return ( {title} {!attempting && !hash && ( {t('vote.earnedPng', { pngSymbol: pngSymbol })} {t('vote.canEitherVote')} {usingDelegate && } setUsingDelegate(!usingDelegate)}> {usingDelegate ? t('vote.remove') : t('vote.add')} {t('vote.delegate')} {!usingDelegate && '+'} )} {attempting && !hash && ( {pngBalance?.toSignificant(4)} )} {hash && ( {t('vote.transactionSubmitted')} {pngBalance?.toSignificant(4)} {chainId && hash && ( {t('modalView.viewTransaction')} )} )} ); }