2022-07-27 17:11:48 +02:00
|
|
|
import React, { useState, useCallback } from 'react';
|
2022-02-18 17:48:38 +01:00
|
|
|
import PropTypes from 'prop-types';
|
2023-07-17 19:48:15 +02:00
|
|
|
import { useDispatch, useSelector } from 'react-redux';
|
2023-01-18 15:47:29 +01:00
|
|
|
import TransactionStatusLabel from '../transaction-status-label/transaction-status-label';
|
2022-02-18 17:48:38 +01:00
|
|
|
import TransactionIcon from '../transaction-icon';
|
|
|
|
import { useI18nContext } from '../../../hooks/useI18nContext';
|
2022-07-27 17:11:48 +02:00
|
|
|
import { useTransactionDisplayData } from '../../../hooks/useTransactionDisplayData';
|
2022-02-18 17:48:38 +01:00
|
|
|
import { formatDateWithYearContext } from '../../../helpers/utils/util';
|
|
|
|
import {
|
2023-01-18 15:47:29 +01:00
|
|
|
TransactionGroupCategory,
|
|
|
|
TransactionGroupStatus,
|
|
|
|
SmartTransactionStatus,
|
2022-02-18 17:48:38 +01:00
|
|
|
} from '../../../../shared/constants/transaction';
|
|
|
|
|
|
|
|
import CancelButton from '../cancel-button';
|
|
|
|
import { cancelSwapsSmartTransaction } from '../../../ducks/swaps/swaps';
|
2022-07-27 17:11:48 +02:00
|
|
|
import TransactionListItemDetails from '../transaction-list-item-details';
|
2023-07-17 19:48:15 +02:00
|
|
|
import { ActivityListItem } from '../../multichain';
|
|
|
|
import {
|
|
|
|
AvatarNetwork,
|
|
|
|
BadgeWrapper,
|
|
|
|
BadgeWrapperAnchorElementShape,
|
|
|
|
Box,
|
|
|
|
} from '../../component-library';
|
|
|
|
import {
|
|
|
|
BackgroundColor,
|
|
|
|
Display,
|
|
|
|
Size,
|
|
|
|
} from '../../../helpers/constants/design-system';
|
|
|
|
import { getCurrentNetwork } from '../../../selectors';
|
2022-02-18 17:48:38 +01:00
|
|
|
|
|
|
|
export default function SmartTransactionListItem({
|
|
|
|
smartTransaction,
|
2022-07-27 17:11:48 +02:00
|
|
|
transactionGroup,
|
2022-02-18 17:48:38 +01:00
|
|
|
isEarliestNonce = false,
|
|
|
|
}) {
|
|
|
|
const dispatch = useDispatch();
|
|
|
|
const t = useI18nContext();
|
|
|
|
const [cancelSwapLinkClicked, setCancelSwapLinkClicked] = useState(false);
|
2022-07-27 17:11:48 +02:00
|
|
|
const [showDetails, setShowDetails] = useState(false);
|
2022-07-31 20:26:40 +02:00
|
|
|
const { primaryCurrency, recipientAddress, isPending, senderAddress } =
|
|
|
|
useTransactionDisplayData(transactionGroup);
|
2023-07-17 19:48:15 +02:00
|
|
|
const currentChain = useSelector(getCurrentNetwork);
|
|
|
|
|
2022-07-31 20:26:40 +02:00
|
|
|
const { sourceTokenSymbol, destinationTokenSymbol, time, status } =
|
|
|
|
smartTransaction;
|
2023-01-18 15:47:29 +01:00
|
|
|
const category = TransactionGroupCategory.swap;
|
2022-02-18 17:48:38 +01:00
|
|
|
const title = t('swapTokenToToken', [
|
|
|
|
sourceTokenSymbol,
|
|
|
|
destinationTokenSymbol,
|
|
|
|
]);
|
2023-07-17 19:48:15 +02:00
|
|
|
const date = formatDateWithYearContext(time, 'MMM d, y', 'MMM d');
|
2022-02-18 17:48:38 +01:00
|
|
|
let displayedStatusKey;
|
2023-01-18 15:47:29 +01:00
|
|
|
if (status === SmartTransactionStatus.pending) {
|
|
|
|
displayedStatusKey = TransactionGroupStatus.pending;
|
|
|
|
} else if (status?.startsWith(SmartTransactionStatus.cancelled)) {
|
|
|
|
displayedStatusKey = TransactionGroupStatus.cancelled;
|
2022-02-18 17:48:38 +01:00
|
|
|
}
|
|
|
|
const showCancelSwapLink =
|
|
|
|
smartTransaction.cancellable && !cancelSwapLinkClicked;
|
|
|
|
const className = 'transaction-list-item transaction-list-item--unconfirmed';
|
2022-07-27 17:11:48 +02:00
|
|
|
const toggleShowDetails = useCallback(() => {
|
|
|
|
setShowDetails((prev) => !prev);
|
|
|
|
}, []);
|
2022-02-18 17:48:38 +01:00
|
|
|
return (
|
|
|
|
<>
|
2023-07-17 19:48:15 +02:00
|
|
|
<ActivityListItem
|
2022-02-18 17:48:38 +01:00
|
|
|
className={className}
|
|
|
|
title={title}
|
2022-07-27 17:11:48 +02:00
|
|
|
onClick={toggleShowDetails}
|
2022-02-18 17:48:38 +01:00
|
|
|
icon={
|
2023-07-17 19:48:15 +02:00
|
|
|
<BadgeWrapper
|
|
|
|
anchorElementShape={BadgeWrapperAnchorElementShape.circular}
|
|
|
|
positionObj={{ top: -4, right: -4 }}
|
|
|
|
display={Display.Block}
|
|
|
|
badge={
|
|
|
|
<AvatarNetwork
|
|
|
|
className="activity-tx__network-badge"
|
|
|
|
data-testid="activity-tx-network-badge"
|
|
|
|
size={Size.XS}
|
|
|
|
name={currentChain?.nickname}
|
|
|
|
src={currentChain?.rpcPrefs?.imageUrl}
|
|
|
|
borderWidth={1}
|
|
|
|
borderColor={BackgroundColor.backgroundDefault}
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<TransactionIcon category={category} status={displayedStatusKey} />
|
|
|
|
</BadgeWrapper>
|
2022-02-18 17:48:38 +01:00
|
|
|
}
|
|
|
|
subtitle={
|
2023-07-17 19:48:15 +02:00
|
|
|
<TransactionStatusLabel
|
|
|
|
isPending
|
|
|
|
isEarliestNonce={isEarliestNonce}
|
|
|
|
date={date}
|
|
|
|
status={displayedStatusKey}
|
|
|
|
/>
|
2022-02-18 17:48:38 +01:00
|
|
|
}
|
|
|
|
>
|
2023-01-18 15:47:29 +01:00
|
|
|
{displayedStatusKey === TransactionGroupStatus.pending &&
|
2022-02-18 17:48:38 +01:00
|
|
|
showCancelSwapLink && (
|
2023-07-17 19:48:15 +02:00
|
|
|
<Box
|
|
|
|
paddingTop={4}
|
|
|
|
className="transaction-list-item__pending-actions"
|
|
|
|
>
|
2022-02-18 17:48:38 +01:00
|
|
|
<CancelButton
|
|
|
|
transaction={smartTransaction.uuid}
|
|
|
|
cancelTransaction={(e) => {
|
|
|
|
e?.preventDefault();
|
|
|
|
dispatch(cancelSwapsSmartTransaction(smartTransaction.uuid));
|
|
|
|
setCancelSwapLinkClicked(true);
|
|
|
|
}}
|
|
|
|
/>
|
2023-07-17 19:48:15 +02:00
|
|
|
</Box>
|
2022-02-18 17:48:38 +01:00
|
|
|
)}
|
2023-07-17 19:48:15 +02:00
|
|
|
</ActivityListItem>
|
2022-07-27 17:11:48 +02:00
|
|
|
{showDetails && (
|
|
|
|
<TransactionListItemDetails
|
|
|
|
title={title}
|
|
|
|
onClose={toggleShowDetails}
|
|
|
|
senderAddress={senderAddress}
|
|
|
|
recipientAddress={recipientAddress}
|
|
|
|
primaryCurrency={primaryCurrency}
|
|
|
|
isEarliestNonce={isEarliestNonce}
|
|
|
|
transactionGroup={transactionGroup}
|
|
|
|
transactionStatus={() => (
|
2023-01-18 15:47:29 +01:00
|
|
|
<TransactionStatusLabel
|
2022-07-27 17:11:48 +02:00
|
|
|
isPending={isPending}
|
|
|
|
isEarliestNonce={isEarliestNonce}
|
|
|
|
date={date}
|
|
|
|
status={displayedStatusKey}
|
|
|
|
statusOnly
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
)}
|
2022-02-18 17:48:38 +01:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
SmartTransactionListItem.propTypes = {
|
|
|
|
smartTransaction: PropTypes.object.isRequired,
|
|
|
|
isEarliestNonce: PropTypes.bool,
|
2022-07-27 17:11:48 +02:00
|
|
|
transactionGroup: PropTypes.object,
|
2022-02-18 17:48:38 +01:00
|
|
|
};
|