Skip to content

Commit 04d6f2d

Browse files
fix: storybook
1 parent 2d30881 commit 04d6f2d

15 files changed

Lines changed: 486 additions & 435 deletions

File tree

apps/csk-marketing-site/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@uniformdev/csk-marketing-site",
3-
"version": "7.0.0",
3+
"version": "7.0.1",
44
"private": true,
55
"engines": {
66
"yarn": "please-use-npm",

apps/csk-storybook/.storybook/main.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { createRequire } from 'node:module';
22
import { dirname, join } from 'node:path';
33
import { fileURLToPath } from 'node:url';
4-
import type { Configuration } from 'webpack';
4+
import webpack, { type Configuration } from 'webpack';
55
import type { StorybookConfig } from '@storybook/nextjs';
66

77
const require = createRequire(import.meta.url);
@@ -38,6 +38,16 @@ const config: StorybookConfig = {
3838

3939
config.resolve.modules = [...modulesArray, join(appDir, 'node_modules'), join(rootDir, 'node_modules')];
4040

41+
// Inject fake Uniform credentials so the SDK's ApiClient constructs without a real key.
42+
// The manifest request these would authenticate is mocked in .storybook/preview.ts.
43+
config.plugins = config.plugins ?? [];
44+
config.plugins.push(
45+
new webpack.DefinePlugin({
46+
'process.env.UNIFORM_API_KEY': JSON.stringify('storybook-fake-key'),
47+
'process.env.UNIFORM_PROJECT_ID': JSON.stringify('storybook-fake-project'),
48+
})
49+
);
50+
4151
return config;
4252
},
4353
};

apps/csk-storybook/.storybook/preview.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,38 @@ import '../src/styles/dimensions.css';
55
import '../src/styles/fonts.css';
66
import '../src/styles/borders.css';
77

8+
// The Uniform SDK's UniformContext fetches the Context manifest while rendering a composition.
9+
// Stories use fake data and no real API key, so intercept that request and return an empty
10+
// manifest. All other requests pass through untouched.
11+
type MockableFetch = typeof window.fetch & { __uniformMock?: boolean };
12+
13+
if (typeof window !== 'undefined' && !(window.fetch as MockableFetch).__uniformMock) {
14+
const originalFetch = window.fetch.bind(window);
15+
const mockFetch: MockableFetch = (input, init) => {
16+
const url = typeof input === 'string' ? input : input instanceof Request ? input.url : String(input);
17+
if (url.includes('/api/v2/manifest')) {
18+
return Promise.resolve(
19+
new Response(JSON.stringify({ project: { pz: { sig: {}, enr: {}, agg: {} }, test: {} } }), {
20+
status: 200,
21+
headers: { 'content-type': 'application/json' },
22+
})
23+
);
24+
}
25+
return originalFetch(input, init);
26+
};
27+
mockFetch.__uniformMock = true;
28+
window.fetch = mockFetch;
29+
}
30+
831
const preview: Preview = {
932
parameters: {
33+
// Components rendered via UniformComposition use App Router hooks (next/navigation).
34+
// appDirectory: true makes @storybook/nextjs initialize the navigation mock for every
35+
// story — required for the production build, where it is otherwise not created in time.
36+
nextjs: {
37+
appDirectory: true,
38+
navigation: { pathname: '/', query: {} },
39+
},
1040
controls: {
1141
matchers: {
1242
color: /(background|color)$/i,

apps/csk-storybook/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@uniformdev/csk-storybook",
3-
"version": "7.0.0",
3+
"version": "7.0.1",
44
"description": "CSK vNext Storybook is an interactive Storybook build showcasing components from the CSK vNext component starter kit. It provides detailed documentation, live previews, and testing capabilities for easy integration into your projects.",
55
"main": "index.js",
66
"scripts": {
@@ -23,6 +23,7 @@
2323
"@uniformdev/csk-components": "*",
2424
"@uniformdev/design-extensions-tools": "*",
2525
"@uniformdev/next-app-router": "^20.66.2",
26+
"@uniformdev/next-app-router-shared": "^20.66.2",
2627
"eslint-plugin-storybook": "^10.4.0",
2728
"next": "^16.2.6",
2829
"next-themes": "^0.4.6",

apps/csk-storybook/src/utils/index.ts

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { ComponentInstance } from '@uniformdev/canvas';
22

3-
import { resolveRouteFromCode } from '@uniformdev/next-app-router';
3+
import { ResolveRouteFunction, resolveRouteFromCode } from '@uniformdev/next-app-router';
4+
import { serializeEvaluationResult } from '@uniformdev/next-app-router-shared';
45

56
interface UniformMockParam {
67
type: string;
@@ -25,15 +26,22 @@ export const createFakeCompositionData = (
2526
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2627
params: Record<string, any>,
2728
slots?: Record<string, ComponentInstance[]>
28-
): Awaited<ReturnType<typeof resolveRouteFromCode>> => {
29-
return {
30-
pageState: {
31-
compositionState: 64,
32-
routePath: 'fake-route-path',
33-
components: {},
34-
keys: {},
35-
releaseId: 'fake-release-id',
36-
},
29+
): { code: string; resolveRoute: ResolveRouteFunction } => {
30+
const pageState = {
31+
compositionState: 64,
32+
routePath: 'fake-route-path',
33+
components: {},
34+
keys: {},
35+
releaseId: 'fake-release-id',
36+
rules: undefined,
37+
defaultConsent: undefined,
38+
previewMode: undefined,
39+
locale: undefined,
40+
isPrefetch: undefined,
41+
};
42+
43+
const result: Awaited<ReturnType<typeof resolveRouteFromCode>> = {
44+
pageState,
3745
route: {
3846
type: 'composition',
3947
matchedRoute: '/',
@@ -54,6 +62,11 @@ export const createFakeCompositionData = (
5462
pattern: false,
5563
},
5664
},
57-
code: 'fake-code',
65+
code: serializeEvaluationResult({ payload: pageState }),
66+
};
67+
68+
return {
69+
code: result.code,
70+
resolveRoute: async () => result,
5871
};
5972
};

apps/csk/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@uniformdev/component-starter-kit",
3-
"version": "7.0.0",
3+
"version": "7.0.1",
44
"private": true,
55
"engines": {
66
"yarn": "please-use-npm",

0 commit comments

Comments
 (0)