-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
/
url.ts
193 lines (171 loc) · 5.74 KB
/
url.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import { navigate as navigateRouter, NavigateOptions } from '@reach/router';
import { NAVIGATE_URL, STORY_ARGS_UPDATED, SET_CURRENT_STORY } from '@storybook/core-events';
import { queryFromLocation, navigate as queryNavigate, buildArgsParam } from '@storybook/router';
import { toId, sanitize } from '@storybook/csf';
import deepEqual from 'fast-deep-equal';
import { window } from 'global';
import { ModuleArgs, ModuleFn } from '../index';
import { PanelPositions } from './layout';
import { isStory } from '../lib/stories';
interface Additions {
isFullscreen?: boolean;
showPanel?: boolean;
panelPosition?: PanelPositions;
showNav?: boolean;
selectedPanel?: string;
viewMode?: string;
}
export interface SubState {
customQueryParams: QueryParams;
}
// Initialize the state based on the URL.
// NOTE:
// Although we don't change the URL when you change the state, we do support setting initial state
// via the following URL parameters:
// - full: 0/1 -- show fullscreen
// - panel: bottom/right/0 -- set addons panel position (or hide)
// - nav: 0/1 -- show or hide the story list
//
// We also support legacy URLs from storybook <5
let prevParams: ReturnType<typeof queryFromLocation>;
const initialUrlSupport = ({
state: { location, path, viewMode, storyId: storyIdFromUrl },
}: ModuleArgs) => {
const addition: Additions = {};
const query = queryFromLocation(location);
let selectedPanel;
const {
full,
panel,
nav,
addons,
panelRight,
stories,
addonPanel,
selectedKind,
selectedStory,
path: queryPath,
...otherParams
} = query;
if (full === '1') {
addition.isFullscreen = true;
}
if (panel) {
if (['right', 'bottom'].includes(panel)) {
addition.panelPosition = panel;
} else if (panel === '0') {
addition.showPanel = false;
}
}
if (nav === '0') {
addition.showNav = false;
}
// Legacy URLs
if (addons === '0') {
addition.showPanel = false;
}
if (panelRight === '1') {
addition.panelPosition = 'right';
}
if (stories === '0') {
addition.showNav = false;
}
if (addonPanel) {
selectedPanel = addonPanel;
}
// If the user hasn't set the storyId on the URL, we support legacy URLs (selectedKind/selectedStory)
// NOTE: this "storyId" can just be a prefix of a storyId, really it is a storyIdSpecifier.
let storyId = storyIdFromUrl;
if (!storyId) {
if (selectedKind && selectedStory) {
storyId = toId(selectedKind, selectedStory);
} else if (selectedKind) {
storyId = sanitize(selectedKind);
}
}
// Avoid returning a new object each time if no params actually changed.
const customQueryParams = deepEqual(prevParams, otherParams) ? prevParams : otherParams;
prevParams = customQueryParams;
return { viewMode, layout: addition, selectedPanel, location, path, customQueryParams, storyId };
};
export interface QueryParams {
[key: string]: string | null;
}
export interface SubAPI {
navigateUrl: (url: string, options: NavigateOptions<{}>) => void;
getQueryParam: (key: string) => string | undefined;
getUrlState: () => {
queryParams: QueryParams;
path: string;
viewMode?: string;
storyId?: string;
url: string;
};
setQueryParams: (input: QueryParams) => void;
}
export const init: ModuleFn = ({ store, navigate, state, provider, fullAPI, ...rest }) => {
const api: SubAPI = {
getQueryParam(key) {
const { customQueryParams } = store.getState();
return customQueryParams ? customQueryParams[key] : undefined;
},
getUrlState() {
const { path, customQueryParams, storyId, url, viewMode } = store.getState();
return { path, queryParams: customQueryParams, storyId, url, viewMode };
},
setQueryParams(input) {
const { customQueryParams } = store.getState();
const queryParams: QueryParams = {};
const update = {
...customQueryParams,
...Object.entries(input).reduce((acc, [key, value]) => {
if (value !== null) {
acc[key] = value;
}
return acc;
}, queryParams),
};
const equal = deepEqual(customQueryParams, update);
if (!equal) store.setState({ customQueryParams: update });
},
navigateUrl(url: string, options: NavigateOptions<{}>) {
navigateRouter(url, options);
},
};
const initModule = () => {
// Sets `args` parameter in URL, omitting any args that have their initial value or cannot be unserialized safely.
const updateArgsParam = () => {
const { path, viewMode } = fullAPI.getUrlState();
if (viewMode !== 'story') return;
const currentStory = fullAPI.getCurrentStoryData();
if (!isStory(currentStory)) return;
const { args, initialArgs } = currentStory;
const argsString = buildArgsParam(initialArgs, args);
const argsParam = argsString.length ? `&args=${argsString}` : '';
queryNavigate(`${path}${argsParam}`, { replace: true });
api.setQueryParams({ args: argsString });
};
fullAPI.on(SET_CURRENT_STORY, () => updateArgsParam());
let handleOrId: any;
fullAPI.on(STORY_ARGS_UPDATED, () => {
if ('requestIdleCallback' in window) {
if (handleOrId) window.cancelIdleCallback(handleOrId);
handleOrId = window.requestIdleCallback(updateArgsParam, { timeout: 1000 });
} else {
if (handleOrId) clearTimeout(handleOrId);
setTimeout(updateArgsParam, 100);
}
});
fullAPI.on(NAVIGATE_URL, (url: string, options: { [k: string]: any }) => {
fullAPI.navigateUrl(url, options);
});
if (fullAPI.showReleaseNotesOnLaunch()) {
navigate('/settings/release-notes');
}
};
return {
api,
state: initialUrlSupport({ store, navigate, state, provider, fullAPI, ...rest }),
init: initModule,
};
};