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';
|
|
|
|
import { useCancelTransaction } from '../../../hooks/useCancelTransaction';
|
|
|
|
import { useRetryTransaction } from '../../../hooks/useRetryTransaction';
|
|
|
|
import Button from '../../ui/button';
|
|
|
|
import Tooltip from '../../ui/tooltip';
|
|
|
|
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';
|
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);
|
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;
|
2020-11-03 00:41:28 +01:00
|
|
|
const [cancelEnabled, cancelTransaction] = useCancelTransaction(
|
|
|
|
transactionGroup,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
const retryTransaction = useRetryTransaction(transactionGroup);
|
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 cancelButton = useMemo(() => {
|
2020-08-18 18:36:45 +02:00
|
|
|
const btn = (
|
2020-05-26 22:49:11 +02:00
|
|
|
<Button
|
|
|
|
onClick={cancelTransaction}
|
|
|
|
rounded
|
|
|
|
className="transaction-list-item__header-button"
|
|
|
|
disabled={!cancelEnabled}
|
|
|
|
>
|
2020-11-03 00:41:28 +01:00
|
|
|
{t('cancel')}
|
2020-05-26 22:49:11 +02:00
|
|
|
</Button>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-05-26 22:49:11 +02:00
|
|
|
if (hasCancelled || !isPending || isUnapproved) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return null;
|
2019-03-05 16:45:01 +01:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
return cancelEnabled ? (
|
|
|
|
btn
|
|
|
|
) : (
|
|
|
|
<Tooltip title={t('notEnoughGas')} position="bottom">
|
|
|
|
<div>{btn}</div>
|
|
|
|
</Tooltip>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-11-03 00:41:28 +01:00
|
|
|
}, [
|
|
|
|
isPending,
|
|
|
|
t,
|
|
|
|
isUnapproved,
|
|
|
|
cancelEnabled,
|
|
|
|
cancelTransaction,
|
|
|
|
hasCancelled,
|
2021-02-04 19:15:23 +01:00
|
|
|
]);
|
2018-08-03 05:20:15 +02:00
|
|
|
|
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
|
|
|
|
type="secondary"
|
|
|
|
rounded
|
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,
|
|
|
|
retryTransaction,
|
|
|
|
hasCancelled,
|
|
|
|
cancelTransaction,
|
|
|
|
]);
|
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}
|
|
|
|
{cancelButton}
|
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}
|
|
|
|
cancelDisabled={!cancelEnabled}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</>
|
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
|
|
|
};
|