Skip to content

Commit

Permalink
feat(frontend): add storybook for development and design (#246)
Browse files Browse the repository at this point in the history
  • Loading branch information
634750802 authored Aug 26, 2024
1 parent e0c3c45 commit 4c65b13
Show file tree
Hide file tree
Showing 9 changed files with 4,415 additions and 105 deletions.
5 changes: 4 additions & 1 deletion frontend/app/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"extends": "next/core-web-vitals"
"extends": [
"next/core-web-vitals",
"plugin:storybook/recommended"
]
}
2 changes: 2 additions & 0 deletions frontend/app/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,5 @@ public/widget.js
public/robots.txt
public/sitemap.xml
public/sitemap-*.xml

*storybook.log
21 changes: 21 additions & 0 deletions frontend/app/.storybook/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type { StorybookConfig } from '@storybook/nextjs';

const config: StorybookConfig = {
stories: [
'../src/!(pages)/**/*.mdx',
'../src/!(pages)/**/*.stories.@(js|jsx|mjs|ts|tsx)',
],
addons: [
'@storybook/addon-onboarding',
'@storybook/addon-links',
'@storybook/addon-essentials',
'@chromatic-com/storybook',
'@storybook/addon-interactions',
],
framework: {
name: '@storybook/nextjs',
options: {},
},
staticDirs: ['../public'],
};
export default config;
15 changes: 15 additions & 0 deletions frontend/app/.storybook/preview.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type { Preview } from "@storybook/react";
import '../src/app/globals.css';

const preview: Preview = {
parameters: {
controls: {
matchers: {
color: /(background|color)$/i,
date: /Date$/i,
},
},
},
};

export default preview;
29 changes: 28 additions & 1 deletion frontend/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
"build:standalone": "STANDALONE=1 next build --no-lint && next-sitemap",
"start": "next start",
"lint": "next lint",
"test": "NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules\" jest"
"test": "NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules\" jest",
"storybook": "storybook dev -p 6006",
"build-storybook": "storybook build"
},
"dependencies": {
"@hookform/resolvers": "^3.9.0",
Expand Down Expand Up @@ -73,7 +75,16 @@
"zod": "^3.23.8"
},
"devDependencies": {
"@chromatic-com/storybook": "^1.7.0",
"@jest/globals": "^29.7.0",
"@storybook/addon-essentials": "^8.2.9",
"@storybook/addon-interactions": "^8.2.9",
"@storybook/addon-links": "^8.2.9",
"@storybook/addon-onboarding": "^8.2.9",
"@storybook/blocks": "^8.2.9",
"@storybook/nextjs": "^8.2.9",
"@storybook/react": "^8.2.9",
"@storybook/test": "^8.2.9",
"@tanstack/react-table": "^8.20.1",
"@tanstack/table-core": "^8.20.1",
"@testing-library/jest-dom": "^6.4.8",
Expand All @@ -92,6 +103,7 @@
"d3": "^7.9.0",
"eslint": "^8.57.0",
"eslint-config-next": "14.2.5",
"eslint-plugin-storybook": "^0.8.0",
"eventemitter3": "^5.0.1",
"fast-equals": "^5.0.1",
"filesize": "^10.1.4",
Expand All @@ -112,12 +124,27 @@
"react-lowlight": "^3.0.0",
"react-textarea-autosize": "^8.5.3",
"sass": "^1.77.8",
"storybook": "^8.2.9",
"swr": "^2.2.5",
"tailwindcss": "^3.4.9",
"ts-jest": "^29.2.4",
"ts-node": "^10.9.2",
"typescript": "^5.5.4",
"undici": "^6.19.7",
"yaml-loader": "^0.8.1"
},
"imports": {
"#*": {
"storybook": [
"./src/*.mock",
"./src/*.mock.ts",
"./src/*.mock.tsx"
],
"default": [
"./src/*",
"./src/*.ts",
"./src/*.tsx"
]
}
}
}
6 changes: 6 additions & 0 deletions frontend/app/src/experimental/chat-verify-service/api.mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { fn } from '@storybook/test';
import * as api from './api';

