Skip to content

Commit

Permalink
Fix - Make loki work with Storybook8
Browse files Browse the repository at this point in the history
  • Loading branch information
Benrajalu committed Mar 21, 2024
1 parent 02a68d5 commit c678ae2
Showing 1 changed file with 32 additions and 7 deletions.
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·&&⏎·····`
(window.loki && window.loki.getStorybook);
if (!getStorybook) {
throw new Error(
Expand All @@ -19,13 +20,6 @@ const getStories = async (window) => {
'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 @@ const getStories = async (window) => {
}
};

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

0 comments on commit c678ae2

Please sign in to comment.