2021-02-04 19:15:23 +01:00
|
|
|
import { ObservableStore } from '@metamask/obs-store';
|
2020-12-11 00:40:29 +01:00
|
|
|
import {
|
|
|
|
TOGGLEABLE_ALERT_TYPES,
|
|
|
|
WEB3_SHIM_USAGE_ALERT_STATES,
|
2021-02-04 19:15:23 +01:00
|
|
|
} from '../../../shared/constants/alerts';
|
2020-05-08 21:45:52 +02:00
|
|
|
|
|
|
|
/**
|
2022-07-27 15:28:05 +02:00
|
|
|
* @typedef {object} AlertControllerInitState
|
|
|
|
* @property {object} alertEnabledness - A map of alerts IDs to booleans, where
|
2020-10-06 19:57:02 +02:00
|
|
|
* `true` indicates that the alert is enabled and shown, and `false` the opposite.
|
2022-07-27 15:28:05 +02:00
|
|
|
* @property {object} unconnectedAccountAlertShownOrigins - A map of origin
|
2020-10-06 19:57:02 +02:00
|
|
|
* 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
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2022-07-27 15:28:05 +02:00
|
|
|
* @typedef {object} AlertControllerOptions
|
2020-05-08 21:45:52 +02:00
|
|
|
* @property {AlertControllerInitState} initState - The initial controller state
|
|
|
|
*/
|
|
|
|
|
|
|
|
const defaultState = {
|
2020-12-11 00:40:29 +01:00
|
|
|
alertEnabledness: TOGGLEABLE_ALERT_TYPES.reduce(
|
2020-11-03 00:41:28 +01:00
|
|
|
(alertEnabledness, alertType) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
alertEnabledness[alertType] = true;
|
|
|
|
return alertEnabledness;
|
2020-11-03 00:41:28 +01:00
|
|
|
},
|
|
|
|
{},
|
|
|
|
),
|
2020-06-15 21:08:53 +02:00
|
|
|
unconnectedAccountAlertShownOrigins: {},
|
2020-12-11 00:40:29 +01:00
|
|
|
web3ShimUsageOrigins: {},
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-05-08 21:45:52 +02:00
|
|
|
|
|
|
|
/**
|
2020-12-11 00:40:29 +01:00
|
|
|
* Controller responsible for maintaining alert-related state.
|
2020-05-08 21:45:52 +02:00
|
|
|
*/
|
|
|
|
export default class AlertController {
|
|
|
|
/**
|
|
|
|
* @param {AlertControllerOptions} [opts] - Controller configuration parameters
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
constructor(opts = {}) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const { initState = {}, preferencesStore } = opts;
|
2020-08-19 18:27:05 +02:00
|
|
|
const state = {
|
|
|
|
...defaultState,
|
2020-12-11 00:40:29 +01:00
|
|
|
alertEnabledness: {
|
|
|
|
...defaultState.alertEnabledness,
|
|
|
|
...initState.alertEnabledness,
|
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-10-06 19:57:02 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
this.store = new ObservableStore(state);
|
2020-05-12 15:01:52 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
this.selectedAddress = preferencesStore.getState().selectedAddress;
|
2020-05-12 15:01:52 +02:00
|
|
|
|
|
|
|
preferencesStore.subscribe(({ selectedAddress }) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const currentState = this.store.getState();
|
2020-11-03 00:41:28 +01:00
|
|
|
if (
|
|
|
|
currentState.unconnectedAccountAlertShownOrigins &&
|
|
|
|
this.selectedAddress !== selectedAddress
|
|
|
|
) {
|
2021-02-04 19:15:23 +01:00
|
|
|
this.selectedAddress = selectedAddress;
|
|
|
|
this.store.updateState({ unconnectedAccountAlertShownOrigins: {} });
|
2020-05-12 15:01:52 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-05-08 21:45:52 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
setAlertEnabledness(alertId, enabledness) {
|
2021-02-04 19:15:23 +01:00
|
|
|
let { alertEnabledness } = this.store.getState();
|
|
|
|
alertEnabledness = { ...alertEnabledness };
|
|
|
|
alertEnabledness[alertId] = enabledness;
|
|
|
|
this.store.updateState({ alertEnabledness });
|
2020-05-08 21:45:52 +02:00
|
|
|
}
|
2020-05-12 15:01:52 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the "switch to connected" alert as shown for the given origin
|
2022-01-07 16:57:33 +01:00
|
|
|
*
|
2020-05-12 15:01:52 +02:00
|
|
|
* @param {string} origin - The origin the alert has been shown for
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
setUnconnectedAccountAlertShown(origin) {
|
2021-02-04 19:15:23 +01:00
|
|
|
let { unconnectedAccountAlertShownOrigins } = this.store.getState();
|
2020-11-03 00:41:28 +01:00
|
|
|
unconnectedAccountAlertShownOrigins = {
|
|
|
|
...unconnectedAccountAlertShownOrigins,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
|
|
|
unconnectedAccountAlertShownOrigins[origin] = true;
|
|
|
|
this.store.updateState({ unconnectedAccountAlertShownOrigins });
|
2020-05-12 15:01:52 +02:00
|
|
|
}
|
2020-12-11 00:40:29 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the web3 shim usage state for the given origin.
|
|
|
|
*
|
|
|
|
* @param {string} origin - The origin to get the web3 shim usage state for.
|
|
|
|
* @returns {undefined | 1 | 2} The web3 shim usage state for the given
|
|
|
|
* origin, or undefined.
|
|
|
|
*/
|
|
|
|
getWeb3ShimUsageState(origin) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return this.store.getState().web3ShimUsageOrigins[origin];
|
2020-12-11 00:40:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the web3 shim usage state for the given origin to RECORDED.
|
|
|
|
*
|
|
|
|
* @param {string} origin - The origin the that used the web3 shim.
|
|
|
|
*/
|
|
|
|
setWeb3ShimUsageRecorded(origin) {
|
2021-02-04 19:15:23 +01:00
|
|
|
this._setWeb3ShimUsageState(origin, WEB3_SHIM_USAGE_ALERT_STATES.RECORDED);
|
2020-12-11 00:40:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the web3 shim usage state for the given origin to DISMISSED.
|
|
|
|
*
|
|
|
|
* @param {string} origin - The origin that the web3 shim notification was
|
|
|
|
* dismissed for.
|
|
|
|
*/
|
|
|
|
setWeb3ShimUsageAlertDismissed(origin) {
|
2021-02-04 19:15:23 +01:00
|
|
|
this._setWeb3ShimUsageState(origin, WEB3_SHIM_USAGE_ALERT_STATES.DISMISSED);
|
2020-12-11 00:40:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
* @param {string} origin - The origin to set the state for.
|
|
|
|
* @param {number} value - The state value to set.
|
|
|
|
*/
|
|
|
|
_setWeb3ShimUsageState(origin, value) {
|
2021-02-04 19:15:23 +01:00
|
|
|
let { web3ShimUsageOrigins } = this.store.getState();
|
2020-12-11 00:40:29 +01:00
|
|
|
web3ShimUsageOrigins = {
|
|
|
|
...web3ShimUsageOrigins,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
|
|
|
web3ShimUsageOrigins[origin] = value;
|
|
|
|
this.store.updateState({ web3ShimUsageOrigins });
|
2020-12-11 00:40:29 +01:00
|
|
|
}
|
2020-05-08 21:45:52 +02:00
|
|
|
}
|