1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00
metamask-extension/app/scripts/controllers/onboarding.js
Mark Stacey a6cc2d3d7c
Track whether seed phrase has been backed up (#9830)
The `seedPhraseBackedUp` now tracks whether or not the seed phrase has
been backed up. Previously this defaulted to `true`, which left no way
to distinguish whether it had been backed up or not during onboarding.

The default is now `null`, and the UI logic has been updated to account
for this, so that "existing users" (i.e. users that have a backup that
is years old) aren't mistakenly considered to have not backed up their
seed phrase. This value is already set explicitly to `true` or `false`
during onboarding, in both the create and import flow.

This change was made primarily to make it easier to fix the onboarding
library integration, which will be done in a subsequent PR.
2020-11-10 12:34:20 -03:30

75 lines
2.3 KiB
JavaScript

import ObservableStore from 'obs-store'
import log from 'loglevel'
/**
* @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
* @property {PreferencesController} preferencesController Controller for managing user perferences
*/
/**
* Controller responsible for maintaining
* state related to onboarding
*/
export default class OnboardingController {
/**
* Creates a new controller instance
*
* @param {OnboardingOptions} [opts] Controller configuration parameters
*/
constructor(opts = {}) {
const initialTransientState = {
onboardingTabs: {},
}
const initState = {
seedPhraseBackedUp: null,
...opts.initState,
...initialTransientState,
}
this.store = new ObservableStore(initState)
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)
}
}
})
}
setSeedPhraseBackedUp(newSeedPhraseBackUpState) {
this.store.updateState({
seedPhraseBackedUp: newSeedPhraseBackUpState,
})
}
/**
* 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
*/
registerOnboarding = async (location, tabId) => {
if (this.completedOnboarding) {
log.debug('Ignoring registerOnboarding; user already onboarded')
return
}
const onboardingTabs = { ...this.store.getState().onboardingTabs }
if (!onboardingTabs[location] || onboardingTabs[location] !== tabId) {
log.debug(
`Registering onboarding tab at location '${location}' with tabId '${tabId}'`,
)
onboardingTabs[location] = tabId
this.store.updateState({ onboardingTabs })
}
}
}