1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-02 22:24:27 +01:00
metamask-extension/ui/app/components/transaction-list-item.js

239 lines
6.3 KiB
JavaScript
Raw Normal View History

const Component = require('react').Component
const h = require('react-hyperscript')
const inherits = require('util').inherits
2017-12-07 04:48:26 +01:00
const connect = require('react-redux').connect
const EthBalance = require('./eth-balance')
const addressSummary = require('../util').addressSummary
2017-10-14 17:23:44 +02:00
const explorerLink = require('etherscan-link').createExplorerLink
2016-06-29 23:12:58 +02:00
const CopyButton = require('./copyButton')
2017-08-03 23:48:19 +02:00
const vreme = new (require('vreme'))()
const Tooltip = require('./tooltip')
2017-05-23 00:43:20 +02:00
const numberToBN = require('number-to-bn')
2017-12-07 04:20:11 +01:00
const actions = require('../actions')
const TransactionIcon = require('./transaction-list-item-icon')
2016-08-18 19:40:35 +02:00
const ShiftListItem = require('./shift-list-item')
2017-12-07 04:48:26 +01:00
const mapDispatchToProps = dispatch => {
return {
retryTransaction: transactionId => dispatch(actions.retryTransaction(transactionId)),
}
}
module.exports = connect(null, mapDispatchToProps)(TransactionListItem)
inherits(TransactionListItem, Component)
2016-06-21 22:18:32 +02:00
function TransactionListItem () {
Component.call(this)
}
TransactionListItem.prototype.showRetryButton = function () {
const { transaction = {} } = this.props
const { status, time } = transaction
return status === 'submitted' && Date.now() - time > 30000
}
2016-06-21 22:18:32 +02:00
TransactionListItem.prototype.render = function () {
const { transaction, network, conversionRate, currentCurrency } = this.props
2017-12-07 04:20:11 +01:00
const { status } = transaction
2016-08-18 19:40:35 +02:00
if (transaction.key === 'shapeshift') {
if (network === '1') return h(ShiftListItem, transaction)
}
var date = formatDate(transaction.time)
let isLinkable = false
2016-05-27 00:00:37 +02:00
const numericNet = parseInt(network)
isLinkable = numericNet === 1 || numericNet === 3 || numericNet === 4 || numericNet === 42
var isMsg = ('msgParams' in transaction)
var isTx = ('txParams' in transaction)
2017-12-07 04:20:11 +01:00
var isPending = status === 'unapproved'
let txParams
if (isTx) {
txParams = transaction.txParams
} else if (isMsg) {
txParams = transaction.msgParams
}
2017-05-23 00:43:20 +02:00
const nonce = txParams.nonce ? numberToBN(txParams.nonce).toString(10) : ''
const isClickable = ('hash' in transaction && isLinkable) || isPending
return (
2017-12-07 04:22:40 +01:00
h('.transaction-list-item.flex-column', {
onClick: (event) => {
if (isPending) {
this.props.showTx(transaction.id)
}
event.stopPropagation()
if (!transaction.hash || !isLinkable) return
var url = explorerLink(transaction.hash, parseInt(network))
2017-04-05 07:43:55 +02:00
global.platform.openWindow({ url })
},
style: {
padding: '20px 0',
2017-12-07 04:22:40 +01:00
alignItems: 'center',
},
}, [
2017-12-07 04:22:40 +01:00
h(`.flex-row.flex-space-between${isClickable ? '.pointer' : ''}`, {
style: {
width: '100%',
},
}, [
h('.identicon-wrapper.flex-column.flex-center.select-none', [
h(TransactionIcon, { txParams, transaction, isTx, isMsg }),
]),
h(Tooltip, {
title: 'Transaction Number',
position: 'right',
}, [
h('span', {
style: {
display: 'flex',
cursor: 'normal',
flexDirection: 'column',
alignItems: 'center',
justifyContent: 'center',
padding: '10px',
},
}, nonce),
]),
h('.flex-column', {style: {width: '200px', overflow: 'hidden'}}, [
domainField(txParams),
h('div', date),
recipientField(txParams, transaction, isTx, isMsg),
]),
// Places a copy button if tx is successful, else places a placeholder empty div.
transaction.hash ? h(CopyButton, { value: transaction.hash }) : h('div', {style: { display: 'flex', alignItems: 'center', width: '26px' }}),
isTx ? h(EthBalance, {
value: txParams.value,
conversionRate,
currentCurrency,
width: '55px',
shorten: true,
showFiat: false,
style: {fontSize: '15px'},
}) : h('.flex-column'),
]),
this.showRetryButton() && h('.transition-list-item__retry.grow-on-hover', {
2017-12-07 04:22:40 +01:00
onClick: event => {
event.stopPropagation()
this.resubmit()
},
style: {
2017-12-07 05:27:43 +01:00
height: '22px',
borderRadius: '22px',
2017-12-07 04:22:40 +01:00
color: '#F9881B',
2017-12-07 05:27:43 +01:00
padding: '0 20px',
2017-12-07 04:22:40 +01:00
backgroundColor: '#FFE3C9',
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
2017-12-07 05:27:43 +01:00
fontSize: '8px',
2017-12-07 04:22:40 +01:00
cursor: 'pointer',
},
2017-05-21 23:18:23 +02:00
}, [
2017-12-07 04:22:40 +01:00
h('div', {
style: {
2017-12-07 04:22:40 +01:00
paddingRight: '2px',
},
2017-12-07 04:22:40 +01:00
}, 'Taking too long?'),
h('div', {
style: {
textDecoration: 'underline',
},
}, 'Retry with a higher gas price here'),
]),
])
)
}
2017-12-07 04:20:11 +01:00
TransactionListItem.prototype.resubmit = function () {
const { transaction } = this.props
2017-12-07 04:48:26 +01:00
this.props.retryTransaction(transaction.id)
2017-12-07 04:20:11 +01:00
}
2016-06-21 22:18:32 +02:00
function domainField (txParams) {
return h('div', {
style: {
2016-06-23 00:31:02 +02:00
fontSize: 'x-small',
color: '#ABA9AA',
overflow: 'hidden',
textOverflow: 'ellipsis',
width: '100%',
},
2016-06-21 22:18:32 +02:00
}, [
txParams.origin,
])
}
2016-06-21 22:18:32 +02:00
function recipientField (txParams, transaction, isTx, isMsg) {
let message
if (isMsg) {
message = 'Signature Requested'
} else if (txParams.to) {
2016-06-21 22:18:32 +02:00
message = addressSummary(txParams.to)
} else {
message = 'Contract Deployment'
}
return h('div', {
style: {
2016-06-23 00:31:02 +02:00
fontSize: 'x-small',
color: '#ABA9AA',
},
2016-06-21 22:18:32 +02:00
}, [
message,
renderErrorOrWarning(transaction),
])
}
2016-06-21 22:18:32 +02:00
function formatDate (date) {
return vreme.format(new Date(date), 'March 16 2014 14:30')
}
function renderErrorOrWarning (transaction) {
2017-11-02 03:30:33 +01:00
const { status } = transaction
// show rejected
if (status === 'rejected') {
return h('span.error', ' (Rejected)')
}
if (transaction.err || transaction.warning) {
const { err, warning = {} } = transaction
2017-08-29 16:50:48 +02:00
const errFirst = !!((err && warning) || err)
errFirst ? err.message : warning.message
2017-11-02 03:30:33 +01:00
// show error
if (err) {
const message = err.message || ''
return (
h(Tooltip, {
title: message,
position: 'bottom',
}, [
h(`span.error`, ` (Failed)`),
])
)
}
// show warning
if (warning) {
const message = warning.message
return h(Tooltip, {
title: message,
position: 'bottom',
}, [
h(`span.warning`, ` (Warning)`),
])
}
}
}