1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-24 11:01:41 +01:00
metamask-extension/ui/components/app/transaction-list-item/transaction-list-item.component.js

239 lines
6.8 KiB
JavaScript
Raw Normal View History

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 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';
import {
TRANSACTION_GROUP_CATEGORIES,
TRANSACTION_STATUSES,
} from '../../../../shared/constants/transaction';
import { EDIT_GAS_MODES } from '../../../../shared/constants/gas';
import EditGasPopover from '../edit-gas-popover';
import { useMetricEvent } from '../../../hooks/useMetricEvent';
import Button from '../../ui/button';
import CancelButton from '../cancel-button';
2020-11-03 00:41:28 +01:00
export default function TransactionListItem({
transactionGroup,
isEarliestNonce = false,
}) {
const t = useI18nContext();
const history = useHistory();
const { hasCancelled } = transactionGroup;
const [showDetails, setShowDetails] = useState(false);
const [showCancelEditGasPopover, setShowCancelEditGasPopover] = useState(
false,
);
const [showRetryEditGasPopover, setShowRetryEditGasPopover] = useState(false);
2020-11-03 00:41:28 +01:00
const {
initialTransaction: { id },
primaryTransaction: { err, status },
} = transactionGroup;
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,
);
const {
title,
subtitle,
subtitleContainsOrigin,
date,
category,
primaryCurrency,
recipientAddress,
secondaryCurrency,
displayedStatusKey,
isPending,
senderAddress,
} = useTransactionDisplayData(transactionGroup);
const isSignatureReq =
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;
const className = classnames('transaction-list-item', {
2020-11-03 00:41:28 +01:00
'transaction-list-item--unconfirmed':
isPending ||
[
TRANSACTION_STATUSES.FAILED,
TRANSACTION_STATUSES.DROPPED,
TRANSACTION_STATUSES.REJECTED,
].includes(displayedStatusKey),
});
const toggleShowDetails = useCallback(() => {
if (isUnapproved) {
history.push(`${CONFIRM_TRANSACTION_ROUTE}/${id}`);
return;
}
setShowDetails((prev) => !prev);
}, [isUnapproved, history, id]);
const speedUpButton = useMemo(() => {
if (!shouldShowSpeedUp || !isPending || isUnapproved) {
return null;
}
return (
<Button
type="primary"
onClick={hasCancelled ? cancelTransaction : retryTransaction}
style={hasCancelled ? { width: 'auto' } : null}
>
{hasCancelled ? t('speedUpCancellation') : t('speedUp')}
</Button>
);
}, [
shouldShowSpeedUp,
isUnapproved,
t,
isPending,
hasCancelled,
retryTransaction,
cancelTransaction,
]);
const showCancelButton = !hasCancelled && isPending && !isUnapproved;
return (
<>
<ListItem
onClick={toggleShowDetails}
className={className}
title={title}
2020-11-03 00:41:28 +01:00
icon={
<TransactionIcon category={category} status={displayedStatusKey} />
}
subtitle={
<h3>
<TransactionStatus
isPending={isPending}
isEarliestNonce={isEarliestNonce}
error={err}
date={date}
status={displayedStatusKey}
/>
2020-11-03 00:41:28 +01:00
<span
className={
subtitleContainsOrigin
? 'transaction-list-item__origin'
: 'transaction-list-item__address'
}
title={subtitle}
>
{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>
</>
)
}
>
<div className="transaction-list-item__pending-actions">
{speedUpButton}
{showCancelButton && (
<CancelButton
transaction={transactionGroup.primaryTransaction}
cancelTransaction={cancelTransaction}
/>
)}
</div>
</ListItem>
{showDetails && (
<TransactionListItemDetails
title={title}
onClose={toggleShowDetails}
transactionGroup={transactionGroup}
primaryCurrency={primaryCurrency}
senderAddress={senderAddress}
recipientAddress={recipientAddress}
onRetry={retryTransaction}
showRetry={status === TRANSACTION_STATUSES.FAILED && !isSwap}
showSpeedUp={shouldShowSpeedUp}
isEarliestNonce={isEarliestNonce}
onCancel={cancelTransaction}
showCancel={isPending && !hasCancelled}
/>
)}
{showRetryEditGasPopover && (
<EditGasPopover
onClose={() => setShowRetryEditGasPopover(false)}
mode={EDIT_GAS_MODES.SPEED_UP}
transaction={transactionGroup.primaryTransaction}
/>
)}
{showCancelEditGasPopover && (
<EditGasPopover
onClose={() => setShowCancelEditGasPopover(false)}
mode={EDIT_GAS_MODES.CANCEL}
transaction={transactionGroup.primaryTransaction}
/>
)}
</>
);
}
TransactionListItem.propTypes = {
transactionGroup: PropTypes.object.isRequired,
isEarliestNonce: PropTypes.bool,
};