1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-18 15:33:21 +01:00
metamask-extension/ui/app/components/identicon.js

117 lines
2.9 KiB
JavaScript
Raw Normal View History

const Component = require('react').Component
const h = require('react-hyperscript')
const inherits = require('util').inherits
const connect = require('react-redux').connect
2017-05-15 23:35:24 +02: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)
const { toDataUrl } = require('../../lib/blockies')
module.exports = connect(mapStateToProps)(IdenticonComponent)
inherits(IdenticonComponent, Component)
2016-06-21 22:18:32 +02:00
function IdenticonComponent () {
Component.call(this)
2016-12-16 19:04:57 +01:00
this.defaultDiameter = 46
}
function mapStateToProps (state) {
return {
2017-11-25 23:47:34 +01:00
useBlockie: state.metamask.useBlockie,
}
}
2016-06-21 22:18:32 +02:00
IdenticonComponent.prototype.render = function () {
2016-10-28 01:16:28 +02:00
var props = this.props
const { className = '', address } = props
2016-10-28 01:16:28 +02:00
var diameter = props.diameter || this.defaultDiameter
2017-09-07 08:03:23 +02: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',
},
})
2017-09-07 08:03:23 +02:00
)
: (
h('img.balance-icon', {
src: './images/eth_logo.svg',
2017-09-07 08:03:23 +02:00
style: {
height: diameter,
width: diameter,
borderRadius: diameter / 2,
},
})
)
}
2016-06-21 22:18:32 +02:00
IdenticonComponent.prototype.componentDidMount = function () {
2016-10-28 01:16:28 +02:00
var props = this.props
const { address, useBlockie } = props
if (!address) return
2017-05-15 23:35:24 +02:00
if (!isNode) {
// eslint-disable-next-line react/no-find-dom-node
var container = findDOMNode(this)
2017-11-25 23:11:29 +01:00
const diameter = props.diameter || this.defaultDiameter
if (useBlockie) {
_generateBlockie(container, address, diameter)
} else {
_generateJazzicon(container, address, diameter)
}
2017-05-15 23:35:24 +02:00
}
}
IdenticonComponent.prototype.componentDidUpdate = function () {
var props = this.props
const { address, useBlockie } = props
if (!address) return
2017-05-15 23:35:24 +02:00
if (!isNode) {
// eslint-disable-next-line react/no-find-dom-node
var container = findDOMNode(this)
var children = container.children
for (var i = 0; i < children.length; i++) {
container.removeChild(children[i])
}
2017-11-25 23:11:29 +01:00
const diameter = props.diameter || this.defaultDiameter
if (useBlockie) {
2017-11-25 23:11:29 +01:00
_generateBlockie(container, address, diameter)
} else {
_generateJazzicon(container, address, diameter)
}
2017-05-15 23:35:24 +02:00
}
}
2017-11-25 23:47:34 +01:00
function _generateBlockie (container, address, diameter) {
const img = new Image()
img.src = toDataUrl(address)
img.height = diameter
img.width = diameter
container.appendChild(img)
}
2017-11-25 23:47:34 +01:00
function _generateJazzicon (container, address, diameter) {
const img = iconFactory.iconForAddress(address, diameter)
container.appendChild(img)
}