1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-21 08:53:20 +01:00
metamask-extension/ui/app/components/tx-list.js

148 lines
4.1 KiB
JavaScript
Raw Normal View History

2017-08-09 12:10:51 +02:00
const Component = require('react').Component
const PropTypes = require('prop-types')
const connect = require('react-redux').connect
2017-08-09 12:10:51 +02:00
const h = require('react-hyperscript')
const inherits = require('util').inherits
const prefixForNetwork = require('../../lib/etherscan-prefix-for-network')
2017-08-11 04:35:10 +02:00
const selectors = require('../selectors')
2017-08-30 12:33:00 +02:00
const TxListItem = require('./tx-list-item')
const ShiftListItem = require('./shift-list-item')
2017-11-02 03:30:33 +01:00
const { formatDate } = require('../util')
const { showConfTxPage } = require('../actions')
const classnames = require('classnames')
const { tokenInfoGetter } = require('../token-util')
2017-08-09 12:10:51 +02:00
TxList.contextTypes = {
t: PropTypes.func,
}
module.exports = connect(mapStateToProps, mapDispatchToProps)(TxList)
2017-08-09 12:10:51 +02:00
function mapStateToProps (state) {
2017-08-09 12:10:51 +02:00
return {
2017-08-11 04:35:10 +02:00
txsToRender: selectors.transactionsSelector(state),
conversionRate: selectors.conversionRateSelector(state),
2017-08-09 12:10:51 +02:00
}
}
function mapDispatchToProps (dispatch) {
return {
2017-11-02 13:15:59 +01:00
showConfTxPage: ({ id }) => dispatch(showConfTxPage({ id })),
}
}
2017-08-09 12:10:51 +02:00
inherits(TxList, Component)
function TxList () {
Component.call(this)
}
TxList.prototype.componentWillMount = function () {
this.tokenInfoGetter = tokenInfoGetter()
}
2017-08-09 12:10:51 +02:00
TxList.prototype.render = function () {
return h('div.flex-column', [
2017-09-07 20:38:21 +02:00
h('div.flex-row.tx-list-header-wrapper', [
h('div.flex-row.tx-list-header', [
h('div', this.context.t('transactions')),
2017-08-09 12:10:51 +02:00
]),
]),
h('div.flex-column.tx-list-container', {}, [
this.renderTransaction(),
]),
2017-08-09 12:10:51 +02:00
])
}
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
2017-09-29 17:40:57 +02:00
if (transaction.key === 'shapeshift') {
return h(ShiftListItem, { ...transaction, key: `shapeshift${index}` })
}
2017-08-11 04:35:10 +02:00
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,
2017-08-11 04:35:10 +02:00
}
2017-09-05 10:48:52 +02:00
const {
2017-09-06 09:33:39 +02:00
address,
2017-09-05 10:48:52 +02:00
transactionStatus,
transactionAmount,
dateString,
transactionId,
transactionHash,
transactionNetworkId,
transactionSubmittedTime,
2017-09-05 10:48:52 +02:00
} = props
const { showConfTxPage } = this.props
2017-09-05 10:48:52 +02:00
2017-08-30 12:33:00 +02:00
const opts = {
key: transactionId || transactionHash,
2017-09-14 10:09:57 +02:00
txParams: transaction.txParams,
isMsg: Boolean(transaction.msgParams),
2017-08-30 12:33:00 +02:00
transactionStatus,
transactionId,
2017-08-30 12:33:00 +02:00
dateString,
address,
transactionAmount,
transactionHash,
conversionRate,
tokenInfoGetter: this.tokenInfoGetter,
transactionSubmittedTime,
2017-08-30 12:33:00 +02:00
}
2017-08-09 12:10:51 +02:00
2017-11-02 13:15:59 +01:00
const isUnapproved = transactionStatus === 'unapproved'
if (isUnapproved) {
opts.onClick = () => showConfTxPage({ id: transactionId })
opts.transactionStatus = this.context.t('notStarted')
2017-09-07 20:38:21 +02:00
} else if (transactionHash) {
opts.onClick = () => this.view(transactionHash, transactionNetworkId)
2017-08-30 12:33:00 +02:00
}
opts.className = classnames('.tx-list-item', {
'.tx-list-pending-item-container': isUnapproved,
'.tx-list-clickable': Boolean(transactionHash) || isUnapproved,
})
2017-08-30 12:33:00 +02:00
return h(TxListItem, opts)
2017-08-09 12:10:51 +02:00
}
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}`
}