import classNames from 'classnames';
import PropTypes from 'prop-types';
import React from 'react';
import {
EDIT_GAS_MODES,
PRIORITY_LEVELS,
} from '../../../../../shared/constants/gas';
import {
ALIGN_ITEMS,
DISPLAY,
} from '../../../../helpers/constants/design-system';
import { PRIORITY_LEVEL_ICON_MAP } from '../../../../helpers/constants/gas';
import { PRIMARY } from '../../../../helpers/constants/common';
import { toHumanReadableTime } from '../../../../helpers/utils/util';
import { useGasFeeContext } from '../../../../contexts/gasFee';
import { useI18nContext } from '../../../../hooks/useI18nContext';
import { useTransactionModalContext } from '../../../../contexts/transaction-modal';
import Box from '../../../ui/box';
import EditGasToolTip from '../edit-gas-tooltip/edit-gas-tooltip';
import InfoTooltip from '../../../ui/info-tooltip';
import LoadingHeartBeat from '../../../ui/loading-heartbeat';
import UserPreferencedCurrencyDisplay from '../../user-preferenced-currency-display';
import { useGasItemFeeDetails } from './useGasItemFeeDetails';
const getTitleAndIcon = (priorityLevel, t, editGasMode) => {
let icon = priorityLevel;
let title = t(priorityLevel);
if (priorityLevel === PRIORITY_LEVELS.DAPP_SUGGESTED) {
title = t('dappSuggestedShortLabel');
} else if (priorityLevel === PRIORITY_LEVELS.MINIMUM) {
icon = null;
title = (
{t('minimumCancelSpeedupGasFee')}
({t('minimum')})
);
} else if (
priorityLevel === PRIORITY_LEVELS.HIGH &&
editGasMode === EDIT_GAS_MODES.SWAPS
) {
icon = 'swapSuggested';
title = t('swapSuggested');
}
return { title, icon };
};
const EditGasItem = ({ priorityLevel }) => {
const {
editGasMode,
estimateUsed,
gasLimit,
updateTransactionToMinimumGasFee,
updateTransactionUsingDAPPSuggestedValues,
updateTransactionUsingEstimate,
transaction,
} = useGasFeeContext();
const t = useI18nContext();
const { closeModal, openModal } = useTransactionModalContext();
const { dappSuggestedGasFees } = transaction;
const {
// for cancel or speedup estimateGreaterThaGasUse is true if previous gas used
// was more than estimate for the priorityLevel
estimateGreaterThanGasUse,
hexMaximumTransactionFee,
maxFeePerGas,
maxPriorityFeePerGas,
minWaitTime,
} = useGasItemFeeDetails(priorityLevel);
if (
priorityLevel === PRIORITY_LEVELS.DAPP_SUGGESTED &&
!dappSuggestedGasFees
) {
return null;
}
const onOptionSelect = () => {
if (priorityLevel === PRIORITY_LEVELS.CUSTOM) {
openModal('advancedGasFee');
} else {
closeModal('editGasFee');
if (priorityLevel === PRIORITY_LEVELS.MINIMUM) {
updateTransactionToMinimumGasFee();
} else if (priorityLevel === PRIORITY_LEVELS.DAPP_SUGGESTED) {
updateTransactionUsingDAPPSuggestedValues();
} else {
updateTransactionUsingEstimate(priorityLevel);
}
}
};
const { title, icon } = getTitleAndIcon(priorityLevel, t, editGasMode);
return (
);
};
EditGasItem.propTypes = {
priorityLevel: PropTypes.string,
};
export default EditGasItem;