Skip to content

Commit 146d0ed

Browse files
committed
Add storybook
When working on UI elements it is a lot easier to iterate and test in storybook where you can put up the interesting cases as stories and browse through them instead of having construct scenarios in a live running app. Signed-off-by: Dobes Vandermeer <dobes.vandermeer@newsela.com>
1 parent 8e680af commit 146d0ed

33 files changed

+6850
-1289
lines changed

ui/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,6 @@ yarn-error.log*
3939
# typescript
4040
*.tsbuildinfo
4141
next-env.d.ts
42+
43+
# storybook
44+
/storybook-static

ui/.storybook/main.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import type { StorybookConfig } from '@storybook/nextjs-vite';
2+
3+
const config: StorybookConfig = {
4+
stories: [
5+
"../src/**/*.mdx",
6+
"../src/**/*.stories.@(js|jsx|mjs|ts|tsx)"
7+
],
8+
addons: [
9+
"@chromatic-com/storybook",
10+
"@storybook/addon-vitest",
11+
"@storybook/addon-a11y",
12+
"@storybook/addon-docs",
13+
"@storybook/addon-onboarding"
14+
],
15+
framework: "@storybook/nextjs-vite",
16+
staticDirs: ["../public"],
17+
};
18+
export default config;

ui/.storybook/preview.tsx

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import type { Preview } from '@storybook/nextjs-vite'
2+
import { worker } from '../src/mocks/browser'
3+
import React, { ReactNode } from 'react'
4+
import '../src/app/globals.css'
5+
import { AgentsContext } from '../src/components/AgentsProvider'
6+
import type { AgentsContextType } from '../src/components/AgentsProvider'
7+
import type { Agent } from '../src/types'
8+
9+
const mockContextValue: AgentsContextType = {
10+
agents: [],
11+
models: [],
12+
loading: false,
13+
error: "",
14+
tools: [],
15+
refreshAgents: async () => {},
16+
refreshModels: async () => {},
17+
refreshTools: async () => {},
18+
createNewAgent: async () => ({ message: "mock", data: {} as Agent }),
19+
updateAgent: async () => ({ message: "mock", data: {} as Agent }),
20+
getAgent: async () => null,
21+
validateAgentData: () => ({}),
22+
};
23+
24+
interface MockAgentsProviderProps {
25+
children: ReactNode;
26+
value?: Partial<AgentsContextType>;
27+
}
28+
29+
function MockAgentsProvider({ children, value }: MockAgentsProviderProps) {
30+
return (
31+
<AgentsContext.Provider value={{ ...mockContextValue, ...value }}>
32+
{children}
33+
</AgentsContext.Provider>
34+
);
35+
}
36+
37+
const preview: Preview = {
38+
beforeAll: async () => {
39+
await worker.start({ onUnhandledRequest: 'bypass' });
40+
},
41+
parameters: {
42+
nextjs: {
43+
appDirectory: true,
44+
},
45+
controls: {
46+
matchers: {
47+
color: /(background|color)$/i,
48+
date: /Date$/i,
49+
},
50+
},
51+
a11y: {
52+
test: 'todo'
53+
}
54+
},
55+
decorators: [
56+
(Story) => {
57+
document.documentElement.classList.add('dark');
58+
return (
59+
<MockAgentsProvider>
60+
<Story />
61+
</MockAgentsProvider>
62+
);
63+
},
64+
],
65+
};
66+
67+
export default preview;

ui/.storybook/vitest.setup.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import * as a11yAddonAnnotations from "@storybook/addon-a11y/preview";
2+
import { setProjectAnnotations } from '@storybook/nextjs-vite';
3+
import * as projectAnnotations from './preview';
4+
5+
// This is an important step to apply the right configuration when testing your stories.
6+
// More info at: https://storybook.js.org/docs/api/portable-stories/portable-stories-vitest#setprojectannotations
7+
setProjectAnnotations([a11yAddonAnnotations, projectAnnotations]);

ui/eslint.config.mjs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// For more info, see https://github.com/storybookjs/eslint-plugin-storybook#configuration-flat-config-format
2+
import storybook from "eslint-plugin-storybook";
3+
14
import { defineConfig, globalIgnores } from "eslint/config";
25
import nextVitals from "eslint-config-next/core-web-vitals";
36
import nextTs from "eslint-config-next/typescript";
@@ -26,7 +29,9 @@ const eslintConfig = defineConfig([
2629
"out/**",
2730
"build/**",
2831
"next-env.d.ts",
32+
"storybook-static/**",
2933
]),
34+
...storybook.configs["flat/recommended"],
3035
]);
3136

3237
export default eslintConfig;

0 commit comments

Comments
 (0)