import React, { useMemo } from 'react'; import PropTypes from 'prop-types'; import { EditGasModes, PriorityLevels, } from '../../../../../shared/constants/gas'; import { FontWeight, TextColor, TextVariant, } from '../../../../helpers/constants/design-system'; import { isMetamaskSuggestedGasEstimate } from '../../../../helpers/utils/gas'; import { roundToDecimalPlacesRemovingExtraZeroes } from '../../../../helpers/utils/util'; import { Text } from '../../../component-library'; const EditGasToolTip = ({ editGasMode, estimateGreaterThanGasUse, gasLimit, priorityLevel, // maxFeePerGas & maxPriorityFeePerGas are derived from conditional logic // related to the source of the estimates. We pass these values from the // the parent component (edit-gas-item) rather than recalculate them maxFeePerGas, maxPriorityFeePerGas, transaction, t, }) => { const toolTipMessage = useMemo(() => { switch (priorityLevel) { case PriorityLevels.low: return t('lowGasSettingToolTipMessage', [ {t('low')} , ]); case PriorityLevels.medium: if (estimateGreaterThanGasUse) { return t('disabledGasOptionToolTipMessage', [ {t(priorityLevel)} , ]); } return t('mediumGasSettingToolTipMessage', [ {t('medium')} , ]); case PriorityLevels.high: if (estimateGreaterThanGasUse) { return t('disabledGasOptionToolTipMessage', [ {t(priorityLevel)} , ]); } if (editGasMode === EditGasModes.swaps) { return t('swapSuggestedGasSettingToolTipMessage'); } return t('highGasSettingToolTipMessage', [ {t('high')} , ]); case PriorityLevels.custom: return t('customGasSettingToolTipMessage', [ {t('custom')} , ]); case PriorityLevels.dAppSuggested: return transaction?.origin ? t('dappSuggestedGasSettingToolTipMessage', [ {transaction?.origin}, ]) : null; default: return ''; } }, [editGasMode, estimateGreaterThanGasUse, priorityLevel, transaction, t]); let imgAltText; if (priorityLevel === PriorityLevels.low) { imgAltText = t('curveLowGasEstimate'); } else if (priorityLevel === PriorityLevels.medium) { imgAltText = t('curveMediumGasEstimate'); } else if (priorityLevel === PriorityLevels.high) { imgAltText = t('curveHighGasEstimate'); } // Gas estimate curve is visible for low/medium/high gas estimates // the curve is not visible for high estimates for swaps // also it is not visible in case of cancel/speedup if the medium/high option is disabled const showGasEstimateCurve = isMetamaskSuggestedGasEstimate(priorityLevel) && !( priorityLevel === PriorityLevels.high && editGasMode === EditGasModes.swaps ) && !estimateGreaterThanGasUse; return (
{showGasEstimateCurve ? ( {imgAltText} ) : null} {toolTipMessage && (
{toolTipMessage}
)} {priorityLevel === PriorityLevels.custom || estimateGreaterThanGasUse ? null : (
{t('maxBaseFee')} {maxFeePerGas && ( {roundToDecimalPlacesRemovingExtraZeroes(maxFeePerGas, 4)} )}
{t('priorityFeeProperCase')} {maxPriorityFeePerGas && ( {roundToDecimalPlacesRemovingExtraZeroes( maxPriorityFeePerGas, 4, )} )}
{t('gasLimit')} {gasLimit && ( {roundToDecimalPlacesRemovingExtraZeroes(gasLimit, 4)} )}
)}
); }; EditGasToolTip.propTypes = { estimateGreaterThanGasUse: PropTypes.bool, priorityLevel: PropTypes.string, maxFeePerGas: PropTypes.oneOfType([PropTypes.number, PropTypes.string]), maxPriorityFeePerGas: PropTypes.oneOfType([ PropTypes.number, PropTypes.string, ]), t: PropTypes.func, editGasMode: PropTypes.string, gasLimit: PropTypes.number, transaction: PropTypes.object, }; export default EditGasToolTip;