mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
53ec42d95f
Add alert suggesting that the user switch to a connected account. This alert is displayed when the popup is opened over an active tab that is connected to some account, but not the current selected account. The user can choose to switch to a connected account, or dismiss the alert. This alert is only shown once per account switch. So if the user repeatedly opens the popup on a dapp without switching accounts, it'll only be shown the first time. The alert also won't be shown if the user has just dismissed an "Unconnected account" alert on this same dapp and account, as that would be redundant. The alert has a "Don't show me this again" checkbox that allows the user to disable the alert. It can be re-enabled again on the Alerts settings page.
82 lines
2.4 KiB
JavaScript
82 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 = {
|
|
switchToConnected: 'switchToConnected',
|
|
unconnectedAccount: 'unconnectedAccount',
|
|
}
|
|
|
|
const defaultState = {
|
|
alertEnabledness: Object.keys(ALERT_TYPES)
|
|
.reduce(
|
|
(alertEnabledness, alertType) => {
|
|
alertEnabledness[alertType] = true
|
|
return alertEnabledness
|
|
},
|
|
{}
|
|
),
|
|
switchToConnectedAlertShown: {},
|
|
}
|
|
|
|
/**
|
|
* 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,
|
|
{
|
|
switchToConnectedAlertShown: {},
|
|
}
|
|
)
|
|
this.store = new ObservableStore(state)
|
|
|
|
const { selectedAddress } = preferencesStore.getState()
|
|
this.selectedAddress = selectedAddress
|
|
|
|
preferencesStore.subscribe(({ selectedAddress }) => {
|
|
const currentState = this.store.getState()
|
|
if (currentState.switchToConnectedAlertShown && this.selectedAddress !== selectedAddress) {
|
|
this.selectedAddress = selectedAddress
|
|
this.store.updateState({ switchToConnectedAlertShown: {} })
|
|
}
|
|
})
|
|
}
|
|
|
|
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
|
|
*/
|
|
setSwitchToConnectedAlertShown (origin) {
|
|
let { switchToConnectedAlertShown } = this.store.getState()
|
|
switchToConnectedAlertShown = { ...switchToConnectedAlertShown }
|
|
switchToConnectedAlertShown[origin] = true
|
|
this.store.updateState({ switchToConnectedAlertShown })
|
|
}
|
|
}
|