Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Core: Allow linking to kind/component ID #7648

Merged
merged 10 commits into from
Oct 24, 2019
24 changes: 19 additions & 5 deletions lib/api/src/modules/stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,17 @@ const initStoriesApi = ({
id: toKey(name),
});

// Recursively traverse storiesHash from the initial storyId until finding
// the leaf story.
const findLeafStoryId = (storiesHash: StoriesHash, storyId: string): string => {
if (storiesHash[storyId].isLeaf) {
return storyId;
}

const childStoryId = storiesHash[storyId].children[0];
return findLeafStoryId(storiesHash, childStoryId);
};

const setStories = (input: StoriesRaw) => {
const hash: StoriesHash = {};
const storiesHashOutOfOrder = Object.values(input).reduce((acc, item) => {
Expand Down Expand Up @@ -286,11 +297,14 @@ Did you create a path that uses the separator char accidentally, such as 'Vue <d
} else if (viewMode && firstLeaf) {
navigate(`/${viewMode}/${firstLeaf.id}`);
}
} else if (storiesHash[storyId] && storiesHash[storyId].children) {
// When story exists but specific child is not specified, it routes
// to the first child of the story.
const firstLeafId = storiesHash[storyId].children[0];
navigate(`/${viewMode}/${firstLeafId}`);
} else if (storiesHash[storyId]) {
// When story exists but if it is not the leaf story, it finds the proper
// leaf story from any depth.
const firstLeafStoryId = storiesHash[storyId].isLeaf
Copy link
Member

Choose a reason for hiding this comment

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

const firstLeafStoryId = findLeafStoryId(storiesHash, storyId);

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member Author

@lonyele lonyele Sep 11, 2019

Choose a reason for hiding this comment

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

@shilman oh wait, I'm writing a test now. BTW, this comment is on the previous file change. Please refresh for the new one

? storiesHash[storyId].children[0]
: findLeafStoryId(storiesHash, storyId);

navigate(`/${viewMode}/${firstLeafStoryId}`);
}

store.setState({
Expand Down