import { useSelector } from 'react-redux'; import React, { useEffect } from 'react'; import { EDIT_GAS_MODES, PRIORITY_LEVELS, } from '../../../../shared/constants/gas'; import { ALIGN_ITEMS, DISPLAY, FLEX_DIRECTION, TYPOGRAPHY, } from '../../../helpers/constants/design-system'; import { getAppIsLoading } from '../../../selectors'; import { gasEstimateGreaterThanGasUsedPlusTenPercent } from '../../../helpers/utils/gas'; import { useGasFeeContext } from '../../../contexts/gasFee'; import { useI18nContext } from '../../../hooks/useI18nContext'; import { useTransactionModalContext } from '../../../contexts/transaction-modal'; import EditGasFeeButton from '../edit-gas-fee-button'; import GasDetailsItem from '../gas-details-item'; import Box from '../../ui/box'; import Button from '../../ui/button'; import I18nValue from '../../ui/i18n-value'; import InfoTooltip from '../../ui/info-tooltip'; import Popover from '../../ui/popover'; import Typography from '../../ui/typography'; import AppLoadingSpinner from '../app-loading-spinner'; const CancelSpeedupPopover = () => { const { cancelTransaction, editGasMode, gasFeeEstimates, speedUpTransaction, transaction, updateTransaction, updateTransactionToTenPercentIncreasedGasFee, updateTransactionUsingEstimate, } = useGasFeeContext(); const t = useI18nContext(); const { closeModal, currentModal } = useTransactionModalContext(); const appIsLoading = useSelector(getAppIsLoading); useEffect(() => { if ( transaction.previousGas || appIsLoading || currentModal !== 'cancelSpeedUpTransaction' ) { return; } // If gas used previously + 10% is less than medium estimated gas // estimate is set to medium, else estimate is set to tenPercentIncreased const gasUsedLessThanMedium = gasFeeEstimates && gasEstimateGreaterThanGasUsedPlusTenPercent( transaction.txParams, gasFeeEstimates, PRIORITY_LEVELS.MEDIUM, ); if (gasUsedLessThanMedium) { updateTransactionUsingEstimate(PRIORITY_LEVELS.MEDIUM); return; } updateTransactionToTenPercentIncreasedGasFee(true); }, [ appIsLoading, currentModal, editGasMode, gasFeeEstimates, transaction, updateTransaction, updateTransactionToTenPercentIncreasedGasFee, updateTransactionUsingEstimate, ]); if (currentModal !== 'cancelSpeedUpTransaction') { return null; } const submitTransactionChange = () => { if (editGasMode === EDIT_GAS_MODES.CANCEL) { cancelTransaction(); } else { speedUpTransaction(); } closeModal(['cancelSpeedUpTransaction']); }; return ( {editGasMode === EDIT_GAS_MODES.CANCEL ? `❌${t('cancel')}` : `🚀${t('speedUp')}`} } onClose={() => closeModal(['cancelSpeedUpTransaction'])} className="cancel-speedup-popover" >
, ]} /> {t('cancelSpeedUpTransactionTooltip', [ EDIT_GAS_MODES.CANCEL ? t('cancel') : t('speedUp'), ])} } />
{!appIsLoading && }
); }; export default CancelSpeedupPopover;