2020-05-08 21:45:52 +02:00
|
|
|
import ObservableStore from 'obs-store'
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {Object} AlertControllerInitState
|
2020-10-06 19:57:02 +02:00
|
|
|
* @property {Object} alertEnabledness - A map of alerts IDs to booleans, where
|
|
|
|
* `true` indicates that the alert is enabled and shown, and `false` the opposite.
|
|
|
|
* @property {Object} unconnectedAccountAlertShownOrigins - A map of origin
|
|
|
|
* strings to booleans indicating whether the "switch to connected" alert has
|
|
|
|
* been shown (`true`) or otherwise (`false`).
|
2020-05-08 21:45:52 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {Object} AlertControllerOptions
|
|
|
|
* @property {AlertControllerInitState} initState - The initial controller state
|
|
|
|
*/
|
|
|
|
|
|
|
|
export const ALERT_TYPES = {
|
|
|
|
unconnectedAccount: 'unconnectedAccount',
|
2020-10-06 19:57:02 +02:00
|
|
|
// enumerated here but has no background state
|
|
|
|
invalidCustomNetwork: 'invalidCustomNetwork',
|
2020-05-08 21:45:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const defaultState = {
|
|
|
|
alertEnabledness: Object.keys(ALERT_TYPES)
|
|
|
|
.reduce(
|
|
|
|
(alertEnabledness, alertType) => {
|
|
|
|
alertEnabledness[alertType] = true
|
|
|
|
return alertEnabledness
|
|
|
|
},
|
2020-07-14 17:20:41 +02:00
|
|
|
{},
|
2020-05-08 21:45:52 +02:00
|
|
|
),
|
2020-06-15 21:08:53 +02:00
|
|
|
unconnectedAccountAlertShownOrigins: {},
|
2020-05-08 21:45:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Controller responsible for maintaining
|
|
|
|
* alert related state
|
|
|
|
*/
|
|
|
|
export default class AlertController {
|
2020-08-19 18:27:05 +02:00
|
|
|
|
2020-05-08 21:45:52 +02:00
|
|
|
/**
|
|
|
|
* @constructor
|
|
|
|
* @param {AlertControllerOptions} [opts] - Controller configuration parameters
|
|
|
|
*/
|
|
|
|
constructor (opts = {}) {
|
2020-05-12 15:01:52 +02:00
|
|
|
const { initState, preferencesStore } = opts
|
2020-08-19 18:27:05 +02:00
|
|
|
const state = {
|
|
|
|
...defaultState,
|
|
|
|
...initState,
|
|
|
|
unconnectedAccountAlertShownOrigins: {},
|
|
|
|
}
|
2020-10-06 19:57:02 +02:00
|
|
|
|
2020-05-08 21:45:52 +02:00
|
|
|
this.store = new ObservableStore(state)
|
2020-05-12 15:01:52 +02:00
|
|
|
|
2020-08-18 18:36:45 +02:00
|
|
|
this.selectedAddress = preferencesStore.getState().selectedAddress
|
2020-05-12 15:01:52 +02:00
|
|
|
|
|
|
|
preferencesStore.subscribe(({ selectedAddress }) => {
|
|
|
|
const currentState = this.store.getState()
|
2020-06-15 21:08:53 +02:00
|
|
|
if (currentState.unconnectedAccountAlertShownOrigins && this.selectedAddress !== selectedAddress) {
|
2020-05-12 15:01:52 +02:00
|
|
|
this.selectedAddress = selectedAddress
|
2020-06-15 21:08:53 +02:00
|
|
|
this.store.updateState({ unconnectedAccountAlertShownOrigins: {} })
|
2020-05-12 15:01:52 +02:00
|
|
|
}
|
|
|
|
})
|
2020-05-08 21:45:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
setAlertEnabledness (alertId, enabledness) {
|
|
|
|
let { alertEnabledness } = this.store.getState()
|
|
|
|
alertEnabledness = { ...alertEnabledness }
|
|
|
|
alertEnabledness[alertId] = enabledness
|
|
|
|
this.store.updateState({ alertEnabledness })
|
|
|
|
}
|
2020-05-12 15:01:52 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the "switch to connected" alert as shown for the given origin
|
|
|
|
* @param {string} origin - The origin the alert has been shown for
|
|
|
|
*/
|
2020-06-15 21:08:53 +02:00
|
|
|
setUnconnectedAccountAlertShown (origin) {
|
|
|
|
let { unconnectedAccountAlertShownOrigins } = this.store.getState()
|
|
|
|
unconnectedAccountAlertShownOrigins = { ...unconnectedAccountAlertShownOrigins }
|
|
|
|
unconnectedAccountAlertShownOrigins[origin] = true
|
|
|
|
this.store.updateState({ unconnectedAccountAlertShownOrigins })
|
2020-05-12 15:01:52 +02:00
|
|
|
}
|
2020-05-08 21:45:52 +02:00
|
|
|
}
|