export * from './api';
export const verify = fn(api.verify).mockName('verify');
export const getVerify = fn(api.getVerify).mockName('getVerify');
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { AuthProvider } from '@/components/auth/AuthProvider';
import { ChatMessageController } from '@/components/chat/chat-message-controller';
import { getVerify, verify, VerifyState } from '@/experimental/chat-verify-service/api.mock';
import type { Meta, StoryObj } from '@storybook/react';
import { mutate } from 'swr';
import { MessageVerify } from './message-verify';

// More on how to set up stories at: https://storybook.js.org/docs/writing-stories#default-export
const meta = {
title: 'Experimental/MessageVerify',
component: undefined,
parameters: {
// Optional parameter to center the component in the Canvas. More info: https://storybook.js.org/docs/configure/story-layout
layout: 'centered',
},
// This component will have an automatically generated Autodocs entry: https://storybook.js.org/docs/writing-docs/autodocs
tags: [],
// More on argTypes: https://storybook.js.org/docs/api/argtypes
argTypes: {
// user: { description: 'User message controller' },
// assistant: { description: 'Assistant message controller' },
},
// Use `fn` to spy on the onClick arg, which will appear in the actions panel once invoked: https://storybook.js.org/docs/essentials/actions#action-args
args: {},
beforeEach: async () => {
await mutate(() => true, undefined, { revalidate: true });
verify.mockReturnValue(Promise.resolve({ job_id: '000' }));
localStorage.setItem('tidbai.experimental.verify-chat-message', 'on');
return () => {
verify.mockReset();
localStorage.removeItem('tidbai.experimental.verify-chat-message');
};
},
render (_, { id }) {
return (
<AuthProvider key={id} isLoading={false} isValidating={false} me={{ email: '[email protected]', is_active: true, is_superuser: true, is_verified: true, id: '000' }} reload={() => {}}>
<MessageVerify
user={new ChatMessageController({ finished_at: new Date(), id: 0, role: 'user', content: 'Question' } as any, undefined)}
assistant={new ChatMessageController({ finished_at: new Date(), id: 1, role: 'assistant', content: 'Answer' } as any, undefined)}
/>
</AuthProvider>
);
},
} satisfies Meta<any>;

export default meta;
type Story = StoryObj<typeof meta>;

// More on writing stories with args: https://storybook.js.org/docs/writing-stories/args
export const Verified: Story = {
beforeEach: () => {
getVerify.mockReturnValue(Promise.resolve({
status: VerifyState.SUCCESS,
message: 'This is a success message returned from server',
runs: [
{ sql: 'SOME A FROM B FROM C FROM D FROM E FROM F FROM G', results: [], success: true, explanation: 'some description for this SQL' },
],
}));
},
};

export const Validating: Story = {
beforeEach: () => {
getVerify.mockReturnValue(Promise.resolve({
status: VerifyState.VALIDATING,
message: 'This is a validating message returned from server',
runs: [],
}));
},
};

export const Failed: Story = {
beforeEach: () => {
getVerify.mockReturnValue(Promise.resolve({
status: VerifyState.FAILED,
message: 'This is a failed message returned from server',
runs: [
{ sql: 'SOME A FROM B FROM C FROM D FROM E FROM F FROM G', results: [], success: true, explanation: 'Some description for this SQL' },
{ sql: 'SOME A FROM B FROM C FROM D FROM E FROM F FROM G', results: [], success: false, explanation: 'Another description for this SQL', sql_error_code: 8848, sql_error_message: 'TiDB Error Message', warnings: [] },
],
}));
},
};

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { ChatMessageController } from '@/components/chat/chat-message-contr
import { isNotFinished } from '@/components/chat/utils';
import { Button } from '@/components/ui/button';
import { Collapsible, CollapsibleContent, CollapsibleTrigger } from '@/components/ui/collapsible';
import { getVerify, isFinalVerifyState, isVisibleVerifyState, verify, VerifyState } from '@/experimental/chat-verify-service/api';
import { getVerify, isFinalVerifyState, isVisibleVerifyState, verify, VerifyState } from '#experimental/chat-verify-service/api';
import { CheckCircle2Icon, CheckIcon, ChevronDownIcon, Loader2Icon, XIcon } from 'lucide-react';
import { InformationCircleIcon } from 'nextra/icons';
import { useEffect, useState } from 'react';
Expand Down
Loading

0 comments on commit 4c65b13

Please sign in to comment.