2021-02-04 19:15:23 +01:00
|
|
|
import { ObservableStore } from '@metamask/obs-store';
|
|
|
|
import log from 'loglevel';
|
2019-08-02 05:57:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {Object} InitState
|
|
|
|
* @property {Boolean} seedPhraseBackedUp Indicates whether the user has completed the seed phrase backup challenge
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {Object} OnboardingOptions
|
|
|
|
* @property {InitState} initState The initial controller state
|
2019-11-22 18:03:51 +01:00
|
|
|
* @property {PreferencesController} preferencesController Controller for managing user perferences
|
2019-08-02 05:57:26 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Controller responsible for maintaining
|
2019-11-22 18:03:51 +01:00
|
|
|
* state related to onboarding
|
2019-08-02 05:57:26 +02:00
|
|
|
*/
|
2020-05-06 00:19:38 +02:00
|
|
|
export default class OnboardingController {
|
2019-08-02 05:57:26 +02:00
|
|
|
/**
|
|
|
|
* Creates a new controller instance
|
|
|
|
*
|
|
|
|
* @param {OnboardingOptions} [opts] Controller configuration parameters
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
constructor(opts = {}) {
|
2019-11-22 18:03:51 +01:00
|
|
|
const initialTransientState = {
|
|
|
|
onboardingTabs: {},
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-08-19 18:27:05 +02:00
|
|
|
const initState = {
|
2020-11-10 17:04:20 +01:00
|
|
|
seedPhraseBackedUp: null,
|
2020-08-19 18:27:05 +02:00
|
|
|
...opts.initState,
|
|
|
|
...initialTransientState,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
|
|
|
this.store = new ObservableStore(initState);
|
|
|
|
this.preferencesController = opts.preferencesController;
|
|
|
|
this.completedOnboarding = this.preferencesController.store.getState().completedOnboarding;
|
2019-11-22 18:03:51 +01:00
|
|
|
|
|
|
|
this.preferencesController.store.subscribe(({ completedOnboarding }) => {
|
|
|
|
if (completedOnboarding !== this.completedOnboarding) {
|
2021-02-04 19:15:23 +01:00
|
|
|
this.completedOnboarding = completedOnboarding;
|
2019-11-22 18:03:51 +01:00
|
|
|
if (completedOnboarding) {
|
2021-02-04 19:15:23 +01:00
|
|
|
this.store.updateState(initialTransientState);
|
2019-11-22 18:03:51 +01:00
|
|
|
}
|
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2019-08-02 05:57:26 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
setSeedPhraseBackedUp(newSeedPhraseBackUpState) {
|
2019-08-02 05:57:26 +02:00
|
|
|
this.store.updateState({
|
|
|
|
seedPhraseBackedUp: newSeedPhraseBackUpState,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2019-08-02 05:57:26 +02:00
|
|
|
}
|
|
|
|
|
2019-11-22 18:03:51 +01:00
|
|
|
/**
|
|
|
|
* Registering a site as having initiated onboarding
|
|
|
|
*
|
|
|
|
* @param {string} location - The location of the site registering
|
|
|
|
* @param {string} tabId - The id of the tab registering
|
|
|
|
*/
|
2019-12-20 16:32:31 +01:00
|
|
|
registerOnboarding = async (location, tabId) => {
|
2019-11-22 18:03:51 +01:00
|
|
|
if (this.completedOnboarding) {
|
2021-02-04 19:15:23 +01:00
|
|
|
log.debug('Ignoring registerOnboarding; user already onboarded');
|
|
|
|
return;
|
2019-11-22 18:03:51 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
const onboardingTabs = { ...this.store.getState().onboardingTabs };
|
2019-11-22 18:03:51 +01:00
|
|
|
if (!onboardingTabs[location] || onboardingTabs[location] !== tabId) {
|
2020-11-03 00:41:28 +01:00
|
|
|
log.debug(
|
|
|
|
`Registering onboarding tab at location '${location}' with tabId '${tabId}'`,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
onboardingTabs[location] = tabId;
|
|
|
|
this.store.updateState({ onboardingTabs });
|
2019-11-22 18:03:51 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2019-08-02 05:57:26 +02:00
|
|
|
}
|