Skip to content

Commit

Permalink
Merge pull request #5858 from storybooks/tech/workaround-persisted-op…
Browse files Browse the repository at this point in the history
…tions-with-defaults

Tech/workaround persisted options with defaults
  • Loading branch information
shilman authored Mar 5, 2019
2 parents be7646e + eeb024f commit 52968c9
Showing 1 changed file with 28 additions and 20 deletions.
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', 'theme');

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(initial, fromState);
const persisted = pick(store.getState(), 'layout', 'ui', 'selectedPanel', 'theme');
const state = merge(initial, persisted);

return { api, state };
}

1 comment on commit 52968c9

@vercel
Copy link

@vercel vercel bot commented on 52968c9 Mar 5, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deployment failed with the following error:

Builds rate limit exceeded (0 of 100 remaining). Try again in 23h

Please sign in to comment.