Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tech/workaround persisted options with defaults #5858

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 28 additions & 20 deletions lib/ui/src/core/layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,23 @@ const checkDeprecatedLayoutOptions = options => {
return {};
};

const initial = {
ui: {
enableShortcuts: true,
sortStoriesByKind: false,
sidebarAnimations: true,
},
layout: {
isToolshown: true,
isFullscreen: false,
showPanel: true,
showNav: true,
panelPosition: 'bottom',
},
theme: themes.light,
};

let hasSetOptions = false;
export default function({ store }) {
const api = {
toggleFullscreen(toggled) {
Expand Down Expand Up @@ -132,7 +149,13 @@ export default function({ store }) {
},

setOptions: options => {
const { layout, ui, selectedPanel, theme } = store.getState();
// The very first time the user sets their options, we don't consider what is in the store.
// At this point in time, what is in the store is what we *persisted*. We did that in order
// to avoid a FOUC (e.g. initial rendering the wrong theme while we waited for the stories to load)
// However, we don't want to have a memory about these things, otherwise we see bugs like the
// user setting a name for their storybook, persisting it, then never being able to unset it
// without clearing localstorage. See https://github.com/storybooks/storybook/issues/5857
const { layout, ui, selectedPanel, theme } = hasSetOptions ? store.getState() : initial;

if (options) {
const updatedLayout = {
Expand Down Expand Up @@ -170,29 +193,14 @@ export default function({ store }) {
if (Object.keys(modification).length) {
store.setState(modification, { persistence: 'permanent' });
}
}
},
};

const fromState = pick(store.getState(), 'layout', 'ui', 'selectedPanel');

const initial = {
ui: {
enableShortcuts: true,
sortStoriesByKind: false,
sidebarAnimations: true,
},
layout: {
isToolshown: true,
isFullscreen: false,
showPanel: true,
showNav: true,
panelPosition: 'bottom',
hasSetOptions = true;
}
},
theme: themes.light,
};

const state = merge(fromState, initial);
const persisted = pick(store.getState(), 'layout', 'ui', 'selectedPanel', 'theme');
const state = merge(initial, persisted);

return { api, state };
}