All files / app/components/home/transaction-list transaction-list-item.tsx

0% Statements 0/78
0% Branches 0/93
0% Functions 0/9
0% Lines 0/72

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191                                                                                                                                                                                                                                                                                                                                                                                             
/* eslint-disable @typescript-eslint/no-unsafe-argument */
import { TransactionIcon, TransactionIconVariants } from './transaction-icon';
import {
  createTxListContextMenu,
  registerHandler,
  deregisterHandler,
} from './transaction-list-context-menu';
import { TransactionListItemContainer } from './transaction-list-item-container';
import { AddressTransactionWithTransfers } from '@stacks/stacks-blockchain-api-types';
import { Box, color, Stack, Text } from '@stacks/ui';
import { getContractName, getTxTypeName } from '@stacks/ui-utils';
import { RootState } from '@store/index';
import { selectPoxInfo } from '@store/stacking';
import { capitalize } from '@utils/capitalize';
import { makeExplorerTxLink } from '@utils/external-links';
import {
  getRecipientAddress,
  isStackingTx,
  isDelegateStxTx,
  isRevokingDelegationTx,
  isDelegatedStackingTx,
  inferSendManyTransferOperation,
  isSendManyTx,
  getMemoForTx,
} from '@utils/tx-utils';
import { toHumanReadableStx } from '@utils/unit-convert';
import React, {
  FC,
  MutableRefObject,
  useLayoutEffect,
  useRef,
  useEffect,
  useCallback,
} from 'react';
import { useSelector } from 'react-redux';
import { useHover, useFocus } from 'use-events';
 
interface TransactionListItemProps {
  txWithEvents: AddressTransactionWithTransfers;
  activeTxIdRef: MutableRefObject<any>;
  domNodeMapRef: MutableRefObject<any>;
  onSelectTx(txId: string): void;
}
 
export const TransactionListItem: FC<TransactionListItemProps> = props => {
  const { txWithEvents, onSelectTx, activeTxIdRef, domNodeMapRef } = props;
  const { poxInfo } = useSelector((state: RootState) => ({
    poxInfo: selectPoxInfo(state),
  }));
 
  const tx = txWithEvents.tx;
 
  const { direction, amount } = inferSendManyTransferOperation(
    txWithEvents.stx_sent,
    txWithEvents.stx_received
  );
 
  const inMicroblock = (tx as any).is_unanchored;
 
  const txFailed =
    tx.tx_status === 'abort_by_response' || tx.tx_status === 'abort_by_post_condition';
 
  const getTxIconVariant = useCallback((): TransactionIconVariants => {
    Iif (txFailed) {
      return 'failed';
    }
    Iif (tx.tx_type === 'token_transfer' || isSendManyTx(tx)) {
      return direction;
    }
    Iif (isStackingTx(tx, poxInfo?.contract_id) || isDelegatedStackingTx(tx, poxInfo?.contract_id)) {
      return 'locked';
    }
    Iif (isDelegateStxTx(tx, poxInfo?.contract_id)) {
      return 'delegated';
    }
    Iif (isRevokingDelegationTx(tx, poxInfo?.contract_id)) {
      return 'revoked';
    }
    return 'default';
  }, [direction, poxInfo?.contract_id, tx, txFailed]);
 
  const transactionTitle = useCallback(() => {
    Iif (tx.tx_type === 'token_transfer') return capitalize(direction);
    Iif (isStackingTx(tx, poxInfo?.contract_id)) {
      Iif (tx.tx_status === 'abort_by_response' || tx.tx_status === 'abort_by_post_condition')
        return 'Stacking initiation failed';
      return 'Stacking initiated successfully';
    }
    Iif (isDelegatedStackingTx(tx, poxInfo?.contract_id)) {
      return 'Pool Stacked STX';
    }
    Iif (isDelegateStxTx(tx, poxInfo?.contract_id)) {
      Iif (tx.tx_status === 'abort_by_response' || tx.tx_status === 'abort_by_post_condition')
        return 'Failed to delegate STX';
      return 'Delegated STX';
    }
    Iif (isRevokingDelegationTx(tx, poxInfo?.contract_id)) {
      Iif (tx.tx_status === 'abort_by_response' || tx.tx_status === 'abort_by_post_condition')
        return 'Failed to revoke delegation';
      return 'Revoked STX';
    }
    Iif (tx.tx_type === 'smart_contract') {
      return getContractName(tx.smart_contract.contract_id);
    }
    Iif (isSendManyTx(tx)) return capitalize(direction);
    return capitalize(tx.tx_type).replace('_', ' ');
  }, [direction, poxInfo?.contract_id, tx]);
 
  const sumPrefix = direction === 'sent' && !isStackingTx(tx, poxInfo?.contract_id) ? '−' : '';
  const memo = getMemoForTx(tx, direction);
 
  const txDate = new Date(tx.burn_block_time_iso || (tx as any).parent_burn_block_time_iso);
  const txDateShort = txDate.toLocaleString();
 
  const containerRef = useRef<HTMLButtonElement>(null);
  const [hovered, bindHover] = useHover();
  const [focused, bindFocus] = useFocus();
 
  useEffect(() => {
    Iif (containerRef.current !== null && domNodeMapRef !== null) {
      domNodeMapRef.current[tx.tx_id] = containerRef.current;
    }
  }, [domNodeMapRef, tx.tx_id]);
 
  Iif (focused && activeTxIdRef !== null) {
    activeTxIdRef.current = tx.tx_id;
  }
 
  const { current: copy } = useRef({
    txid: tx.tx_id,
    recipientAddress: getRecipientAddress(tx) || '',
    memo: memo || '',
    date: txDate instanceof Date ? txDate.toISOString() : '',
    txDetails: JSON.stringify(tx, null, 2),
    explorerLink: makeExplorerTxLink(tx.tx_id),
  });
 
  useLayoutEffect(() => {
    const el = containerRef.current;
    const contextMenuHandler = (event: Event) => createTxListContextMenu(event, { tx, copy });
    registerHandler(el, contextMenuHandler);
    return () => deregisterHandler(el, contextMenuHandler);
  }, [tx, copy]);
 
  return (
    <TransactionListItemContainer
      ref={containerRef}
      onClick={() => onSelectTx(tx.tx_id)}
      focused={focused}
      hovered={hovered}
      txId={tx.tx_id}
      {...bindHover}
      {...bindFocus}
    >
      <TransactionIcon variant={getTxIconVariant()} mr="base-loose" />
      <Box flex={1}>
        <Text textStyle="body.large.medium" display="block">
          {transactionTitle()}
        </Text>
        <Stack isInline spacing="tight">
          <Text textStyle="body.small" color={color('text-caption')}>
            {getTxTypeName(tx)}
          </Text>
          <Text textStyle="body.small" color={color('text-caption')}>
            {inMicroblock ? 'In Microblock' : txDateShort}
          </Text>
        </Stack>
      </Box>
      <Box textAlign="right">
        <Text
          textStyle="body.large"
          color={color('text-title')}
          title={`Fee: ${toHumanReadableStx(tx.fee_rate)}`}
          display="block"
        >
          {txFailed ? (
            <Text mr="tight" color={color('feedback-error')} fontSize={0} fontWeight={500}>
              Failed
            </Text>
          ) : null}
          {sumPrefix}
          {toHumanReadableStx(amount)}
        </Text>
        <Text textStyle="body.small" color={color('text-caption')}>
          {memo}
        </Text>
      </Box>
    </TransactionListItemContainer>
  );
};