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

fix: storysort #677

Merged
merged 8 commits into from
Feb 8, 2025
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
6 changes: 6 additions & 0 deletions examples/expo-example/.storybook/preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ const preview: Preview = {
date: /Date$/,
},
},
options: {
storySort: {
method: 'alphabetical',
order: ['Notes example', 'TextInput'],
},
},
hideFullScreenButton: false,
noSafeArea: false,
my_param: 'anything',
Expand Down
45 changes: 39 additions & 6 deletions packages/react-native/src/Start.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,17 @@ import {
composeConfigs,
addons as previewAddons,
PreviewWithSelection,
sortStoriesV7,
userOrAutoTitleFromSpecifier,
} from '@storybook/core/preview-api';
import { isExportStory, storyNameFromExport, toId } from '@storybook/csf';
// NOTE this really should be exported from preview-api, but it's not
import { createBrowserChannel } from '@storybook/core/channels';
import type { NormalizedStoriesSpecifier, StoryIndex } from '@storybook/core/types';
import type {
Addon_StorySortParameterV7,
NormalizedStoriesSpecifier,
StoryIndex,
} from '@storybook/core/types';
import type { ReactRenderer } from '@storybook/react';
import { View } from './View';

Expand All @@ -46,12 +51,14 @@ if (Platform.OS === 'web' && typeof globalThis.setImmediate === 'undefined') {
export function prepareStories({
storyEntries,
options,
storySort,
}: {
storyEntries: Array<NormalizedStoriesSpecifier & { req: any }>;
options?: ReactNativeOptions;
storySort?: Addon_StorySortParameterV7;
}) {
let index: StoryIndex = {
v: 4,
v: 5,
entries: {},
};

Expand Down Expand Up @@ -142,7 +149,20 @@ export function prepareStories({
});
});

return { index, importMap };
const sortableStories = Object.values(index.entries);

sortStoriesV7(
Copy link
Member Author

Choose a reason for hiding this comment

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

pulling in sort stories from preview api to reuse sorting code

sortableStories,
storySort,
Object.values(index.entries).map((entry) => entry.importPath)
);

const sorted = sortableStories.reduce((acc, item) => {
acc[item.id] = item;
return acc;
}, {} as StoryIndex['entries']);

return { index: { v: 5, entries: sorted }, importMap };
}

export const getProjectAnnotations = (view: View, annotations: any[]) => async () =>
Expand Down Expand Up @@ -175,7 +195,13 @@ export function start({
annotations: any[];
options?: ReactNativeOptions;
}) {
const { index, importMap } = prepareStories({ storyEntries, options });
const composedAnnotations = composeConfigs<ReactRenderer>(annotations);
Copy link
Member Author

Choose a reason for hiding this comment

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

using composeConfig lets us read the parameters synchronously


const { index, importMap } = prepareStories({
storyEntries,
options,
storySort: composedAnnotations.parameters?.options?.storySort,
});

const channel = createBrowserChannel({ page: 'preview' });

Expand Down Expand Up @@ -230,7 +256,6 @@ export function start({
...annotations,
]);

// const preview = new PreviewWithSelection<ReactRenderer>(urlStore, previewView);
const preview = new PreviewWithSelection<ReactRenderer>(
async (importPath: string) => importMap[importPath],
getProjectAnnotationsInitial,
Expand Down Expand Up @@ -258,7 +283,15 @@ export function updateView(
normalizedStories: Array<NormalizedStoriesSpecifier & { req: any }>,
options?: ReactNativeOptions
) {
const { importMap, index } = prepareStories({ storyEntries: normalizedStories, options });
const composedAnnotations = composeConfigs<ReactRenderer>(annotations);

const storySort = composedAnnotations.parameters?.options?.storySort;

const { importMap, index } = prepareStories({
storyEntries: normalizedStories,
options,
storySort,
});

viewInstance._preview.onStoriesChanged({
importFn: async (importPath: string) => importMap[importPath],
Expand Down
Loading