1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00
metamask-extension/app/scripts/ui.js

76 lines
2.4 KiB
JavaScript
Raw Normal View History

2016-01-15 03:26:54 +01:00
const injectCss = require('inject-css')
const NewMetaMaskUiCss = require('../../ui/css')
2016-09-13 08:28:07 +02:00
const startPopup = require('./popup-core')
const PortStream = require('extension-port-stream')
const { getEnvironmentType } = require('./lib/util')
const { ENVIRONMENT_TYPE_NOTIFICATION, ENVIRONMENT_TYPE_FULLSCREEN } = require('./lib/enums')
const extension = require('extensionizer')
const ExtensionPlatform = require('./platforms/extension')
const NotificationManager = require('./lib/notification-manager')
const notificationManager = new NotificationManager()
const setupSentry = require('./lib/setupSentry')
const log = require('loglevel')
2016-01-15 03:26:54 +01:00
2018-03-15 00:31:45 +01:00
start().catch(log.error)
2018-07-03 00:49:33 +02:00
async function start () {
2018-03-15 00:31:45 +01:00
// create platform global
global.platform = new ExtensionPlatform()
// setup sentry error reporting
const release = global.platform.getVersion()
setupSentry({ release, getState })
// provide app state to append to error logs
function getState () {
// get app state
const state = window.getCleanAppState()
// remove unnecessary data
delete state.localeMessages
delete state.metamask.recentBlocks
// return state to be added to request
return state
}
2018-03-15 00:31:45 +01:00
// identify window type (popup, notification)
const windowType = getEnvironmentType(window.location.href)
2018-03-15 00:31:45 +01:00
global.METAMASK_UI_TYPE = windowType
closePopupIfOpen(windowType)
// setup stream to background
const extensionPort = extension.runtime.connect({ name: windowType })
const connectionStream = new PortStream(extensionPort)
// start ui
const container = document.getElementById('app-content')
startPopup({ container, connectionStream }, (err, store) => {
2018-03-15 00:31:45 +01:00
if (err) return displayCriticalError(err)
const state = store.getState()
2019-01-30 15:16:40 +01:00
const { metamask: { completedOnboarding } = {} } = state
if (!completedOnboarding && windowType !== ENVIRONMENT_TYPE_FULLSCREEN) {
global.platform.openExtensionInBrowser()
return
}
2019-01-30 15:16:40 +01:00
injectCss(NewMetaMaskUiCss())
})
2016-01-15 11:03:42 +01:00
2016-08-17 01:46:44 +02:00
2018-03-15 00:31:45 +01:00
function closePopupIfOpen (windowType) {
2018-07-24 16:15:36 +02:00
if (windowType !== ENVIRONMENT_TYPE_NOTIFICATION) {
// should close only chrome popup
2018-07-24 16:15:36 +02:00
notificationManager.closePopup()
}
2018-03-15 00:31:45 +01:00
}
function displayCriticalError (err) {
container.innerHTML = '<div class="critical-error">The MetaMask app failed to load: please open and close MetaMask again to restart.</div>'
container.style.height = '80px'
log.error(err.stack)
throw err
2016-08-24 00:44:50 +02:00
}
}