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/approve-content-card/approve-content-card.js

339 lines
9.8 KiB
JavaScript
Raw Normal View History

2022-10-05 21:48:35 +02:00
import React, { useContext } from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import Box from '../../ui/box/box';
import Button from '../../ui/button';
import EditGasFeeButton from '../edit-gas-fee-button/edit-gas-fee-button';
import { Text } from '../../component-library';
2022-10-05 21:48:35 +02:00
import {
AlignItems,
2022-10-05 21:48:35 +02:00
BLOCK_SIZES,
DISPLAY,
FLEX_DIRECTION,
FONT_WEIGHT,
JustifyContent,
2022-10-05 21:48:35 +02:00
TEXT_ALIGN,
TextColor,
TextVariant,
2022-10-05 21:48:35 +02:00
} from '../../../helpers/constants/design-system';
import { I18nContext } from '../../../contexts/i18n';
2022-10-05 21:48:35 +02:00
import GasDetailsItem from '../gas-details-item/gas-details-item';
import MultiLayerFeeMessage from '../multilayer-fee-message/multi-layer-fee-message';
import { formatCurrency } from '../../../helpers/utils/confirm-tx.util';
export default function ApproveContentCard({
showHeader = true,
symbol,
title,
showEdit,
showAdvanceGasFeeOptions = false,
onEditClick,
footer,
noBorder,
supportsEIP1559,
2022-10-05 21:48:35 +02:00
renderTransactionDetailsContent,
renderDataContent,
isMultiLayerFeeNetwork,
ethTransactionTotal,
nativeCurrency,
fullTxData,
hexTransactionTotal,
fiatTransactionTotal,
currentCurrency,
isSetApproveForAll,
isApprovalOrRejection,
data,
userAcknowledgedGasMissing,
renderSimulationFailureWarning,
useCurrencyRateCheck,
2022-10-05 21:48:35 +02:00
}) {
const t = useContext(I18nContext);
return (
<Box
className={classnames({
'approve-content-card-container__card': !noBorder,
'approve-content-card-container__card--no-border': noBorder,
})}
>
{showHeader && (
<Box
display={DISPLAY.FLEX}
flexDirection={FLEX_DIRECTION.ROW}
alignItems={AlignItems.center}
justifyContent={JustifyContent.flexEnd}
2022-10-05 21:48:35 +02:00
className="approve-content-card-container__card-header"
>
{supportsEIP1559 && title === t('transactionFee') ? null : (
2022-10-05 21:48:35 +02:00
<>
<Box className="approve-content-card-container__card-header__symbol">
{symbol}
</Box>
<Box
marginLeft={4}
className="approve-content-card-container__card-header__title"
>
<Text variant={TextVariant.bodySmBold} as="h6">
2022-10-05 21:48:35 +02:00
{title}
</Text>
2022-10-05 21:48:35 +02:00
</Box>
</>
)}
{showEdit && (!showAdvanceGasFeeOptions || !supportsEIP1559) && (
2022-10-05 21:48:35 +02:00
<Box width={BLOCK_SIZES.ONE_SIXTH}>
<Button type="link" onClick={() => onEditClick()}>
<Text
variant={TextVariant.bodySm}
color={TextColor.primaryDefault}
as="h6"
2022-10-05 21:48:35 +02:00
>
{t('edit')}
</Text>
2022-10-05 21:48:35 +02:00
</Button>
</Box>
)}
{showEdit &&
showAdvanceGasFeeOptions &&
supportsEIP1559 &&
!renderSimulationFailureWarning && (
<EditGasFeeButton
userAcknowledgedGasMissing={userAcknowledgedGasMissing}
/>
)}
2022-10-05 21:48:35 +02:00
</Box>
)}
<Box
marginTop={1}
marginBottom={3}
className="approve-content-card-container__card-content"
>
{renderTransactionDetailsContent &&
(!isMultiLayerFeeNetwork &&
supportsEIP1559 &&
!renderSimulationFailureWarning ? (
<GasDetailsItem
userAcknowledgedGasMissing={userAcknowledgedGasMissing}
/>
2022-10-05 21:48:35 +02:00
) : (
<Box
display={DISPLAY.FLEX}
flexDirection={FLEX_DIRECTION.ROW}
justifyContent={JustifyContent.spaceBetween}
2022-10-05 21:48:35 +02:00
>
{isMultiLayerFeeNetwork ? (
<Box
display={DISPLAY.FLEX}
flexDirection={FLEX_DIRECTION.COLUMN}
className="approve-content-card-container__transaction-details-extra-content"
>
<Box
display={DISPLAY.FLEX}
justifyContent={JustifyContent.spaceBetween}
2022-10-05 21:48:35 +02:00
>
<Text
variant={TextVariant.bodySm}
2022-10-05 21:48:35 +02:00
fontWeight={FONT_WEIGHT.NORMAL}
color={TextColor.textMuted}
as="h6"
2022-10-05 21:48:35 +02:00
>
<span>{t('transactionDetailLayer2GasHeading')}</span>
{`${ethTransactionTotal} ${nativeCurrency}`}
</Text>
2022-10-05 21:48:35 +02:00
</Box>
<MultiLayerFeeMessage
transaction={fullTxData}
layer2fee={hexTransactionTotal}
nativeCurrency={nativeCurrency}
plainStyle
/>
</Box>
) : (
<>
<Box>
<Text
variant={TextVariant.bodySm}
color={TextColor.textAlternative}
as="h6"
2022-10-05 21:48:35 +02:00
>
{t('feeAssociatedRequest')}
</Text>
2022-10-05 21:48:35 +02:00
</Box>
<Box
display={DISPLAY.FLEX}
flexDirection={FLEX_DIRECTION.COLUMN}
alignItems={AlignItems.flexEnd}
2022-10-05 21:48:35 +02:00
textAlign={TEXT_ALIGN.RIGHT}
>
{useCurrencyRateCheck && (
<Box>
<Text
variant={TextVariant.headingSm}
fontWeight={FONT_WEIGHT.BOLD}
color={TextColor.TEXT_DEFAULT}
as="h4"
>
{formatCurrency(
fiatTransactionTotal,
currentCurrency,
)}
</Text>
</Box>
)}
2022-10-05 21:48:35 +02:00
<Box>
<Text
variant={TextVariant.bodySm}
2022-10-05 21:48:35 +02:00
fontWeight={FONT_WEIGHT.NORMAL}
color={TextColor.textMuted}
as="h6"
2022-10-05 21:48:35 +02:00
>
{`${ethTransactionTotal} ${nativeCurrency}`}
</Text>
2022-10-05 21:48:35 +02:00
</Box>
</Box>
</>
)}
</Box>
))}
{renderDataContent && (
<Box display={DISPLAY.FLEX} flexDirection={FLEX_DIRECTION.COLUMN}>
<Box>
<Text
variant={TextVariant.bodySm}
color={TextColor.textAlternative}
as="h6"
2022-10-05 21:48:35 +02:00
>
{isSetApproveForAll
? t('functionSetApprovalForAll')
: t('functionApprove')}
</Text>
2022-10-05 21:48:35 +02:00
</Box>
{isSetApproveForAll && isApprovalOrRejection !== undefined ? (
<Box>
<Text
variant={TextVariant.bodySm}
color={TextColor.textAlternative}
as="h6"
2022-10-05 21:48:35 +02:00
>
{`${t('parameters')}: ${isApprovalOrRejection}`}
</Text>
2022-10-05 21:48:35 +02:00
</Box>
) : null}
<Box
marginRight={4}
className="approve-content-card-container__data__data-block"
>
<Text
variant={TextVariant.bodySm}
color={TextColor.textAlternative}
as="h6"
2022-10-05 21:48:35 +02:00
>
{data}
</Text>
2022-10-05 21:48:35 +02:00
</Box>
</Box>
)}
</Box>
{footer}
</Box>
);
}
ApproveContentCard.propTypes = {
/**
* Whether to show header including icon, transaction fee text and edit button
*/
2022-10-05 21:48:35 +02:00
showHeader: PropTypes.bool,
/**
* Symbol icon
*/
2022-10-05 21:48:35 +02:00
symbol: PropTypes.node,
/**
* Title to be included in the header
*/
2022-10-05 21:48:35 +02:00
title: PropTypes.string,
/**
* Whether to show edit button or not
*/
2022-10-05 21:48:35 +02:00
showEdit: PropTypes.bool,
/**
* Whether to show advanced gas fee options or not
*/
2022-10-05 21:48:35 +02:00
showAdvanceGasFeeOptions: PropTypes.bool,
/**
* Should open customize gas modal when edit button is clicked
*/
2022-10-05 21:48:35 +02:00
onEditClick: PropTypes.func,
/**
* Footer to be shown
*/
2022-10-05 21:48:35 +02:00
footer: PropTypes.node,
/**
* Whether to include border-bottom or not
*/
2022-10-05 21:48:35 +02:00
noBorder: PropTypes.bool,
/**
* Is enhanced gas fee enabled or not
*/
supportsEIP1559: PropTypes.bool,
/**
* Whether to render transaction details content or not
*/
2022-10-05 21:48:35 +02:00
renderTransactionDetailsContent: PropTypes.bool,
/**
* Whether to render data content or not
*/
2022-10-05 21:48:35 +02:00
renderDataContent: PropTypes.bool,
/**
* Is multi-layer fee network or not
*/
2022-10-05 21:48:35 +02:00
isMultiLayerFeeNetwork: PropTypes.bool,
/**
* Total sum of the transaction in native currency
*/
2022-10-05 21:48:35 +02:00
ethTransactionTotal: PropTypes.string,
/**
* Current native currency
*/
2022-10-05 21:48:35 +02:00
nativeCurrency: PropTypes.string,
/**
* Current transaction
*/
2022-10-05 21:48:35 +02:00
fullTxData: PropTypes.object,
/**
* Total sum of the transaction converted to hex value
*/
2022-10-05 21:48:35 +02:00
hexTransactionTotal: PropTypes.string,
/**
* Total sum of the transaction in fiat currency
*/
2022-10-05 21:48:35 +02:00
fiatTransactionTotal: PropTypes.string,
/**
* Current fiat currency
*/
2022-10-05 21:48:35 +02:00
currentCurrency: PropTypes.string,
/**
* Is set approve for all or not
*/
2022-10-05 21:48:35 +02:00
isSetApproveForAll: PropTypes.bool,
/**
* Whether a current set approval for all transaction will approve or revoke access
*/
2022-10-05 21:48:35 +02:00
isApprovalOrRejection: PropTypes.bool,
/**
* Current transaction data
*/
2022-10-05 21:48:35 +02:00
data: PropTypes.string,
/**
* User acknowledge gas is missing or not
*/
userAcknowledgedGasMissing: PropTypes.bool,
/**
* Render simulation failure warning
*/
renderSimulationFailureWarning: PropTypes.bool,
/**
* Fiat conversion control
*/
useCurrencyRateCheck: PropTypes.bool,
2022-10-05 21:48:35 +02:00
};