2021-02-04 19:15:23 +01:00
|
|
|
import React, { useMemo, useState, useCallback } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import classnames from 'classnames';
|
|
|
|
import { useHistory } from 'react-router-dom';
|
|
|
|
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';
|
|
|
|
import TransactionStatus from '../transaction-status/transaction-status.component';
|
|
|
|
import TransactionIcon from '../transaction-icon';
|
2020-11-03 23:57:51 +01:00
|
|
|
import {
|
|
|
|
TRANSACTION_GROUP_CATEGORIES,
|
|
|
|
TRANSACTION_STATUSES,
|
2021-04-28 21:53:59 +02:00
|
|
|
} from '../../../../shared/constants/transaction';
|
2021-07-14 18:45:37 +02:00
|
|
|
import { EDIT_GAS_MODES } from '../../../../shared/constants/gas';
|
2021-07-12 21:35:54 +02:00
|
|
|
import EditGasPopover from '../edit-gas-popover';
|
2021-09-15 17:59:51 +02:00
|
|
|
import { useMetricEvent } from '../../../hooks/useMetricEvent';
|
|
|
|
import Button from '../../ui/button';
|
|
|
|
import CancelButton from '../cancel-button';
|
2020-05-26 22:49:11 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export default function TransactionListItem({
|
|
|
|
transactionGroup,
|
|
|
|
isEarliestNonce = false,
|
|
|
|
}) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const t = useI18nContext();
|
|
|
|
const history = useHistory();
|
|
|
|
const { hasCancelled } = transactionGroup;
|
|
|
|
const [showDetails, setShowDetails] = useState(false);
|
2021-09-15 17:59:51 +02:00
|
|
|
const [showCancelEditGasPopover, setShowCancelEditGasPopover] = useState(
|
|
|
|
false,
|
|
|
|
);
|
|
|
|
const [showRetryEditGasPopover, setShowRetryEditGasPopover] = useState(false);
|
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
|
|
|
|
|
|
|
const speedUpMetricsEvent = useMetricEvent({
|
|
|
|
eventOpts: {
|
|
|
|
category: 'Navigation',
|
|
|
|
action: 'Activity Log',
|
|
|
|
name: 'Clicked "Speed Up"',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const cancelMetricsEvent = useMetricEvent({
|
|
|
|
eventOpts: {
|
|
|
|
category: 'Navigation',
|
|
|
|
action: 'Activity Log',
|
|
|
|
name: 'Clicked "Cancel"',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const retryTransaction = useCallback(
|
|
|
|
async (event) => {
|
|
|
|
event.stopPropagation();
|
|
|
|
setShowRetryEditGasPopover(true);
|
|
|
|
speedUpMetricsEvent();
|
|
|
|
},
|
|
|
|
[speedUpMetricsEvent],
|
|
|
|
);
|
|
|
|
|
|
|
|
const cancelTransaction = useCallback(
|
|
|
|
(event) => {
|
|
|
|
event.stopPropagation();
|
|
|
|
setShowCancelEditGasPopover(true);
|
|
|
|
cancelMetricsEvent();
|
|
|
|
},
|
|
|
|
[cancelMetricsEvent],
|
|
|
|
);
|
|
|
|
|
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
|
|
|
|
2020-11-03 23:57:51 +01:00
|
|
|
const isSignatureReq =
|
2021-02-04 19:15:23 +01:00
|
|
|
category === TRANSACTION_GROUP_CATEGORIES.SIGNATURE_REQUEST;
|
|
|
|
const isApproval = category === TRANSACTION_GROUP_CATEGORIES.APPROVAL;
|
|
|
|
const isUnapproved = status === TRANSACTION_STATUSES.UNAPPROVED;
|
|
|
|
const isSwap = category === TRANSACTION_GROUP_CATEGORIES.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
|
|
|
[
|
|
|
|
TRANSACTION_STATUSES.FAILED,
|
|
|
|
TRANSACTION_STATUSES.DROPPED,
|
|
|
|
TRANSACTION_STATUSES.REJECTED,
|
|
|
|
].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>
|
|
|
|
<TransactionStatus
|
|
|
|
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
|
|
|
/>
|
2020-11-03 00:41:28 +01:00
|
|
|
<span
|
|
|
|
className={
|
|
|
|
subtitleContainsOrigin
|
|
|
|
? 'transaction-list-item__origin'
|
|
|
|
: 'transaction-list-item__address'
|
|
|
|
}
|
|
|
|
title={subtitle}
|
|
|
|
>
|
2020-06-17 18:38:15 +02:00
|
|
|
{subtitle}
|
|
|
|
</span>
|
|
|
|
</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}
|
2020-11-03 23:57:51 +01:00
|
|
|
showRetry={status === TRANSACTION_STATUSES.FAILED && !isSwap}
|
2020-05-26 22:49:11 +02:00
|
|
|
showSpeedUp={shouldShowSpeedUp}
|
|
|
|
isEarliestNonce={isEarliestNonce}
|
|
|
|
onCancel={cancelTransaction}
|
|
|
|
showCancel={isPending && !hasCancelled}
|
|
|
|
/>
|
|
|
|
)}
|
2021-07-31 03:29:21 +02:00
|
|
|
{showRetryEditGasPopover && (
|
2021-07-12 21:35:54 +02:00
|
|
|
<EditGasPopover
|
2021-09-15 17:59:51 +02:00
|
|
|
onClose={() => setShowRetryEditGasPopover(false)}
|
2021-07-14 18:45:37 +02:00
|
|
|
mode={EDIT_GAS_MODES.SPEED_UP}
|
2021-09-15 17:59:51 +02:00
|
|
|
transaction={transactionGroup.primaryTransaction}
|
2021-07-12 21:35:54 +02:00
|
|
|
/>
|
|
|
|
)}
|
2021-07-31 03:29:21 +02:00
|
|
|
{showCancelEditGasPopover && (
|
2021-07-12 21:35:54 +02:00
|
|
|
<EditGasPopover
|
2021-09-15 17:59:51 +02:00
|
|
|
onClose={() => setShowCancelEditGasPopover(false)}
|
2021-07-14 18:45:37 +02:00
|
|
|
mode={EDIT_GAS_MODES.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
|
|
|
}
|
|
|
|
|
|
|
|
TransactionListItem.propTypes = {
|
|
|
|
transactionGroup: PropTypes.object.isRequired,
|
|
|
|
isEarliestNonce: PropTypes.bool,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|