nifty-wallet/ui/app/components/identicon.js

87 lines
2.1 KiB
JavaScript
Raw Normal View History

const Component = require('react').Component
const h = require('react-hyperscript')
const inherits = require('util').inherits
2017-05-15 14:35:24 -07:00
const isNode = require('detect-node')
const findDOMNode = require('react-dom').findDOMNode
const jazzicon = require('jazzicon')
const iconFactoryGen = require('../../lib/icon-factory')
const iconFactory = iconFactoryGen(jazzicon)
module.exports = IdenticonComponent
inherits(IdenticonComponent, Component)
2016-06-21 13:18:32 -07:00
function IdenticonComponent () {
Component.call(this)
2016-12-16 10:04:57 -08:00
this.defaultDiameter = 46
}
2016-06-21 13:18:32 -07:00
IdenticonComponent.prototype.render = function () {
2016-10-27 16:16:28 -07:00
var props = this.props
2017-09-06 23:03:23 -07:00
const { className = '', address } = props
2016-10-27 16:16:28 -07:00
var diameter = props.diameter || this.defaultDiameter
2017-09-06 23:03:23 -07:00
return address
? (
h('div', {
className: `${className} identicon`,
key: 'identicon-' + address,
style: {
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
height: diameter,
width: diameter,
borderRadius: diameter / 2,
overflow: 'hidden',
},
})
)
: (
h('img.balance-icon', {
src: '../images/eth_logo.svg',
style: {
height: diameter,
width: diameter,
borderRadius: diameter / 2,
},
})
)
}
2016-06-21 13:18:32 -07:00
IdenticonComponent.prototype.componentDidMount = function () {
2016-10-27 16:16:28 -07:00
var props = this.props
const { address } = props
if (!address) return
var container = findDOMNode(this)
var diameter = props.diameter || this.defaultDiameter
2017-05-15 14:35:24 -07:00
if (!isNode) {
var img = iconFactory.iconForAddress(address, diameter)
2017-05-15 14:35:24 -07:00
container.appendChild(img)
}
}
IdenticonComponent.prototype.componentDidUpdate = function () {
var props = this.props
const { address } = props
if (!address) return
var container = findDOMNode(this)
var children = container.children
for (var i = 0; i < children.length; i++) {
container.removeChild(children[i])
}
var diameter = props.diameter || this.defaultDiameter
2017-05-15 14:35:24 -07:00
if (!isNode) {
var img = iconFactory.iconForAddress(address, diameter)
2017-05-15 14:35:24 -07:00
container.appendChild(img)
}
}