Skip to content
Merged
Show file tree
Hide file tree
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
32 changes: 17 additions & 15 deletions code/lib/preview-api/src/modules/preview-web/UrlStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const getQueryString = ({
selection?: Selection;
extraParams?: qs.ParsedQs;
}) => {
const { search = '' } = document.location;
const search = typeof document !== 'undefined' ? document.location.search : '';
const { path, selectedKind, selectedStory, ...rest } = qs.parse(search, {
ignoreQueryPrefix: true,
});
Expand Down Expand Up @@ -65,20 +65,22 @@ const getFirstString = (v: ValueOf<qs.ParsedQs>): string | void => {
};

export const getSelectionSpecifierFromPath: () => SelectionSpecifier | null = () => {
const query = qs.parse(document.location.search, { ignoreQueryPrefix: true });
const args = typeof query.args === 'string' ? parseArgsParam(query.args) : undefined;
const globals = typeof query.globals === 'string' ? parseArgsParam(query.globals) : undefined;

let viewMode = getFirstString(query.viewMode) as ViewMode;
if (typeof viewMode !== 'string' || !viewMode.match(/docs|story/)) {
viewMode = 'story';
}

const path = getFirstString(query.path);
const storyId = path ? pathToId(path) : getFirstString(query.id);

if (storyId) {
return { storySpecifier: storyId, args, globals, viewMode };
if (typeof document !== 'undefined') {
const query = qs.parse(document.location.search, { ignoreQueryPrefix: true });
const args = typeof query.args === 'string' ? parseArgsParam(query.args) : undefined;
const globals = typeof query.globals === 'string' ? parseArgsParam(query.globals) : undefined;

let viewMode = getFirstString(query.viewMode) as ViewMode;
if (typeof viewMode !== 'string' || !viewMode.match(/docs|story/)) {
viewMode = 'story';
}

const path = getFirstString(query.path);
const storyId = path ? pathToId(path) : getFirstString(query.id);

if (storyId) {
return { storySpecifier: storyId, args, globals, viewMode };
}
}

return null;
Expand Down
38 changes: 21 additions & 17 deletions code/lib/preview-api/src/modules/preview-web/WebView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,24 @@ export class WebView implements View<HTMLElement> {

constructor() {
// Special code for testing situations
// eslint-disable-next-line @typescript-eslint/naming-convention
const { __SPECIAL_TEST_PARAMETER__ } = qs.parse(document.location.search, {
ignoreQueryPrefix: true,
});
switch (__SPECIAL_TEST_PARAMETER__) {
case 'preparing-story': {
this.showPreparingStory();
this.testing = true;
break;
}
case 'preparing-docs': {
this.showPreparingDocs();
this.testing = true;
break;
if (typeof document !== 'undefined') {
// eslint-disable-next-line @typescript-eslint/naming-convention
const { __SPECIAL_TEST_PARAMETER__ } = qs.parse(document.location.search, {
ignoreQueryPrefix: true,
});
switch (__SPECIAL_TEST_PARAMETER__) {
case 'preparing-story': {
this.showPreparingStory();
this.testing = true;
break;
}
case 'preparing-docs': {
this.showPreparingDocs();
this.testing = true;
break;
}
default: // pass;
}
default: // pass;
}
}

Expand Down Expand Up @@ -114,8 +116,10 @@ export class WebView implements View<HTMLElement> {
checkIfLayoutExists(layout: keyof typeof layoutClassMap) {
if (!layoutClassMap[layout]) {
logger.warn(
dedent`The desired layout: ${layout} is not a valid option.
The possible options are: ${Object.keys(layoutClassMap).join(', ')}, none.`
dedent`
The desired layout: ${layout} is not a valid option.
The possible options are: ${Object.keys(layoutClassMap).join(', ')}, none.
`
);
}
}
Expand Down