1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-26 12:29:06 +01:00
metamask-extension/ui/components/app/transaction-list-item/smart-transaction-list-item.component.js
vthomas13 4dcde1e216
Multichain Activity Screen (#19557)
* layout wip

* Icon changes, add badgewrapped icons to smart tx

* grouping by date wip

* typo fix

* group txs by date, button styling

* removing queue/history division, adding datestamp for pending tx, minor styling changes

* adding tests, updating snap

* font size fix

* e2e fixes

* Remove unnecessary tabIndex and keypress handler

* Fix typo for fontWeight

* Fix nesting warning by removing unnecessary Text

* Fix tests

* Fix import and exports

* Remove unused verbiage

* Update E2E selectors

* More E2E

* More E2Es

* More test fixes

* awaiting find instead of click

* adding regularDelayMs to flaky test

* removing delay

* increasing delay outside of wait

* adding back first-child to selector

* test fixes

* using datatestid for primary currency

* sorting date txgroups

* wip alignment for big numbers

* alignment issues fix

* lintfix

* adding tabindex, cursor pointer, updating snap

* unit test fix

* storybook additions

* snaphot update

* update snap

---------

Co-authored-by: David Walsh <davidwalsh83@gmail.com>
2023-07-17 13:48:15 -04:00

146 lines
4.9 KiB
JavaScript

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 (
<>
<ActivityListItem
className={className}
title={title}
onClick={toggleShowDetails}
icon={
<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>
}
subtitle={
<TransactionStatusLabel
isPending
isEarliestNonce={isEarliestNonce}
date={date}
status={displayedStatusKey}
/>
}
>
{displayedStatusKey === TransactionGroupStatus.pending &&
showCancelSwapLink && (
<Box
paddingTop={4}
className="transaction-list-item__pending-actions"
>
<CancelButton
transaction={smartTransaction.uuid}
cancelTransaction={(e) => {
e?.preventDefault();
dispatch(cancelSwapsSmartTransaction(smartTransaction.uuid));
setCancelSwapLinkClicked(true);
}}
/>
</Box>
)}
</ActivityListItem>
{showDetails && (
<TransactionListItemDetails
title={title}
onClose={toggleShowDetails}
senderAddress={senderAddress}
recipientAddress={recipientAddress}
primaryCurrency={primaryCurrency}
isEarliestNonce={isEarliestNonce}
transactionGroup={transactionGroup}
transactionStatus={() => (
<TransactionStatusLabel
isPending={isPending}
isEarliestNonce={isEarliestNonce}
date={date}
status={displayedStatusKey}
statusOnly
/>
)}
/>
)}
</>
);
}
SmartTransactionListItem.propTypes = {
smartTransaction: PropTypes.object.isRequired,
isEarliestNonce: PropTypes.bool,
transactionGroup: PropTypes.object,
};