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
|
|
|
|
|
|
|
/**
|
2022-07-27 15:28:05 +02:00
|
|
|
* @typedef {object} InitState
|
2022-01-07 16:57:33 +01:00
|
|
|
* @property {boolean} seedPhraseBackedUp Indicates whether the user has completed the seed phrase backup challenge
|
|
|
|
* @property {boolean} completedOnboarding Indicates whether the user has completed the onboarding flow
|
2019-08-02 05:57:26 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2022-07-27 15:28:05 +02:00
|
|
|
* @typedef {object} OnboardingOptions
|
2019-08-02 05:57:26 +02:00
|
|
|
* @property {InitState} initState The initial controller state
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*
|
2022-01-07 16:57:33 +01:00
|
|
|
* @param {OnboardingOptions} [opts] - Controller configuration parameters
|
2019-08-02 05:57:26 +02:00
|
|
|
*/
|
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,
|
2021-10-15 20:52:52 +02:00
|
|
|
firstTimeFlowType: null,
|
|
|
|
completedOnboarding: false,
|
2020-08-19 18:27:05 +02:00
|
|
|
...opts.initState,
|
|
|
|
...initialTransientState,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
|
|
|
this.store = new ObservableStore(initState);
|
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
|
|
|
}
|
|
|
|
|
2021-10-15 20:52:52 +02:00
|
|
|
// /**
|
|
|
|
// * Sets the completedOnboarding state to true, indicating that the user has completed the
|
|
|
|
// * onboarding process.
|
|
|
|
// */
|
2023-01-20 21:20:18 +01:00
|
|
|
async completeOnboarding() {
|
2021-10-15 20:52:52 +02:00
|
|
|
this.store.updateState({
|
|
|
|
completedOnboarding: true,
|
|
|
|
});
|
2023-01-20 21:20:18 +01:00
|
|
|
return true;
|
2021-10-15 20:52:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Setter for the `firstTimeFlowType` property
|
|
|
|
*
|
|
|
|
* @param {string} type - Indicates the type of first time flow - create or import - the user wishes to follow
|
|
|
|
*/
|
|
|
|
setFirstTimeFlowType(type) {
|
|
|
|
this.store.updateState({ firstTimeFlowType: type });
|
|
|
|
}
|
|
|
|
|
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) => {
|
2022-02-23 01:59:26 +01:00
|
|
|
if (this.store.getState().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
|
|
|
}
|