2020-01-09 04:34:58 +01:00
|
|
|
import ObservableStore from '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 {
|
2020-08-19 18:27:05 +02:00
|
|
|
|
2019-08-02 05:57:26 +02:00
|
|
|
/**
|
|
|
|
* Creates a new controller instance
|
|
|
|
*
|
|
|
|
* @param {OnboardingOptions} [opts] Controller configuration parameters
|
|
|
|
*/
|
|
|
|
constructor (opts = {}) {
|
2019-11-22 18:03:51 +01:00
|
|
|
const initialTransientState = {
|
|
|
|
onboardingTabs: {},
|
|
|
|
}
|
2020-08-19 18:27:05 +02:00
|
|
|
const initState = {
|
|
|
|
seedPhraseBackedUp: true,
|
|
|
|
...opts.initState,
|
|
|
|
...initialTransientState,
|
|
|
|
}
|
2019-08-02 05:57:26 +02:00
|
|
|
this.store = new ObservableStore(initState)
|
2019-11-22 18:03:51 +01:00
|
|
|
this.preferencesController = opts.preferencesController
|
|
|
|
this.completedOnboarding = this.preferencesController.store.getState().completedOnboarding
|
|
|
|
|
|
|
|
this.preferencesController.store.subscribe(({ completedOnboarding }) => {
|
|
|
|
if (completedOnboarding !== this.completedOnboarding) {
|
|
|
|
this.completedOnboarding = completedOnboarding
|
|
|
|
if (completedOnboarding) {
|
|
|
|
this.store.updateState(initialTransientState)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2019-08-02 05:57:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
setSeedPhraseBackedUp (newSeedPhraseBackUpState) {
|
|
|
|
this.store.updateState({
|
|
|
|
seedPhraseBackedUp: newSeedPhraseBackUpState,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
log.debug('Ignoring registerOnboarding; user already onboarded')
|
|
|
|
return
|
|
|
|
}
|
2020-08-19 18:27:05 +02:00
|
|
|
const onboardingTabs = { ...this.store.getState().onboardingTabs }
|
2019-11-22 18:03:51 +01:00
|
|
|
if (!onboardingTabs[location] || onboardingTabs[location] !== tabId) {
|
|
|
|
log.debug(`Registering onboarding tab at location '${location}' with tabId '${tabId}'`)
|
|
|
|
onboardingTabs[location] = tabId
|
|
|
|
this.store.updateState({ onboardingTabs })
|
|
|
|
}
|
|
|
|
}
|
2019-08-02 05:57:26 +02:00
|
|
|
}
|