1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-10-22 19:26:13 +02:00
metamask-extension/app/scripts/controllers/alert.js
Whymarrh Whitby 4357cda7b8
Fix no-shadow issues (#9246)
See [`no-shadow`](https://eslint.org/docs/rules/no-shadow) for more information.

This change enables `no-shadow` and fixes the issues raised by the rule.
2020-08-18 14:06:45 -02:30

80 lines
2.4 KiB
JavaScript

import ObservableStore from 'obs-store'
/**
* @typedef {Object} AlertControllerInitState
* @property {Object} alertEnabledness - A map of any alerts that were suppressed keyed by alert ID, where the value
* is the timestamp of when the user suppressed the alert.
*/
/**
* @typedef {Object} AlertControllerOptions
* @property {AlertControllerInitState} initState - The initial controller state
*/
export const ALERT_TYPES = {
unconnectedAccount: 'unconnectedAccount',
}
const defaultState = {
alertEnabledness: Object.keys(ALERT_TYPES)
.reduce(
(alertEnabledness, alertType) => {
alertEnabledness[alertType] = true
return alertEnabledness
},
{},
),
unconnectedAccountAlertShownOrigins: {},
}
/**
* Controller responsible for maintaining
* alert related state
*/
export default class AlertController {
/**
* @constructor
* @param {AlertControllerOptions} [opts] - Controller configuration parameters
*/
constructor (opts = {}) {
const { initState, preferencesStore } = opts
const state = Object.assign(
{},
defaultState,
initState,
{
unconnectedAccountAlertShownOrigins: {},
},
)
this.store = new ObservableStore(state)
this.selectedAddress = preferencesStore.getState().selectedAddress
preferencesStore.subscribe(({ selectedAddress }) => {
const currentState = this.store.getState()
if (currentState.unconnectedAccountAlertShownOrigins && this.selectedAddress !== selectedAddress) {
this.selectedAddress = selectedAddress
this.store.updateState({ unconnectedAccountAlertShownOrigins: {} })
}
})
}
setAlertEnabledness (alertId, enabledness) {
let { alertEnabledness } = this.store.getState()
alertEnabledness = { ...alertEnabledness }
alertEnabledness[alertId] = enabledness
this.store.updateState({ alertEnabledness })
}
/**
* Sets the "switch to connected" alert as shown for the given origin
* @param {string} origin - The origin the alert has been shown for
*/
setUnconnectedAccountAlertShown (origin) {
let { unconnectedAccountAlertShownOrigins } = this.store.getState()
unconnectedAccountAlertShownOrigins = { ...unconnectedAccountAlertShownOrigins }
unconnectedAccountAlertShownOrigins[origin] = true
this.store.updateState({ unconnectedAccountAlertShownOrigins })
}
}