Skip to content

Commit

Permalink
Restore args param when switching stories or viewMode
Browse files Browse the repository at this point in the history
  • Loading branch information
ghengeveld committed Apr 7, 2021
1 parent 0b4c01c commit 6d54805
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions lib/api/src/modules/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { window } from 'global';
import { ModuleArgs, ModuleFn } from '../index';
import { PanelPositions } from './layout';
import { isStory } from '../lib/stories';
import type { Story } from '../lib/stories';

interface Additions {
isFullscreen?: boolean;
Expand Down Expand Up @@ -150,11 +149,14 @@ export const init: ModuleFn = ({ store, navigate, state, provider, fullAPI, ...r

const initModule = () => {
// Sets `args` parameter in URL, omitting any args that have their initial value or cannot be unserialized safely.
const updateArgsParam = (args?: Story['args']) => {
const updateArgsParam = () => {
const { path, viewMode } = fullAPI.getUrlState();
if (viewMode !== 'story') return;

const currentStory = fullAPI.getCurrentStoryData();
const initialArgs = (isStory(currentStory) && currentStory.initialArgs) || {};
if (!isStory(currentStory)) return;

const { args, initialArgs } = currentStory;
const argsString = buildArgsParam(initialArgs, args);
const argsParam = argsString.length ? `&args=${argsString}` : '';
queryNavigate(`${path}${argsParam}`, { replace: true });
Expand All @@ -164,13 +166,13 @@ export const init: ModuleFn = ({ store, navigate, state, provider, fullAPI, ...r
fullAPI.on(SET_CURRENT_STORY, () => updateArgsParam());

let handleOrId: any;
fullAPI.on(STORY_ARGS_UPDATED, ({ args }) => {
fullAPI.on(STORY_ARGS_UPDATED, () => {
if ('requestIdleCallback' in window) {
if (handleOrId) window.cancelIdleCallback(handleOrId);
handleOrId = window.requestIdleCallback(() => updateArgsParam(args), { timeout: 1000 });
handleOrId = window.requestIdleCallback(updateArgsParam, { timeout: 1000 });
} else {
if (handleOrId) clearTimeout(handleOrId);
setTimeout(updateArgsParam, 100, args);
setTimeout(updateArgsParam, 100);
}
});

Expand Down

0 comments on commit 6d54805

Please sign in to comment.