nifty-wallet/app/scripts/lib/notifications.js

165 lines
4.8 KiB
JavaScript
Raw Normal View History

const createId = require('hat')
2016-06-24 17:22:27 -07:00
const extend = require('xtend')
const unmountComponentAtNode = require('react-dom').unmountComponentAtNode
const findDOMNode = require('react-dom').findDOMNode
const render = require('react-dom').render
const h = require('react-hyperscript')
2016-06-24 17:22:27 -07:00
const PendingTxDetails = require('../../../ui/app/components/pending-tx-details')
const PendingMsgDetails = require('../../../ui/app/components/pending-msg-details')
const MetaMaskUiCss = require('../../../ui/css')
var notificationHandlers = {}
module.exports = {
createUnlockRequestNotification: createUnlockRequestNotification,
createTxNotification: createTxNotification,
createMsgNotification: createMsgNotification,
}
2016-06-02 17:29:49 -07:00
setupListeners()
2016-06-21 13:18:32 -07:00
function setupListeners () {
2016-06-02 17:29:49 -07:00
// guard for chrome bug https://github.com/MetaMask/metamask-plugin/issues/236
if (!chrome.notifications) return console.error('Chrome notifications API missing...')
2016-06-02 17:29:49 -07:00
// notification button press
2016-06-21 13:18:32 -07:00
chrome.notifications.onButtonClicked.addListener(function (notificationId, buttonIndex) {
2016-06-02 17:29:49 -07:00
var handlers = notificationHandlers[notificationId]
if (buttonIndex === 0) {
handlers.confirm()
} else {
handlers.cancel()
}
chrome.notifications.clear(notificationId)
})
// notification teardown
2016-06-21 13:18:32 -07:00
chrome.notifications.onClosed.addListener(function (notificationId) {
2016-06-02 17:29:49 -07:00
delete notificationHandlers[notificationId]
})
}
// creation helper
2016-06-21 13:18:32 -07:00
function createUnlockRequestNotification (opts) {
// guard for chrome bug https://github.com/MetaMask/metamask-plugin/issues/236
if (!chrome.notifications) return console.error('Chrome notifications API missing...')
var message = 'An Ethereum app has requested a signature. Please unlock your account.'
var id = createId()
chrome.notifications.create(id, {
type: 'basic',
iconUrl: '/images/icon-128.png',
title: opts.title,
message: message,
})
}
function createTxNotification (state) {
// guard for chrome bug https://github.com/MetaMask/metamask-plugin/issues/236
if (!chrome.notifications) return console.error('Chrome notifications API missing...')
2016-06-24 17:22:27 -07:00
renderTxNotificationSVG(state, function(err, notificationSvgSource){
2016-06-23 16:53:45 -07:00
if (err) throw err
2016-06-24 17:22:27 -07:00
showNotification(extend(state, {
title: 'New Unsigned Transaction',
2016-06-24 17:22:27 -07:00
imageUrl: toSvgUri(notificationSvgSource),
}))
2016-06-22 19:28:11 -07:00
})
}
function createMsgNotification (state) {
// guard for chrome bug https://github.com/MetaMask/metamask-plugin/issues/236
if (!chrome.notifications) return console.error('Chrome notifications API missing...')
2016-06-24 17:22:27 -07:00
renderMsgNotificationSVG(state, function(err, notificationSvgSource){
if (err) throw err
showNotification(extend(state, {
title: 'New Unsigned Message',
2016-06-24 17:22:27 -07:00
imageUrl: toSvgUri(notificationSvgSource),
}))
})
}
function showNotification (state) {
// guard for chrome bug https://github.com/MetaMask/metamask-plugin/issues/236
if (!chrome.notifications) return console.error('Chrome notifications API missing...')
var id = createId()
chrome.notifications.create(id, {
2016-06-24 17:22:27 -07:00
type: 'image',
requireInteraction: true,
iconUrl: '/images/icon-128.png',
2016-06-24 17:22:27 -07:00
imageUrl: state.imageUrl,
title: state.title,
message: '',
buttons: [{
title: 'confirm',
2016-06-21 13:18:32 -07:00
}, {
title: 'cancel',
2016-06-21 13:18:32 -07:00
}],
})
notificationHandlers[id] = {
2016-06-24 17:22:27 -07:00
confirm: state.onConfirm,
cancel: state.onCancel,
}
2016-06-22 19:28:11 -07:00
2016-06-24 17:22:27 -07:00
}
2016-06-22 19:28:11 -07:00
2016-06-24 17:22:27 -07:00
function renderTxNotificationSVG(state, cb){
var content = h(PendingTxDetails, state)
renderNotificationSVG(content, cb)
}
2016-06-22 19:28:11 -07:00
2016-06-24 17:22:27 -07:00
function renderMsgNotificationSVG(state, cb){
var content = h(PendingMsgDetails, state)
renderNotificationSVG(content, cb)
}
2016-06-22 19:28:11 -07:00
2016-06-24 17:22:27 -07:00
function renderNotificationSVG(content, cb){
2016-06-22 19:28:11 -07:00
var container = document.createElement('div')
var confirmView = h('div.app-primary', {
style: {
width: '360px',
height: '240px',
padding: '16px',
// background: '#F7F7F7',
background: 'white',
},
}, [
h('style', MetaMaskUiCss()),
2016-06-24 17:22:27 -07:00
content,
2016-06-22 19:28:11 -07:00
])
render(confirmView, container, function ready(){
var rootElement = findDOMNode(this)
var viewSource = rootElement.outerHTML
2016-06-22 19:28:11 -07:00
unmountComponentAtNode(container)
var svgSource = svgWrapper(viewSource)
2016-06-22 19:28:11 -07:00
// insert content into svg wrapper
cb(null, svgSource)
2016-06-22 19:28:11 -07:00
})
}
function svgWrapper(content){
<<<<<<< HEAD
var wrapperSource = `
<svg xmlns="http://www.w3.org/2000/svg" width="360" height="240">
=======
var wrapperSource = `
<svg xmlns="http://www.w3.org/2000/svg" width="450" height="300">
>>>>>>> master
<foreignObject x="0" y="0" width="100%" height="100%">
<body xmlns="http://www.w3.org/1999/xhtml" height="100%">{{content}}</body>
</foreignObject>
</svg>
`
return wrapperSource.split('{{content}}').join(content)
2016-06-24 17:22:27 -07:00
}
function toSvgUri(content){
return 'data:image/svg+xml;utf8,' + encodeURIComponent(content)
}