AFFiNE/apps/storybook/.storybook/preview.tsx

95 lines
2.5 KiB
TypeScript
Raw Normal View History

import 'ses';
2023-04-20 22:31:54 +03:00
import '@affine/component/theme/global.css';
import '@affine/component/theme/theme.css';
2023-08-15 23:34:02 +03:00
import '@toeverything/components/style.css';
import { createI18n } from '@affine/i18n';
import { ThemeProvider, useTheme } from 'next-themes';
import { useDarkMode } from 'storybook-dark-mode';
2023-08-15 23:34:02 +03:00
import { AffineContext } from '@affine/component/context';
2023-08-16 23:07:55 +03:00
import useSWR from 'swr';
import type { Decorator } from '@storybook/react';
2023-08-18 22:50:35 +03:00
import { createStore } from 'jotai/vanilla';
import { _setCurrentStore } from '@toeverything/infra/atom';
import { setupGlobal } from '@affine/env/global';
2023-04-20 22:31:54 +03:00
2023-08-18 22:50:35 +03:00
setupGlobal();
export const parameters = {
backgrounds: { disable: true },
actions: { argTypesRegex: '^on[A-Z].*' },
controls: {
matchers: {
color: /(background|color)$/i,
date: /Date$/,
},
},
};
2023-08-16 23:07:55 +03:00
const i18n = createI18n();
const withI18n: Decorator = (Story, context) => {
const locale = context.globals.locale;
useSWR(
locale,
async () => {
await i18n.changeLanguage(locale);
},
{
suspense: true,
}
);
return <Story {...context} />;
};
2023-08-16 23:07:55 +03:00
const ThemeChange = () => {
2023-04-20 22:31:54 +03:00
const isDark = useDarkMode();
const theme = useTheme();
2023-08-16 23:07:55 +03:00
if (theme.resolvedTheme === 'dark' && !isDark) {
theme.setTheme('light');
} else if (theme.resolvedTheme === 'light' && isDark) {
theme.setTheme('dark');
}
2023-04-20 22:31:54 +03:00
return null;
};
2023-08-19 20:30:24 +03:00
const storeMap = new Map<string, ReturnType<typeof createStore>>();
2023-08-23 05:56:47 +03:00
const bootstrapPluginSystemPromise = import(
'@affine/core/bootstrap/register-plugins'
).then(({ bootstrapPluginSystem }) => bootstrapPluginSystem);
const setupPromise = import('@affine/core/bootstrap/setup').then(
({ setup }) => setup
);
2023-08-16 23:07:55 +03:00
const withContextDecorator: Decorator = (Story, context) => {
2023-08-18 22:50:35 +03:00
const { data: store } = useSWR(
context.id,
async () => {
2023-08-19 20:30:24 +03:00
if (storeMap.has(context.id)) {
return storeMap.get(context.id);
}
2023-08-18 22:50:35 +03:00
localStorage.clear();
const store = createStore();
_setCurrentStore(store);
2023-08-23 05:56:47 +03:00
const setup = await setupPromise;
2023-08-18 22:50:35 +03:00
await setup(store);
2023-08-23 05:56:47 +03:00
const bootstrapPluginSystem = await bootstrapPluginSystemPromise;
2023-08-18 22:50:35 +03:00
await bootstrapPluginSystem(store);
2023-08-19 20:30:24 +03:00
storeMap.set(context.id, store);
2023-08-18 22:50:35 +03:00
return store;
},
{
suspense: true,
}
);
2023-08-16 23:07:55 +03:00
return (
<ThemeProvider>
2023-08-18 22:50:35 +03:00
<AffineContext store={store}>
2023-08-16 23:07:55 +03:00
<ThemeChange />
<Story {...context} />
</AffineContext>
</ThemeProvider>
);
};
export const decorators = [withContextDecorator, withI18n];