2022-03-14 19:12:38 +01:00
|
|
|
import React, { useMemo, useState, useCallback, useContext } from 'react';
|
2021-02-04 19:15:23 +01:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import classnames from 'classnames';
|
|
|
|
import { useHistory } from 'react-router-dom';
|
2022-01-06 03:47:26 +01:00
|
|
|
import { useSelector } from 'react-redux';
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
import ListItem from '../../ui/list-item';
|
|
|
|
import { useTransactionDisplayData } from '../../../hooks/useTransactionDisplayData';
|
|
|
|
import { useI18nContext } from '../../../hooks/useI18nContext';
|
2021-09-15 17:59:51 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
import TransactionListItemDetails from '../transaction-list-item-details';
|
|
|
|
import { CONFIRM_TRANSACTION_ROUTE } from '../../../helpers/constants/routes';
|
|
|
|
import { useShouldShowSpeedUp } from '../../../hooks/useShouldShowSpeedUp';
|
2023-01-18 15:47:29 +01:00
|
|
|
import TransactionStatusLabel from '../transaction-status-label/transaction-status-label';
|
2021-02-04 19:15:23 +01:00
|
|
|
import TransactionIcon from '../transaction-icon';
|
2023-04-03 17:31:04 +02:00
|
|
|
import { MetaMetricsEventCategory } from '../../../../shared/constants/metametrics';
|
2020-11-03 23:57:51 +01:00
|
|
|
import {
|
2023-01-18 15:47:29 +01:00
|
|
|
TransactionGroupCategory,
|
|
|
|
TransactionStatus,
|
2021-04-28 21:53:59 +02:00
|
|
|
} from '../../../../shared/constants/transaction';
|
2023-01-27 19:28:03 +01:00
|
|
|
import { EditGasModes } from '../../../../shared/constants/gas';
|
2022-01-06 03:47:26 +01:00
|
|
|
import {
|
|
|
|
GasFeeContextProvider,
|
|
|
|
useGasFeeContext,
|
|
|
|
} from '../../../contexts/gasFee';
|
|
|
|
import {
|
|
|
|
TransactionModalContextProvider,
|
|
|
|
useTransactionModalContext,
|
|
|
|
} from '../../../contexts/transaction-modal';
|
2022-12-08 19:37:06 +01:00
|
|
|
import { checkNetworkAndAccountSupports1559 } from '../../../selectors';
|
2022-01-06 03:47:26 +01:00
|
|
|
import { isLegacyTransaction } from '../../../helpers/utils/transactions.util';
|
2021-09-15 17:59:51 +02:00
|
|
|
import Button from '../../ui/button';
|
2022-01-06 03:47:26 +01:00
|
|
|
import AdvancedGasFeePopover from '../advanced-gas-fee-popover';
|
2021-09-15 17:59:51 +02:00
|
|
|
import CancelButton from '../cancel-button';
|
2022-01-06 03:47:26 +01:00
|
|
|
import CancelSpeedupPopover from '../cancel-speedup-popover';
|
|
|
|
import EditGasFeePopover from '../edit-gas-fee-popover';
|
|
|
|
import EditGasPopover from '../edit-gas-popover';
|
2022-04-01 21:11:12 +02:00
|
|
|
import { MetaMetricsContext } from '../../../contexts/metametrics';
|
2022-05-20 16:35:18 +02:00
|
|
|
import SiteOrigin from '../../ui/site-origin';
|
2020-05-26 22:49:11 +02:00
|
|
|
|
2022-01-06 03:47:26 +01:00
|
|
|
function TransactionListItemInner({
|
2020-11-03 00:41:28 +01:00
|
|
|
transactionGroup,
|
2022-01-06 03:47:26 +01:00
|
|
|
setEditGasMode,
|
2020-11-03 00:41:28 +01:00
|
|
|
isEarliestNonce = false,
|
|
|
|
}) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const t = useI18nContext();
|
|
|
|
const history = useHistory();
|
|
|
|
const { hasCancelled } = transactionGroup;
|
|
|
|
const [showDetails, setShowDetails] = useState(false);
|
2022-07-31 20:26:40 +02:00
|
|
|
const [showCancelEditGasPopover, setShowCancelEditGasPopover] =
|
|
|
|
useState(false);
|
2021-09-15 17:59:51 +02:00
|
|
|
const [showRetryEditGasPopover, setShowRetryEditGasPopover] = useState(false);
|
2022-12-08 19:37:06 +01:00
|
|
|
const { supportsEIP1559 } = useGasFeeContext();
|
2022-01-06 03:47:26 +01:00
|
|
|
const { openModal } = useTransactionModalContext();
|
2020-05-26 22:49:11 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
const {
|
|
|
|
initialTransaction: { id },
|
2020-12-03 00:25:19 +01:00
|
|
|
primaryTransaction: { err, status },
|
2021-02-04 19:15:23 +01:00
|
|
|
} = transactionGroup;
|
2021-09-15 17:59:51 +02:00
|
|
|
|
2022-03-14 19:12:38 +01:00
|
|
|
const trackEvent = useContext(MetaMetricsContext);
|
2021-09-15 17:59:51 +02:00
|
|
|
|
|
|
|
const retryTransaction = useCallback(
|
|
|
|
async (event) => {
|
|
|
|
event.stopPropagation();
|
2022-03-14 19:12:38 +01:00
|
|
|
trackEvent({
|
|
|
|
event: 'Clicked "Speed Up"',
|
2023-04-03 17:31:04 +02:00
|
|
|
category: MetaMetricsEventCategory.Navigation,
|
2022-03-14 19:12:38 +01:00
|
|
|
properties: {
|
|
|
|
action: 'Activity Log',
|
|
|
|
legacy_event: true,
|
|
|
|
},
|
|
|
|
});
|
2022-12-08 19:37:06 +01:00
|
|
|
if (supportsEIP1559) {
|
2023-01-27 19:28:03 +01:00
|
|
|
setEditGasMode(EditGasModes.speedUp);
|
2022-01-06 03:47:26 +01:00
|
|
|
openModal('cancelSpeedUpTransaction');
|
|
|
|
} else {
|
|
|
|
setShowRetryEditGasPopover(true);
|
|
|
|
}
|
2021-09-15 17:59:51 +02:00
|
|
|
},
|
2022-12-08 19:37:06 +01:00
|
|
|
[openModal, setEditGasMode, trackEvent, supportsEIP1559],
|
2021-09-15 17:59:51 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
const cancelTransaction = useCallback(
|
|
|
|
(event) => {
|
|
|
|
event.stopPropagation();
|
2022-03-14 19:12:38 +01:00
|
|
|
trackEvent({
|
|
|
|
event: 'Clicked "Cancel"',
|
2023-04-03 17:31:04 +02:00
|
|
|
category: MetaMetricsEventCategory.Navigation,
|
2022-03-14 19:12:38 +01:00
|
|
|
properties: {
|
|
|
|
action: 'Activity Log',
|
|
|
|
legacy_event: true,
|
|
|
|
},
|
|
|
|
});
|
2022-12-08 19:37:06 +01:00
|
|
|
if (supportsEIP1559) {
|
2023-01-27 19:28:03 +01:00
|
|
|
setEditGasMode(EditGasModes.cancel);
|
2022-01-06 03:47:26 +01:00
|
|
|
openModal('cancelSpeedUpTransaction');
|
|
|
|
} else {
|
|
|
|
setShowCancelEditGasPopover(true);
|
|
|
|
}
|
2021-09-15 17:59:51 +02:00
|
|
|
},
|
2022-12-08 19:37:06 +01:00
|
|
|
[trackEvent, openModal, setEditGasMode, supportsEIP1559],
|
2021-09-15 17:59:51 +02:00
|
|
|
);
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
const shouldShowSpeedUp = useShouldShowSpeedUp(
|
|
|
|
transactionGroup,
|
|
|
|
isEarliestNonce,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-05-26 22:49:11 +02:00
|
|
|
|
|
|
|
const {
|
|
|
|
title,
|
|
|
|
subtitle,
|
2020-06-17 18:38:15 +02:00
|
|
|
subtitleContainsOrigin,
|
2020-05-26 22:49:11 +02:00
|
|
|
date,
|
|
|
|
category,
|
|
|
|
primaryCurrency,
|
|
|
|
recipientAddress,
|
|
|
|
secondaryCurrency,
|
2020-10-07 23:59:38 +02:00
|
|
|
displayedStatusKey,
|
2020-05-26 22:49:11 +02:00
|
|
|
isPending,
|
|
|
|
senderAddress,
|
2021-02-04 19:15:23 +01:00
|
|
|
} = useTransactionDisplayData(transactionGroup);
|
2020-05-26 22:49:11 +02:00
|
|
|
|
2023-01-18 15:47:29 +01:00
|
|
|
const isSignatureReq = category === TransactionGroupCategory.signatureRequest;
|
|
|
|
const isApproval = category === TransactionGroupCategory.approval;
|
|
|
|
const isUnapproved = status === TransactionStatus.unapproved;
|
|
|
|
const isSwap = category === TransactionGroupCategory.swap;
|
2020-05-26 22:49:11 +02:00
|
|
|
|
2020-06-10 22:38:34 +02:00
|
|
|
const className = classnames('transaction-list-item', {
|
2020-11-03 00:41:28 +01:00
|
|
|
'transaction-list-item--unconfirmed':
|
|
|
|
isPending ||
|
2020-11-03 23:57:51 +01:00
|
|
|
[
|
2023-01-18 15:47:29 +01:00
|
|
|
TransactionStatus.failed,
|
|
|
|
TransactionStatus.dropped,
|
|
|
|
TransactionStatus.rejected,
|
2020-11-03 23:57:51 +01:00
|
|
|
].includes(displayedStatusKey),
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-07-31 07:03:20 +02:00
|
|
|
|
2020-05-26 22:49:11 +02:00
|
|
|
const toggleShowDetails = useCallback(() => {
|
|
|
|
if (isUnapproved) {
|
2021-02-04 19:15:23 +01:00
|
|
|
history.push(`${CONFIRM_TRANSACTION_ROUTE}/${id}`);
|
|
|
|
return;
|
2018-07-31 07:03:20 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
setShowDetails((prev) => !prev);
|
|
|
|
}, [isUnapproved, history, id]);
|
2020-05-26 22:49:11 +02:00
|
|
|
|
|
|
|
const speedUpButton = useMemo(() => {
|
|
|
|
if (!shouldShowSpeedUp || !isPending || isUnapproved) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return null;
|
2018-08-03 05:20:15 +02:00
|
|
|
}
|
2018-07-31 07:03:20 +02:00
|
|
|
return (
|
2020-05-26 22:49:11 +02:00
|
|
|
<Button
|
2021-10-05 21:20:42 +02:00
|
|
|
type="primary"
|
2021-03-12 20:26:07 +01:00
|
|
|
onClick={hasCancelled ? cancelTransaction : retryTransaction}
|
|
|
|
style={hasCancelled ? { width: 'auto' } : null}
|
2020-05-26 22:49:11 +02:00
|
|
|
>
|
2021-03-12 20:26:07 +01:00
|
|
|
{hasCancelled ? t('speedUpCancellation') : t('speedUp')}
|
2020-05-26 22:49:11 +02:00
|
|
|
</Button>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2021-03-12 20:26:07 +01:00
|
|
|
}, [
|
|
|
|
shouldShowSpeedUp,
|
|
|
|
isUnapproved,
|
|
|
|
t,
|
|
|
|
isPending,
|
|
|
|
hasCancelled,
|
2021-09-15 17:59:51 +02:00
|
|
|
retryTransaction,
|
2021-03-12 20:26:07 +01:00
|
|
|
cancelTransaction,
|
|
|
|
]);
|
2020-05-26 22:49:11 +02:00
|
|
|
|
2021-09-15 17:59:51 +02:00
|
|
|
const showCancelButton = !hasCancelled && isPending && !isUnapproved;
|
|
|
|
|
2020-05-26 22:49:11 +02:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<ListItem
|
|
|
|
onClick={toggleShowDetails}
|
|
|
|
className={className}
|
|
|
|
title={title}
|
2020-11-03 00:41:28 +01:00
|
|
|
icon={
|
|
|
|
<TransactionIcon category={category} status={displayedStatusKey} />
|
|
|
|
}
|
|
|
|
subtitle={
|
2020-06-17 18:38:15 +02:00
|
|
|
<h3>
|
2023-01-18 15:47:29 +01:00
|
|
|
<TransactionStatusLabel
|
2020-06-17 18:38:15 +02:00
|
|
|
isPending={isPending}
|
|
|
|
isEarliestNonce={isEarliestNonce}
|
|
|
|
error={err}
|
|
|
|
date={date}
|
2020-10-07 23:59:38 +02:00
|
|
|
status={displayedStatusKey}
|
2020-06-17 18:38:15 +02:00
|
|
|
/>
|
2022-05-20 16:35:18 +02:00
|
|
|
{subtitleContainsOrigin ? (
|
|
|
|
<SiteOrigin siteOrigin={subtitle} />
|
|
|
|
) : (
|
|
|
|
<span className="transaction-list-item__address" title={subtitle}>
|
|
|
|
{subtitle}
|
|
|
|
</span>
|
|
|
|
)}
|
2020-06-17 18:38:15 +02:00
|
|
|
</h3>
|
2020-11-03 00:41:28 +01:00
|
|
|
}
|
|
|
|
rightContent={
|
|
|
|
!isSignatureReq &&
|
|
|
|
!isApproval && (
|
|
|
|
<>
|
|
|
|
<h2
|
|
|
|
title={primaryCurrency}
|
|
|
|
className="transaction-list-item__primary-currency"
|
|
|
|
>
|
|
|
|
{primaryCurrency}
|
|
|
|
</h2>
|
|
|
|
<h3 className="transaction-list-item__secondary-currency">
|
|
|
|
{secondaryCurrency}
|
|
|
|
</h3>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
2020-05-26 22:49:11 +02:00
|
|
|
>
|
|
|
|
<div className="transaction-list-item__pending-actions">
|
|
|
|
{speedUpButton}
|
2021-09-15 17:59:51 +02:00
|
|
|
{showCancelButton && (
|
|
|
|
<CancelButton
|
|
|
|
transaction={transactionGroup.primaryTransaction}
|
|
|
|
cancelTransaction={cancelTransaction}
|
|
|
|
/>
|
|
|
|
)}
|
2018-09-18 04:35:47 +02:00
|
|
|
</div>
|
2020-05-26 22:49:11 +02:00
|
|
|
</ListItem>
|
|
|
|
{showDetails && (
|
|
|
|
<TransactionListItemDetails
|
|
|
|
title={title}
|
|
|
|
onClose={toggleShowDetails}
|
|
|
|
transactionGroup={transactionGroup}
|
2020-10-09 22:11:39 +02:00
|
|
|
primaryCurrency={primaryCurrency}
|
2020-05-26 22:49:11 +02:00
|
|
|
senderAddress={senderAddress}
|
|
|
|
recipientAddress={recipientAddress}
|
|
|
|
onRetry={retryTransaction}
|
2023-01-18 15:47:29 +01:00
|
|
|
showRetry={status === TransactionStatus.failed && !isSwap}
|
2020-05-26 22:49:11 +02:00
|
|
|
showSpeedUp={shouldShowSpeedUp}
|
|
|
|
isEarliestNonce={isEarliestNonce}
|
|
|
|
onCancel={cancelTransaction}
|
|
|
|
showCancel={isPending && !hasCancelled}
|
2021-12-01 18:22:08 +01:00
|
|
|
transactionStatus={() => (
|
2023-01-18 15:47:29 +01:00
|
|
|
<TransactionStatusLabel
|
2021-12-01 18:22:08 +01:00
|
|
|
isPending={isPending}
|
|
|
|
isEarliestNonce={isEarliestNonce}
|
|
|
|
error={err}
|
|
|
|
date={date}
|
|
|
|
status={displayedStatusKey}
|
|
|
|
statusOnly
|
|
|
|
/>
|
|
|
|
)}
|
2020-05-26 22:49:11 +02:00
|
|
|
/>
|
|
|
|
)}
|
2022-12-08 19:37:06 +01:00
|
|
|
{!supportsEIP1559 && showRetryEditGasPopover && (
|
2021-07-12 21:35:54 +02:00
|
|
|
<EditGasPopover
|
2021-09-15 17:59:51 +02:00
|
|
|
onClose={() => setShowRetryEditGasPopover(false)}
|
2023-01-27 19:28:03 +01:00
|
|
|
mode={EditGasModes.speedUp}
|
2021-09-15 17:59:51 +02:00
|
|
|
transaction={transactionGroup.primaryTransaction}
|
2021-07-12 21:35:54 +02:00
|
|
|
/>
|
|
|
|
)}
|
2022-12-08 19:37:06 +01:00
|
|
|
{!supportsEIP1559 && showCancelEditGasPopover && (
|
2021-07-12 21:35:54 +02:00
|
|
|
<EditGasPopover
|
2021-09-15 17:59:51 +02:00
|
|
|
onClose={() => setShowCancelEditGasPopover(false)}
|
2023-01-27 19:28:03 +01:00
|
|
|
mode={EditGasModes.cancel}
|
2021-09-15 17:59:51 +02:00
|
|
|
transaction={transactionGroup.primaryTransaction}
|
2021-07-12 21:35:54 +02:00
|
|
|
/>
|
|
|
|
)}
|
2020-05-26 22:49:11 +02:00
|
|
|
</>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-05-26 22:49:11 +02:00
|
|
|
}
|
|
|
|
|
2022-01-06 03:47:26 +01:00
|
|
|
TransactionListItemInner.propTypes = {
|
2020-05-26 22:49:11 +02:00
|
|
|
transactionGroup: PropTypes.object.isRequired,
|
|
|
|
isEarliestNonce: PropTypes.bool,
|
2022-01-06 03:47:26 +01:00
|
|
|
setEditGasMode: PropTypes.func,
|
|
|
|
};
|
|
|
|
|
|
|
|
const TransactionListItem = (props) => {
|
|
|
|
const { transactionGroup } = props;
|
|
|
|
const [editGasMode, setEditGasMode] = useState();
|
|
|
|
const transaction = transactionGroup.primaryTransaction;
|
|
|
|
|
|
|
|
const supportsEIP1559 =
|
|
|
|
useSelector(checkNetworkAndAccountSupports1559) &&
|
|
|
|
!isLegacyTransaction(transaction?.txParams);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<GasFeeContextProvider
|
|
|
|
transaction={transactionGroup.primaryTransaction}
|
|
|
|
editGasMode={editGasMode}
|
|
|
|
>
|
2022-02-01 18:53:03 +01:00
|
|
|
<TransactionModalContextProvider>
|
2022-01-06 03:47:26 +01:00
|
|
|
<TransactionListItemInner {...props} setEditGasMode={setEditGasMode} />
|
2022-12-08 19:37:06 +01:00
|
|
|
{supportsEIP1559 && (
|
2022-01-06 03:47:26 +01:00
|
|
|
<>
|
|
|
|
<CancelSpeedupPopover />
|
|
|
|
<EditGasFeePopover />
|
|
|
|
<AdvancedGasFeePopover />
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</TransactionModalContextProvider>
|
|
|
|
</GasFeeContextProvider>
|
|
|
|
);
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2022-01-06 03:47:26 +01:00
|
|
|
|
|
|
|
TransactionListItem.propTypes = {
|
|
|
|
transactionGroup: PropTypes.object.isRequired,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default TransactionListItem;
|