1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00
metamask-extension/ui/components/app/edit-gas-fee-button/edit-gas-fee-button.js

122 lines
3.7 KiB
JavaScript
Raw Normal View History

import React from 'react';
import PropTypes from 'prop-types';
import { EditGasModes, PriorityLevels } from '../../../../shared/constants/gas';
import {
Color,
TextColor,
TypographyVariant,
} from '../../../helpers/constants/design-system';
import { PRIORITY_LEVEL_ICON_MAP } from '../../../helpers/constants/gas';
import { useGasFeeContext } from '../../../contexts/gasFee';
import { useI18nContext } from '../../../hooks/useI18nContext';
import { useTransactionEventFragment } from '../../../hooks/useTransactionEventFragment';
import { useTransactionModalContext } from '../../../contexts/transaction-modal';
import InfoTooltip from '../../ui/info-tooltip/info-tooltip';
import Typography from '../../ui/typography/typography';
import {
Icon,
ICON_NAMES,
ICON_SIZES,
} from '../../component-library/icon/deprecated';
export default function EditGasFeeButton({ userAcknowledgedGasMissing }) {
const t = useI18nContext();
const {
2021-12-12 00:26:28 +01:00
editGasMode,
gasLimit,
hasSimulationError,
estimateUsed,
maxFeePerGas,
maxPriorityFeePerGas,
supportsEIP1559,
transaction,
} = useGasFeeContext();
const { updateTransactionEventFragment } = useTransactionEventFragment();
const { openModal } = useTransactionModalContext();
const editEnabled =
!hasSimulationError || userAcknowledgedGasMissing === true;
if (!supportsEIP1559 || !estimateUsed || !editEnabled) {
return null;
}
2021-12-12 00:26:28 +01:00
let icon = estimateUsed;
let title = estimateUsed;
if (
estimateUsed === PriorityLevels.high &&
editGasMode === EditGasModes.swaps
2021-12-12 00:26:28 +01:00
) {
icon = 'swapSuggested';
title = 'swapSuggested';
} else if (estimateUsed === PriorityLevels.tenPercentIncreased) {
icon = undefined;
2022-01-06 23:40:31 +01:00
title = 'tenPercentIncreased';
2021-12-12 00:26:28 +01:00
}
const openEditGasFeeModal = () => {
updateTransactionEventFragment({
gas_edit_attempted: 'basic',
});
openModal('editGasFee');
};
const openAdvancedGasFeeModal = () => {
updateTransactionEventFragment({
gas_edit_attempted: 'advanced',
});
openModal('advancedGasFee');
};
return (
<div className="edit-gas-fee-button">
<button onClick={openEditGasFeeModal} data-testid="edit-gas-fee-button">
{icon && (
<span className="edit-gas-fee-button__icon">
{PRIORITY_LEVEL_ICON_MAP[icon]}
</span>
)}
2021-12-12 00:26:28 +01:00
<span className="edit-gas-fee-button__label">{t(title)}</span>
<Icon
name={ICON_NAMES.ARROW_RIGHT}
color={Color.primaryDefault}
size={ICON_SIZES.XS}
/>
</button>
{estimateUsed === 'custom' && (
<button onClick={openAdvancedGasFeeModal}>{t('edit')}</button>
)}
{estimateUsed === 'dappSuggested' && (
<InfoTooltip
contentText={
<div className="edit-gas-fee-button__tooltip">
{transaction?.origin && (
<Typography
variant={TypographyVariant.H7}
color={TextColor.textAlternative}
>
{t('dappSuggestedTooltip', [transaction.origin])}
</Typography>
)}
<Typography variant={TypographyVariant.H7}>
<b>{t('maxBaseFee')}</b> {maxFeePerGas}
</Typography>
<Typography variant={TypographyVariant.H7}>
<b>{t('maxPriorityFee')}</b> {maxPriorityFeePerGas}
</Typography>
<Typography variant={TypographyVariant.H7}>
<b>{t('gasLimit')}</b> {gasLimit}
</Typography>
</div>
}
position="top"
/>
)}
</div>
);
}
EditGasFeeButton.propTypes = {
userAcknowledgedGasMissing: PropTypes.bool,
};