-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor story_store
- Loading branch information
Showing
8 changed files
with
190 additions
and
112 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { history, document } from 'global'; | ||
import qs from 'qs'; | ||
import { toId } from '@storybook/router/utils'; | ||
|
||
export function pathToId(path) { | ||
const match = (path || '').match(/^\/story\/(.+)/); | ||
if (!match) { | ||
throw new Error(`Invalid path '${path}', must start with '/story/'`); | ||
} | ||
return match[1]; | ||
} | ||
|
||
export const setPath = ({ storyId }) => { | ||
const { path, selectedKind, selectedStory, ...rest } = qs.parse(document.location.search, { | ||
ignoreQueryPrefix: true, | ||
}); | ||
const newPath = `${document.location.pathname}?${qs.stringify({ ...rest, id: storyId })}`; | ||
history.replaceState({}, '', newPath); | ||
}; | ||
|
||
export const getIdFromLegacyQuery = ({ path, selectedKind, selectedStory }) => | ||
(path && pathToId(path)) || (selectedKind && selectedStory && toId(selectedKind, selectedStory)); | ||
|
||
export const parseQueryParameters = search => { | ||
const { id } = qs.parse(search, { ignoreQueryPrefix: true }); | ||
return id; | ||
}; | ||
|
||
export const initializePath = () => { | ||
const query = qs.parse(document.location.search, { ignoreQueryPrefix: true }); | ||
let { id: storyId } = query; | ||
if (!storyId) { | ||
storyId = getIdFromLegacyQuery(query); | ||
if (storyId) { | ||
setPath({ storyId }); | ||
} | ||
} | ||
return { storyId }; | ||
}; |
Oops, something went wrong.