Skip to content
31 changes: 23 additions & 8 deletions src/pages/createStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
};
})(),
},
},
};
Expand Down
Loading