Skip to content

Commit

Permalink
Allow browsing to a kind and get the first story (#6720)
Browse files Browse the repository at this point in the history
Allow browsing to a kind and get the first story
  • Loading branch information
shilman authored May 6, 2019
2 parents 87fd9b9 + 1e72338 commit 7ad8699
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/api/src/modules/stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,16 @@ Did you create a path that uses the separator char accidentally, such as 'Vue <d

const { storyId, viewMode } = store.getState();

if (!storyId || !storiesHash[storyId]) {
if (storyId && storyId.match(/--\*$/)) {
const idStart = storyId.slice(0, -1); // drop the * at the end
const firstKindLeaf = Object.values(storiesHash).find(
(s: Story | Group) => !s.children && s.id.substring(0, idStart.length) === idStart
);

if (viewMode && firstKindLeaf) {
navigate(`/${viewMode}/${firstKindLeaf.id}`);
}
} else if (!storyId || storyId === '*' || !storiesHash[storyId]) {
// when there's no storyId or the storyId item doesn't exist
// we pick the first leaf and navigate
const firstLeaf = Object.values(storiesHash).find((s: Story | Group) => !s.children);
Expand Down
4 changes: 4 additions & 0 deletions lib/api/src/modules/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ const initialUrlSupport = ({ navigate, location, path }: Module) => {
if (selectedKind && selectedStory) {
const storyId = toId(selectedKind, selectedStory);
setTimeout(() => navigate(`/story/${storyId}`, { replace: true }), 1);
} else if (selectedKind) {
// Create a "storyId" of the form `kind-sanitized--*`
const standInId = toId(selectedKind, 'star').replace(/star$/, '*');
setTimeout(() => navigate(`/story/${standInId}`, { replace: true }), 1);
} else if (!queryPath || queryPath === '/') {
setTimeout(() => navigate(`/story/*`, { replace: true }), 1);
} else if (Object.keys(query).length > 1) {
Expand Down

1 comment on commit 7ad8699

@vercel
Copy link

@vercel vercel bot commented on 7ad8699 May 6, 2019

Choose a reason for hiding this comment

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

Please sign in to comment.