aboutsummaryrefslogtreecommitdiffstats
path: root/ui/app/components/tx-list.js
blob: d8c4a9d1979345d1eeda8b49c7c6eafc2817ff67 (plain) (blame)
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
const Component = require('react').Component
const PropTypes = require('prop-types')
const connect = require('react-redux').connect
const h = require('react-hyperscript')
const inherits = require('util').inherits
const prefixForNetwork = require('../../lib/etherscan-prefix-for-network')
const selectors = require('../selectors')
const TxListItem = require('./tx-list-item')
const ShiftListItem = require('./shift-list-item')
const { formatDate } = require('../util')
const { showConfTxPage, updateNetworkNonce } = require('../actions')
const classnames = require('classnames')
const { tokenInfoGetter } = require('../token-util')
const { withRouter } = require('react-router-dom')
const { compose } = require('recompose')
const { CONFIRM_TRANSACTION_ROUTE } = require('../routes')

module.exports = compose(
  withRouter,
  connect(mapStateToProps, mapDispatchToProps)
)(TxList)

TxList.contextTypes = {
  t: PropTypes.func,
}

function mapStateToProps (state) {
  return {
    txsToRender: selectors.transactionsSelector(state),
    conversionRate: selectors.conversionRateSelector(state),
    selectedAddress: selectors.getSelectedAddress(state),
  }
}

function mapDispatchToProps (dispatch) {
  return {
    showConfTxPage: ({ id }) => dispatch(showConfTxPage({ id })),
    updateNetworkNonce: (address) => dispatch(updateNetworkNonce(address)),
  }
}

inherits(TxList, Component)
function TxList () {
  Component.call(this)
}

TxList.prototype.componentWillMount = function () {
  this.tokenInfoGetter = tokenInfoGetter()
  this.props.updateNetworkNonce(this.props.selectedAddress)
}

TxList.prototype.componentDidUpdate = function (prevProps) {
  const oldTxsToRender = prevProps.txsToRender
  const {
    txsToRender: newTxsToRender,
    selectedAddress,
    updateNetworkNonce,
  } = this.props

  if (newTxsToRender.length > oldTxsToRender.length) {
    updateNetworkNonce(selectedAddress)
  }
}

TxList.prototype.render = function () {
  return h('div.flex-column', [
    h('div.flex-row.tx-list-header-wrapper', [
      h('div.flex-row.tx-list-header', [
        h('div', this.context.t('transactions')),
      ]),
    ]),
    h('div.flex-column.tx-list-container', {}, [
      this.renderTransaction(),
    ]),
  ])
}

TxList.prototype.renderTransaction = function () {
  const { txsToRender, conversionRate } = this.props

  return txsToRender.length
    ? txsToRender.map((transaction, i) => this.renderTransactionListItem(transaction, conversionRate, i))
    : [h(
        'div.tx-list-item.tx-list-item--empty',
        { key: 'tx-list-none' },
        [ this.context.t('noTransactions') ],
      )]
}

// TODO: Consider moving TxListItem into a separate component
TxList.prototype.renderTransactionListItem = function (transaction, conversionRate, index) {
  // console.log({transaction})
  // refer to transaction-list.js:line 58

  if (transaction.key === 'shapeshift') {
    return h(ShiftListItem, { ...transaction, key: `shapeshift${index}` })
  }

  const props = {
    dateString: formatDate(transaction.time),
    address: transaction.txParams && transaction.txParams.to,
    transactionStatus: transaction.status,
    transactionAmount: transaction.txParams && transaction.txParams.value,
    transactionId: transaction.id,
    transactionHash: transaction.hash,
    transactionNetworkId: transaction.metamaskNetworkId,
    transactionSubmittedTime: transaction.submittedTime,
  }

  const {
    address,
    transactionStatus,
    transactionAmount,
    dateString,
    transactionId,
    transactionHash,
    transactionNetworkId,
    transactionSubmittedTime,
  } = props
  const { history } = this.props

  const opts = {
    key: transactionId || transactionHash,
    txParams: transaction.txParams,
    isMsg: Boolean(transaction.msgParams),
    transactionStatus,
    transactionId,
    dateString,
    address,
    transactionAmount,
    transactionHash,
    conversionRate,
    tokenInfoGetter: this.tokenInfoGetter,
    transactionSubmittedTime,
  }

  const isUnapproved = transactionStatus === 'unapproved'

  if (isUnapproved) {
    opts.onClick = () => {
      this.props.showConfTxPage({ id: transactionId })
      history.push(CONFIRM_TRANSACTION_ROUTE)
    }
    opts.transactionStatus = this.context.t('notStarted')
  } else if (transactionHash) {
    opts.onClick = () => this.view(transactionHash, transactionNetworkId)
  }

  opts.className = classnames('.tx-list-item', {
    '.tx-list-pending-item-container': isUnapproved,
    '.tx-list-clickable': Boolean(transactionHash) || isUnapproved,
  })

  return h(TxListItem, opts)
}

TxList.prototype.view = function (txHash, network) {
  const url = etherscanLinkFor(txHash, network)
  if (url) {
    navigateTo(url)
  }
}

function navigateTo (url) {
  global.platform.openWindow({ url })
}

function etherscanLinkFor (txHash, network) {
  const prefix = prefixForNetwork(network)
  return `https://${prefix}etherscan.io/tx/${txHash}`
}