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 - Make loki work with Storybook8 #507

Merged
merged 2 commits into from
Mar 30, 2024
Merged
Changes from 1 commit
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
39 changes: 32 additions & 7 deletions packages/browser/src/get-stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const getStories = async (window) => {
const getStorybook =
(window.__STORYBOOK_CLIENT_API__ && window.__STORYBOOK_CLIENT_API__.raw) ||
(window.__STORYBOOK_PREVIEW__ && window.__STORYBOOK_PREVIEW__.extract && window.__STORYBOOK_PREVIEW__.storyStore.raw) ||

Check failure on line 6 in packages/browser/src/get-stories.js

View workflow job for this annotation

GitHub Actions / Static analysis

Replace `·window.__STORYBOOK_PREVIEW__.extract·&&` with `⏎······window.__STORYBOOK_PREVIEW__.extract·&&⏎·····`
oblador marked this conversation as resolved.
Show resolved Hide resolved
(window.loki && window.loki.getStorybook);
if (!getStorybook) {
throw new Error(
Expand All @@ -19,13 +20,6 @@
'storySource',
];

if (
window.__STORYBOOK_CLIENT_API__.storyStore &&
window.__STORYBOOK_CLIENT_API__.storyStore.cacheAllCSFFiles
) {
await window.__STORYBOOK_CLIENT_API__.storyStore.cacheAllCSFFiles();
}

const isSerializable = (value) => {
try {
JSON.stringify(value);
Expand All @@ -35,6 +29,37 @@
}
};

if (window.__STORYBOOK_PREVIEW__ && window.__STORYBOOK_PREVIEW__.extract) {
// New official API to extract stories from preview
await window.__STORYBOOK_PREVIEW__.extract();

// Deprecated, will be removed in V9
const stories = window.__STORYBOOK_PREVIEW__.storyStore.raw();

return stories
.map((component) => ({
id: component.id,
kind: component.kind,
story: component.story,
parameters: Object.fromEntries(
Object.entries(component.parameters || {}).filter(
([key, value]) =>
!key.startsWith('__') &&
!blockedParams.includes(key) &&
isSerializable(value)
)
),
}))
.filter(({ parameters }) => !parameters.loki || !parameters.loki.skip);
}

if (
window.__STORYBOOK_CLIENT_API__.storyStore &&
window.__STORYBOOK_CLIENT_API__.storyStore.cacheAllCSFFiles
) {
await window.__STORYBOOK_CLIENT_API__.storyStore.cacheAllCSFFiles();
}

return getStorybook()
.map((component) => ({
id: component.id,
Expand Down
Loading