Skip to content

Commit dcfbcd8

Browse files
Benrajaluoblador
andauthored
Fix - Make loki work with Storybook8 (#507)
* Fix - Make loki work with Storybook8 * Fix formatting --------- Co-authored-by: Joel Arvidsson <[email protected]>
1 parent 02a68d5 commit dcfbcd8

File tree

1 file changed

+34
-7
lines changed

1 file changed

+34
-7
lines changed

packages/browser/src/get-stories.js

+34-7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
const getStories = async (window) => {
44
const getStorybook =
55
(window.__STORYBOOK_CLIENT_API__ && window.__STORYBOOK_CLIENT_API__.raw) ||
6+
(window.__STORYBOOK_PREVIEW__ &&
7+
window.__STORYBOOK_PREVIEW__.extract &&
8+
window.__STORYBOOK_PREVIEW__.storyStore.raw) ||
69
(window.loki && window.loki.getStorybook);
710
if (!getStorybook) {
811
throw new Error(
@@ -19,13 +22,6 @@ const getStories = async (window) => {
1922
'storySource',
2023
];
2124

22-
if (
23-
window.__STORYBOOK_CLIENT_API__.storyStore &&
24-
window.__STORYBOOK_CLIENT_API__.storyStore.cacheAllCSFFiles
25-
) {
26-
await window.__STORYBOOK_CLIENT_API__.storyStore.cacheAllCSFFiles();
27-
}
28-
2925
const isSerializable = (value) => {
3026
try {
3127
JSON.stringify(value);
@@ -35,6 +31,37 @@ const getStories = async (window) => {
3531
}
3632
};
3733

34+
if (window.__STORYBOOK_PREVIEW__ && window.__STORYBOOK_PREVIEW__.extract) {
35+
// New official API to extract stories from preview
36+
await window.__STORYBOOK_PREVIEW__.extract();
37+
38+
// Deprecated, will be removed in V9
39+
const stories = window.__STORYBOOK_PREVIEW__.storyStore.raw();
40+
41+
return stories
42+
.map((component) => ({
43+
id: component.id,
44+
kind: component.kind,
45+
story: component.story,
46+
parameters: Object.fromEntries(
47+
Object.entries(component.parameters || {}).filter(
48+
([key, value]) =>
49+
!key.startsWith('__') &&
50+
!blockedParams.includes(key) &&
51+
isSerializable(value)
52+
)
53+
),
54+
}))
55+
.filter(({ parameters }) => !parameters.loki || !parameters.loki.skip);
56+
}
57+
58+
if (
59+
window.__STORYBOOK_CLIENT_API__.storyStore &&
60+
window.__STORYBOOK_CLIENT_API__.storyStore.cacheAllCSFFiles
61+
) {
62+
await window.__STORYBOOK_CLIENT_API__.storyStore.cacheAllCSFFiles();
63+
}
64+
3865
return getStorybook()
3966
.map((component) => ({
4067
id: component.id,

0 commit comments

Comments
 (0)