1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-01 21:57:06 +01:00
metamask-extension/ui/components/app/gas-details-item/gas-details-item.js

149 lines
5.0 KiB
JavaScript
Raw Normal View History

import React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import { useSelector } from 'react-redux';
import { TextColor } from '../../../helpers/constants/design-system';
import { PRIMARY, SECONDARY } from '../../../helpers/constants/common';
import {
getPreferences,
getUseCurrencyRateCheck,
transactionFeeSelector,
} from '../../../selectors';
import { getCurrentDraftTransaction } from '../../../ducks/send';
import { useGasFeeContext } from '../../../contexts/gasFee';
import { useI18nContext } from '../../../hooks/useI18nContext';
import Box from '../../ui/box';
import LoadingHeartBeat from '../../ui/loading-heartbeat';
import GasTiming from '../gas-timing/gas-timing.component';
import TransactionDetailItem from '../transaction-detail-item/transaction-detail-item.component';
import UserPreferencedCurrencyDisplay from '../user-preferenced-currency-display';
import { hexWEIToDecGWEI } from '../../../../shared/modules/conversion.utils';
import { useDraftTransactionWithTxParams } from '../../../hooks/useDraftTransactionWithTxParams';
When gas fees suggested by dapp is too high, show warning color and icon (#19088) * When gas fees suggested by dapp is too high, show warning color and icon Signed-off-by: Olusegun Akintayo <akintayo.segun@gmail.com> tests Signed-off-by: Olusegun Akintayo <akintayo.segun@gmail.com> Fix tests Signed-off-by: Olusegun Akintayo <akintayo.segun@gmail.com> set a default for high gas fees Signed-off-by: Olusegun Akintayo <akintayo.segun@gmail.com> Fix test cases where transaction is undefined. Signed-off-by: Olusegun Akintayo <akintayo.segun@gmail.com> Fix locale error Signed-off-by: Olusegun Akintayo <akintayo.segun@gmail.com> Fix error where dappSuggestedGasFees is null Signed-off-by: Olusegun Akintayo <akintayo.segun@gmail.com> Fix icon for site suggested Signed-off-by: Olusegun Akintayo <akintayo.segun@gmail.com> Fix unit tests snapshot Signed-off-by: Olusegun Akintayo <akintayo.segun@gmail.com> * Fix QA Comments Signed-off-by: Olusegun Akintayo <akintayo.segun@gmail.com> * lint:fix Signed-off-by: Olusegun Akintayo <akintayo.segun@gmail.com> * Fix unit tests Signed-off-by: Olusegun Akintayo <akintayo.segun@gmail.com> * Fix PR comments Signed-off-by: Olusegun Akintayo <akintayo.segun@gmail.com> * Lint fix Signed-off-by: Olusegun Akintayo <akintayo.segun@gmail.com> * Fix PR comment. - call setEstimateUsed only once. Signed-off-by: Olusegun Akintayo <akintayo.segun@gmail.com> * use constants for Priority levels. Signed-off-by: Olusegun Akintayo <akintayo.segun@gmail.com> --------- Signed-off-by: Olusegun Akintayo <akintayo.segun@gmail.com>
2023-06-08 12:26:18 +02:00
import { PriorityLevels } from '../../../../shared/constants/gas';
2021-12-12 00:26:28 +01:00
import GasDetailsItemTitle from './gas-details-item-title';
const GasDetailsItem = ({
'data-testid': dataTestId,
userAcknowledgedGasMissing = false,
}) => {
const t = useI18nContext();
const draftTransaction = useSelector(getCurrentDraftTransaction);
const transactionData = useDraftTransactionWithTxParams();
const {
hexMinimumTransactionFee: draftHexMinimumTransactionFee,
hexMaximumTransactionFee: draftHexMaximumTransactionFee,
} = useSelector((state) => transactionFeeSelector(state, transactionData));
const {
estimateUsed,
hasSimulationError,
maximumCostInHexWei: hexMaximumTransactionFee,
minimumCostInHexWei: hexMinimumTransactionFee,
maxPriorityFeePerGas,
maxFeePerGas,
} = useGasFeeContext();
const { useNativeCurrencyAsPrimaryCurrency } = useSelector(getPreferences);
const useCurrencyRateCheck = useSelector(getUseCurrencyRateCheck);
if (hasSimulationError && !userAcknowledgedGasMissing) {
return null;
}
const maxPriorityFeePerGasToRender = (
maxPriorityFeePerGas ??
hexWEIToDecGWEI(transactionData.txParams?.maxPriorityFeePerGas ?? '0x0')
).toString();
const maxFeePerGasToRender = (
maxFeePerGas ??
hexWEIToDecGWEI(transactionData.txParams?.maxFeePerGas ?? '0x0')
).toString();
return (
<TransactionDetailItem
key="gas-details-item"
data-testid={dataTestId}
2021-12-12 00:26:28 +01:00
detailTitle={<GasDetailsItemTitle />}
detailTitleColor={TextColor.textDefault}
detailText={
useCurrencyRateCheck &&
Object.keys(draftTransaction).length === 0 && (
<div className="gas-details-item__currency-container">
<LoadingHeartBeat estimateUsed={estimateUsed} />
<UserPreferencedCurrencyDisplay
type={SECONDARY}
value={hexMinimumTransactionFee}
hideLabel={Boolean(useNativeCurrencyAsPrimaryCurrency)}
/>
</div>
)
}
detailTotal={
<div className="gas-details-item__currency-container">
2022-01-26 19:18:43 +01:00
<LoadingHeartBeat estimateUsed={estimateUsed} />
<UserPreferencedCurrencyDisplay
type={PRIMARY}
value={hexMinimumTransactionFee || draftHexMinimumTransactionFee}
hideLabel={!useNativeCurrencyAsPrimaryCurrency}
/>
</div>
}
subText={
<>
<Box
key="editGasSubTextFeeLabel"
display="inline-flex"
className={classNames('gas-details-item__gasfee-label', {
When gas fees suggested by dapp is too high, show warning color and icon (#19088) * When gas fees suggested by dapp is too high, show warning color and icon Signed-off-by: Olusegun Akintayo <akintayo.segun@gmail.com> tests Signed-off-by: Olusegun Akintayo <akintayo.segun@gmail.com> Fix tests Signed-off-by: Olusegun Akintayo <akintayo.segun@gmail.com> set a default for high gas fees Signed-off-by: Olusegun Akintayo <akintayo.segun@gmail.com> Fix test cases where transaction is undefined. Signed-off-by: Olusegun Akintayo <akintayo.segun@gmail.com> Fix locale error Signed-off-by: Olusegun Akintayo <akintayo.segun@gmail.com> Fix error where dappSuggestedGasFees is null Signed-off-by: Olusegun Akintayo <akintayo.segun@gmail.com> Fix icon for site suggested Signed-off-by: Olusegun Akintayo <akintayo.segun@gmail.com> Fix unit tests snapshot Signed-off-by: Olusegun Akintayo <akintayo.segun@gmail.com> * Fix QA Comments Signed-off-by: Olusegun Akintayo <akintayo.segun@gmail.com> * lint:fix Signed-off-by: Olusegun Akintayo <akintayo.segun@gmail.com> * Fix unit tests Signed-off-by: Olusegun Akintayo <akintayo.segun@gmail.com> * Fix PR comments Signed-off-by: Olusegun Akintayo <akintayo.segun@gmail.com> * Lint fix Signed-off-by: Olusegun Akintayo <akintayo.segun@gmail.com> * Fix PR comment. - call setEstimateUsed only once. Signed-off-by: Olusegun Akintayo <akintayo.segun@gmail.com> * use constants for Priority levels. Signed-off-by: Olusegun Akintayo <akintayo.segun@gmail.com> --------- Signed-off-by: Olusegun Akintayo <akintayo.segun@gmail.com>
2023-06-08 12:26:18 +02:00
'gas-details-item__gas-fee-warning':
estimateUsed === PriorityLevels.high ||
estimateUsed === PriorityLevels.dappSuggestedHigh,
})}
>
2022-01-26 19:18:43 +01:00
<LoadingHeartBeat estimateUsed={estimateUsed} />
<Box marginRight={1}>
<strong>
When gas fees suggested by dapp is too high, show warning color and icon (#19088) * When gas fees suggested by dapp is too high, show warning color and icon Signed-off-by: Olusegun Akintayo <akintayo.segun@gmail.com> tests Signed-off-by: Olusegun Akintayo <akintayo.segun@gmail.com> Fix tests Signed-off-by: Olusegun Akintayo <akintayo.segun@gmail.com> set a default for high gas fees Signed-off-by: Olusegun Akintayo <akintayo.segun@gmail.com> Fix test cases where transaction is undefined. Signed-off-by: Olusegun Akintayo <akintayo.segun@gmail.com> Fix locale error Signed-off-by: Olusegun Akintayo <akintayo.segun@gmail.com> Fix error where dappSuggestedGasFees is null Signed-off-by: Olusegun Akintayo <akintayo.segun@gmail.com> Fix icon for site suggested Signed-off-by: Olusegun Akintayo <akintayo.segun@gmail.com> Fix unit tests snapshot Signed-off-by: Olusegun Akintayo <akintayo.segun@gmail.com> * Fix QA Comments Signed-off-by: Olusegun Akintayo <akintayo.segun@gmail.com> * lint:fix Signed-off-by: Olusegun Akintayo <akintayo.segun@gmail.com> * Fix unit tests Signed-off-by: Olusegun Akintayo <akintayo.segun@gmail.com> * Fix PR comments Signed-off-by: Olusegun Akintayo <akintayo.segun@gmail.com> * Lint fix Signed-off-by: Olusegun Akintayo <akintayo.segun@gmail.com> * Fix PR comment. - call setEstimateUsed only once. Signed-off-by: Olusegun Akintayo <akintayo.segun@gmail.com> * use constants for Priority levels. Signed-off-by: Olusegun Akintayo <akintayo.segun@gmail.com> --------- Signed-off-by: Olusegun Akintayo <akintayo.segun@gmail.com>
2023-06-08 12:26:18 +02:00
{(estimateUsed === PriorityLevels.high ||
estimateUsed === PriorityLevels.dappSuggestedHigh) &&
'⚠ '}
{t('editGasSubTextFeeLabel')}
</strong>
</Box>
<div
key="editGasSubTextFeeValue"
className="gas-details-item__currency-container"
>
2022-01-26 19:18:43 +01:00
<LoadingHeartBeat estimateUsed={estimateUsed} />
<UserPreferencedCurrencyDisplay
key="editGasSubTextFeeAmount"
type={PRIMARY}
value={
hexMaximumTransactionFee || draftHexMaximumTransactionFee
}
hideLabel={!useNativeCurrencyAsPrimaryCurrency}
/>
</div>
</Box>
</>
}
subTitle={
<GasTiming
maxPriorityFeePerGas={maxPriorityFeePerGasToRender}
maxFeePerGas={maxFeePerGasToRender}
/>
}
/>
);
};
GasDetailsItem.propTypes = {
'data-testid': PropTypes.string,
userAcknowledgedGasMissing: PropTypes.bool,
};
export default GasDetailsItem;