import { AutoColumn, Box, Button, CloseIcon, Loader, Modal, Text } from '@honeycomb-finance/core'; import { Portfolio, Position } from '@honeycomb-finance/sar'; import { ExternalLink, getEtherscanLink, useChainId, useTranslation } from '@honeycomb-finance/shared'; import { CHAINS, GovernanceType, TokenAmount } from '@pangolindex/sdk'; import { BigNumber } from 'ethers'; import React, { useContext, useState } from 'react'; import { AlertTriangle, ArrowUpCircle } from 'react-feather'; import { ThemeContext } from 'styled-components'; import { useVoteCallbackHook } from 'src/hooks'; import { useUserVotes } from 'src/hooks/evm'; import { ConfirmOrLoadingWrapper, ConfirmedIcon, ContentWrapper, ErrorBox, ErrorWrapper, Wrapper } from './styleds'; interface VoteModalProps { isOpen: boolean; onDismiss: () => void; support: boolean; // if user is for or against proposal proposalId: string | undefined; // id for the proposal to vote on filteredPositions: Position[]; nftLoading?: boolean; } export default function VoteModal({ isOpen, onDismiss, proposalId, support, filteredPositions, nftLoading, }: VoteModalProps) { const chainId = useChainId(); const [voteErrorMessage, setVoteErrorMessage] = useState(undefined); const useVoteCallback = useVoteCallbackHook[chainId]; const { voteCallback, }: { voteCallback: ( proposalId: string | undefined, support: boolean, nftId?: BigNumber, ) => Promise | undefined; } = useVoteCallback(); const availableVotes: TokenAmount | undefined = useUserVotes(); // monitor call to help UI loading state const [hash, setHash] = useState(); const [attempting, setAttempting] = useState(false); const [selectedPosition, setSelectedPosition] = useState(null); const onSelectPosition = (position: Position | null) => { setSelectedPosition(position); }; // get theme for colors const theme = useContext(ThemeContext); const { t } = useTranslation(); // wrapper to reset state on modal close function wrappedOndismiss() { setHash(undefined); setAttempting(false); setVoteErrorMessage(undefined); onDismiss(); } async function onVote() { try { setAttempting(true); // if callback not returned properly ignore if (!voteCallback) return; // try delegation and store hash const _hash = await voteCallback(proposalId, support, selectedPosition?.id); if (_hash) { setHash(_hash); } else { setAttempting(false); setVoteErrorMessage('Something went wrong'); } } catch (err: any) { setAttempting(false); const _err = err as any; setVoteErrorMessage(err?.message); // we only care if the error is something _other_ than the user rejected the tx if (_err?.code !== 4001) { console.error(_err); } } } const renderModalBody = () => { if (CHAINS[chainId]?.contracts?.governor?.type === GovernanceType.SAR_NFT) { return ( Choose NFTs to Vote: {support ? t('vote.for').toUpperCase() : t('vote.against').toUpperCase()} ); } else { return ( {availableVotes?.toSignificant(4)} {t('vote.votes')} ); } }; return (
{!attempting && !hash && !nftLoading && !voteErrorMessage && <>{renderModalBody()}} {nftLoading && ( {' '} )} {attempting && !hash && !nftLoading && ( {t('vote.confirmTransaction')} )} {hash && ( {t('vote.transactionSubmitted')} {chainId && ( {t('vote.viewExplorer')} )} )} {voteErrorMessage && ( {voteErrorMessage || 'Something went wrong'} )} ); }