import React, { PureComponent } from 'react'; import PropTypes from 'prop-types'; import classnames from 'classnames'; import { getEthConversionFromWeiHex, getValueFromWeiHex, } from '../../../helpers/utils/conversions.util'; import { formatDate } from '../../../helpers/utils/util'; import { getBlockExplorerUrlForTx } from '../../../../shared/modules/transaction.utils'; import TransactionActivityLogIcon from './transaction-activity-log-icon'; import { CONFIRMED_STATUS } from './transaction-activity-log.constants'; export default class TransactionActivityLog extends PureComponent { static contextTypes = { t: PropTypes.func, metricEvent: PropTypes.func, }; static propTypes = { activities: PropTypes.array, className: PropTypes.string, conversionRate: PropTypes.number, inlineRetryIndex: PropTypes.number, inlineCancelIndex: PropTypes.number, nativeCurrency: PropTypes.string, onCancel: PropTypes.func, onRetry: PropTypes.func, primaryTransaction: PropTypes.object, isEarliestNonce: PropTypes.bool, rpcPrefs: PropTypes.object, }; handleActivityClick = (activity) => { const etherscanUrl = getBlockExplorerUrlForTx( activity, this.props.rpcPrefs, ); global.platform.openTab({ url: etherscanUrl }); }; renderInlineRetry(index) { const { t } = this.context; const { inlineRetryIndex, primaryTransaction = {}, onRetry, isEarliestNonce, } = this.props; const { status } = primaryTransaction; return isEarliestNonce && status !== CONFIRMED_STATUS && index === inlineRetryIndex ? (
{t('speedUpTransaction')}
) : null; } renderInlineCancel(index) { const { t } = this.context; const { inlineCancelIndex, primaryTransaction = {}, onCancel, isEarliestNonce, } = this.props; const { status } = primaryTransaction; return isEarliestNonce && status !== CONFIRMED_STATUS && index === inlineCancelIndex ? (
{t('speedUpCancellation')}
) : null; } renderActivity(activity, index) { const { conversionRate, nativeCurrency } = this.props; const { eventKey, value, timestamp } = activity; const ethValue = index === 0 ? `${getValueFromWeiHex({ value, fromCurrency: 'ETH', toCurrency: 'ETH', conversionRate, numberOfDecimals: 6, })} ${nativeCurrency}` : getEthConversionFromWeiHex({ value, fromCurrency: 'ETH', conversionRate, numberOfDecimals: 3, }); const formattedTimestamp = formatDate(timestamp, "T 'on' M/d/y"); const activityText = this.context.t(eventKey, [ ethValue, formattedTimestamp, ]); return (
this.handleActivityClick(activity)} > {activityText}
{this.renderInlineRetry(index)} {this.renderInlineCancel(index)}
); } render() { const { t } = this.context; const { className, activities } = this.props; if (activities.length === 0) { return null; } return (
{t('activityLog')}
{activities.map((activity, index) => this.renderActivity(activity, index), )}
); } }