diff --git a/src/pages/createStore.ts b/src/pages/createStore.ts index e795f1faa7..34ea0779e8 100644 --- a/src/pages/createStore.ts +++ b/src/pages/createStore.ts @@ -4,7 +4,12 @@ import { throttle } from 'lodash-es'; import createSagaMiddleware from 'redux-saga'; import type { SourceActionType } from 'src/commons/utils/ActionsHelper'; -import { defaultState, type OverallState } from '../commons/application/ApplicationTypes'; +import { + ALL_LANGUAGES, + defaultLanguageConfig, + defaultState, + type OverallState, +} from '../commons/application/ApplicationTypes'; import rootReducer from '../commons/application/reducers/RootReducer'; import MainSaga from '../commons/sagas/MainSaga'; import { generateOctokitInstance } from '../commons/utils/GitHubPersistenceHelper'; @@ -79,15 +84,25 @@ function loadStore(loadedStore: SavedState | undefined) { externalLibrary: loadedStore.playgroundExternalLibrary ? loadedStore.playgroundExternalLibrary : defaultState.workspaces.playground.externalLibrary, - context: { - ...defaultState.workspaces.playground.context, - chapter: loadedStore.playgroundSourceChapter + context: (() => { + const chapter = loadedStore.playgroundSourceChapter ? loadedStore.playgroundSourceChapter - : defaultState.workspaces.playground.context.chapter, - variant: loadedStore.playgroundSourceVariant + : defaultState.workspaces.playground.context.chapter; + const variant = loadedStore.playgroundSourceVariant ? loadedStore.playgroundSourceVariant - : defaultState.workspaces.playground.context.variant, - }, + : defaultState.workspaces.playground.context.variant; + const validCombination = ALL_LANGUAGES.some( + lang => lang.chapter === chapter && lang.variant === variant, + ); + // Invalid stored combinations (e.g. chapter 1 + explicit-control, which can be + // persisted when toggling feature flags) would crash on load, so fall back to + // the default language config as a coherent, always-valid pair. + return { + ...defaultState.workspaces.playground.context, + chapter: validCombination ? chapter : defaultLanguageConfig.chapter, + variant: validCombination ? variant : defaultLanguageConfig.variant, + }; + })(), }, }, };