import React, { useState, useCallback } from 'react'; import PropTypes from 'prop-types'; import { useDispatch, useSelector } from 'react-redux'; import TransactionStatusLabel from '../transaction-status-label/transaction-status-label'; import TransactionIcon from '../transaction-icon'; import { useI18nContext } from '../../../hooks/useI18nContext'; import { useTransactionDisplayData } from '../../../hooks/useTransactionDisplayData'; import { formatDateWithYearContext } from '../../../helpers/utils/util'; import { TransactionGroupCategory, TransactionGroupStatus, SmartTransactionStatus, } from '../../../../shared/constants/transaction'; import CancelButton from '../cancel-button'; import { cancelSwapsSmartTransaction } from '../../../ducks/swaps/swaps'; import TransactionListItemDetails from '../transaction-list-item-details'; 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'; export default function SmartTransactionListItem({ smartTransaction, transactionGroup, isEarliestNonce = false, }) { const dispatch = useDispatch(); const t = useI18nContext(); const [cancelSwapLinkClicked, setCancelSwapLinkClicked] = useState(false); const [showDetails, setShowDetails] = useState(false); const { primaryCurrency, recipientAddress, isPending, senderAddress } = useTransactionDisplayData(transactionGroup); const currentChain = useSelector(getCurrentNetwork); const { sourceTokenSymbol, destinationTokenSymbol, time, status } = smartTransaction; const category = TransactionGroupCategory.swap; const title = t('swapTokenToToken', [ sourceTokenSymbol, destinationTokenSymbol, ]); const date = formatDateWithYearContext(time, 'MMM d, y', 'MMM d'); let displayedStatusKey; if (status === SmartTransactionStatus.pending) { displayedStatusKey = TransactionGroupStatus.pending; } else if (status?.startsWith(SmartTransactionStatus.cancelled)) { displayedStatusKey = TransactionGroupStatus.cancelled; } const showCancelSwapLink = smartTransaction.cancellable && !cancelSwapLinkClicked; const className = 'transaction-list-item transaction-list-item--unconfirmed'; const toggleShowDetails = useCallback(() => { setShowDetails((prev) => !prev); }, []); return ( <> } > } subtitle={ } > {displayedStatusKey === TransactionGroupStatus.pending && showCancelSwapLink && ( { e?.preventDefault(); dispatch(cancelSwapsSmartTransaction(smartTransaction.uuid)); setCancelSwapLinkClicked(true); }} /> )} {showDetails && ( ( )} /> )} ); } SmartTransactionListItem.propTypes = { smartTransaction: PropTypes.object.isRequired, isEarliestNonce: PropTypes.bool, transactionGroup: PropTypes.object, };