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

Add new SET_INDEX event #19590

Merged
Show file tree
Hide file tree
Changes from 2 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
30 changes: 6 additions & 24 deletions code/lib/api/src/lib/stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ export const denormalizeStoryParameters = ({

const TITLE_PATH_SEPARATOR = /\s*\/\s*/;

// We used to received a bit more data over the channel on the SET_STORIES event, including
// We recieve a bit more data over the channel on the SET_INDEX event (v6 store), including
// the full parameters for each story.
type PreparedIndexEntry = IndexEntry & {
parameters?: Parameters;
Expand All @@ -265,19 +265,14 @@ export const transformSetStoriesStoryDataToStoriesHash = (
data: SetStoriesStoryData,
{ provider, docsOptions }: { provider: Provider; docsOptions: DocsOptions }
) =>
transformStoryIndexToStoriesHash(
transformSetStoriesStoryDataToPreparedStoryIndex(data, { docsOptions }),
{
provider,
docsOptions,
}
);
transformStoryIndexToStoriesHash(transformSetStoriesStoryDataToPreparedStoryIndex(data), {
provider,
docsOptions,
});

const transformSetStoriesStoryDataToPreparedStoryIndex = (
stories: SetStoriesStoryData,
{ docsOptions }: { docsOptions: DocsOptions }
stories: SetStoriesStoryData
): PreparedStoryIndex => {
const seenTitles = new Set<ComponentTitle>();
const entries: PreparedStoryIndex['entries'] = Object.entries(stories).reduce(
(acc, [id, story]) => {
if (!story) return acc;
Expand All @@ -296,19 +291,6 @@ const transformSetStoriesStoryDataToPreparedStoryIndex = (
...base,
};
} else {
if (!seenTitles.has(base.title) && docsOptions.docsPage) {
const name = docsOptions.defaultName;
const docsId = toId(story.componentId || base.title, name);
seenTitles.add(base.title);
acc[docsId] = {
type: 'docs',
storiesImports: [],
...base,
id: docsId,
name,
};
}

const { argTypes, args, initialArgs } = story;
acc[id] = {
type: 'story',
Expand Down
54 changes: 26 additions & 28 deletions code/lib/api/src/modules/stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
STORY_CHANGED,
SELECT_STORY,
SET_STORIES,
SET_INDEX,
STORY_SPECIFIED,
STORY_INDEX_INVALIDATED,
CONFIG_ERROR,
Expand All @@ -26,6 +27,7 @@ import {
HashEntry,
LeafEntry,
addPreparedStories,
PreparedStoryIndex,
} from '../lib/stories';

import type {
Expand Down Expand Up @@ -67,7 +69,7 @@ export interface SubAPI {
obj?: { ref?: string; viewMode?: ViewMode }
) => void;
getCurrentStoryData: () => LeafEntry;
setStories: (stories: SetStoriesStoryData, failed?: Error) => Promise<void>;
setIndex: (index: PreparedStoryIndex) => Promise<void>;
jumpToComponent: (direction: Direction) => void;
jumpToStory: (direction: Direction) => void;
getData: (storyId: StoryId, refId?: string) => LeafEntry;
Expand All @@ -87,8 +89,7 @@ export interface SubAPI {
direction: Direction,
toSiblingGroup: boolean // when true, skip over leafs within the same group
): StoryId;
fetchStoryList: () => Promise<void>;
setStoryList: (storyList: StoryIndex) => Promise<void>;
fetchIndex: () => Promise<void>;
updateStory: (storyId: StoryId, update: StoryUpdate, ref?: ComposedRef) => Promise<void>;
}

Expand Down Expand Up @@ -206,19 +207,6 @@ export const init: ModuleFn<SubAPI, SubState, true> = ({
api.selectStory(result, undefined, { ref: refId });
}
},
setStories: async (input, error) => {
// Now create storiesHash by reordering the above by group
const hash = transformSetStoriesStoryDataToStoriesHash(input, {
provider,
docsOptions,
});

await store.setState({
storiesHash: hash,
storiesConfigured: true,
storiesFailed: error,
});
},
selectFirstStory: () => {
const { storiesHash } = store.getState();
const firstStory = Object.keys(storiesHash).find((id) => storiesHash[id].type === 'story');
Expand Down Expand Up @@ -330,7 +318,7 @@ export const init: ModuleFn<SubAPI, SubState, true> = ({
options: { target: refId },
});
},
fetchStoryList: async () => {
fetchIndex: async () => {
try {
const result = await fetch(STORY_INDEX_PATH);
if (result.status !== 200) throw new Error(await result.text());
Expand All @@ -343,15 +331,18 @@ export const init: ModuleFn<SubAPI, SubState, true> = ({
return;
}

await fullAPI.setStoryList(storyIndex);
await fullAPI.setIndex(storyIndex);
} catch (err) {
store.setState({
storiesConfigured: true,
storiesFailed: err,
});
}
},
setStoryList: async (storyIndex: StoryIndex) => {
// The story index we receive on SET_INDEX is "prepared" in that it has parameters
// The story index we receive on fetchStoryIndex is not, but all the prepared fields are optional
// so we can cast one to the other easily enough
setIndex: async (storyIndex: PreparedStoryIndex) => {
const newHash = transformStoryIndexToStoriesHash(storyIndex, {
provider,
docsOptions,
Expand Down Expand Up @@ -463,19 +454,26 @@ export const init: ModuleFn<SubAPI, SubState, true> = ({
}
});

fullAPI.on(SET_STORIES, function handler(data: SetStoriesPayload) {
fullAPI.on(SET_INDEX, function handler(index: PreparedStoryIndex) {
const { ref } = getEventMetadata(this, fullAPI);
const setStoriesData = data.v ? denormalizeStoryParameters(data) : data.stories;

if (!ref) {
if (!data.v) {
throw new Error('Unexpected legacy SET_STORIES event from local source');
}

fullAPI.setStories(setStoriesData);
fullAPI.setIndex(index);
const options = fullAPI.getCurrentParameter('options');
checkDeprecatedOptionParameters(options);
fullAPI.setOptions(options);
} else {
fullAPI.setRef(ref.id, { ...ref, storyIndex: index }, true);
}
});

// For composition back-compatibilty
fullAPI.on(SET_STORIES, function handler(data: SetStoriesPayload) {
const { ref } = getEventMetadata(this, fullAPI);
const setStoriesData = data.v ? denormalizeStoryParameters(data) : data.stories;

if (!ref) {
throw new Error('Cannot call SET_STORIES for local frame');
} else {
fullAPI.setRef(ref.id, { ...ref, setStoriesData }, true);
}
Expand Down Expand Up @@ -520,8 +518,8 @@ export const init: ModuleFn<SubAPI, SubState, true> = ({
});

if (FEATURES?.storyStoreV7) {
provider.serverChannel?.on(STORY_INDEX_INVALIDATED, () => fullAPI.fetchStoryList());
await fullAPI.fetchStoryList();
provider.serverChannel?.on(STORY_INDEX_INVALIDATED, () => fullAPI.fetchIndex());
await fullAPI.fetchIndex();
}
};

Expand Down
Loading