2022-02-25 23:11:22 +01:00
|
|
|
import React, { useEffect, useState } from 'react';
|
2021-02-08 16:45:06 +01:00
|
|
|
import { addDecorator, addParameters } from '@storybook/react';
|
2021-05-25 02:20:09 +02:00
|
|
|
import { action } from '@storybook/addon-actions';
|
2021-02-08 16:45:06 +01:00
|
|
|
import { Provider } from 'react-redux';
|
2021-04-28 21:53:59 +02:00
|
|
|
import configureStore from '../ui/store/store';
|
|
|
|
import '../ui/css/index.scss';
|
2021-02-08 16:45:06 +01:00
|
|
|
import localeList from '../app/_locales/index.json';
|
|
|
|
import * as allLocales from './locales';
|
|
|
|
import { I18nProvider, LegacyI18nProvider } from './i18n';
|
2021-10-29 19:22:07 +02:00
|
|
|
import MetaMetricsProviderStorybook from './metametrics';
|
2021-04-28 21:53:59 +02:00
|
|
|
import testData from './test-data.js';
|
2021-10-29 19:22:07 +02:00
|
|
|
import { Router } from 'react-router-dom';
|
|
|
|
import { createBrowserHistory } from 'history';
|
|
|
|
import { _setBackgroundConnection } from '../ui/store/actions';
|
2021-12-07 00:55:21 +01:00
|
|
|
import MetaMaskStorybookTheme from './metamask-storybook-theme';
|
2022-02-25 23:11:22 +01:00
|
|
|
import addons from '@storybook/addons';
|
2018-05-03 19:51:15 +02:00
|
|
|
|
2020-02-26 18:40:53 +01:00
|
|
|
addParameters({
|
2020-11-30 21:10:46 +01:00
|
|
|
backgrounds: {
|
2022-02-25 23:11:22 +01:00
|
|
|
default: 'default',
|
2020-11-30 21:10:46 +01:00
|
|
|
values: [
|
2022-02-25 23:11:22 +01:00
|
|
|
{ name: 'default', value: 'var(--color-background-default)' },
|
|
|
|
{ name: 'alternative', value: 'var(--color-background-alternative)' },
|
2020-11-30 21:10:46 +01:00
|
|
|
],
|
2021-02-08 16:45:06 +01:00
|
|
|
},
|
2021-12-07 00:55:21 +01:00
|
|
|
docs: {
|
|
|
|
theme: MetaMaskStorybookTheme,
|
|
|
|
},
|
2021-12-01 20:27:57 +01:00
|
|
|
options: {
|
|
|
|
storySort: {
|
2022-02-23 03:26:11 +01:00
|
|
|
order: [
|
|
|
|
'Getting Started',
|
2022-07-18 17:23:27 +02:00
|
|
|
'Foundations',
|
2022-02-23 03:26:11 +01:00
|
|
|
'Components',
|
|
|
|
['UI', 'App'],
|
|
|
|
'Pages',
|
|
|
|
],
|
2021-12-01 20:27:57 +01:00
|
|
|
},
|
|
|
|
},
|
2021-02-08 16:45:06 +01:00
|
|
|
});
|
2020-02-26 18:40:53 +01:00
|
|
|
|
2021-02-06 03:28:54 +01:00
|
|
|
export const globalTypes = {
|
|
|
|
locale: {
|
|
|
|
name: 'Locale',
|
|
|
|
description: 'internationalization locale',
|
|
|
|
defaultValue: 'en',
|
|
|
|
toolbar: {
|
|
|
|
icon: 'globe',
|
|
|
|
items: localeList.map(({ code, name }) => {
|
2021-02-08 16:45:06 +01:00
|
|
|
return { value: code, right: code, title: name };
|
|
|
|
}),
|
2021-02-06 03:28:54 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-09-08 01:51:41 +02:00
|
|
|
export const getNewState = (state, props) => {
|
|
|
|
return Object.assign(state, props);
|
2021-10-29 19:22:07 +02:00
|
|
|
};
|
2021-09-08 01:51:41 +02:00
|
|
|
|
2021-05-25 02:20:09 +02:00
|
|
|
export const store = configureStore(testData);
|
|
|
|
const history = createBrowserHistory();
|
2021-10-29 19:22:07 +02:00
|
|
|
const proxiedBackground = new Proxy(
|
|
|
|
{},
|
|
|
|
{
|
2021-05-25 02:20:09 +02:00
|
|
|
get(_, method) {
|
2021-10-29 19:22:07 +02:00
|
|
|
return function () {
|
|
|
|
action(`Background call: ${method}`)();
|
|
|
|
return new Promise(() => {});
|
|
|
|
};
|
|
|
|
},
|
|
|
|
},
|
|
|
|
);
|
|
|
|
_setBackgroundConnection(proxiedBackground);
|
2020-03-17 20:15:53 +01:00
|
|
|
|
2021-02-06 03:28:54 +01:00
|
|
|
const metamaskDecorator = (story, context) => {
|
2022-02-25 23:11:22 +01:00
|
|
|
const [isDark, setDark] = useState(false);
|
|
|
|
const channel = addons.getChannel();
|
2021-02-08 16:45:06 +01:00
|
|
|
const currentLocale = context.globals.locale;
|
|
|
|
const current = allLocales[currentLocale];
|
2022-02-25 23:11:22 +01:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
channel.on('DARK_MODE', setDark);
|
|
|
|
return () => channel.off('DARK_MODE', setDark);
|
|
|
|
}, [channel, setDark]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const currentTheme = document.documentElement.getAttribute('data-theme');
|
|
|
|
|
|
|
|
if (!currentTheme)
|
|
|
|
document.documentElement.setAttribute('data-theme', 'light');
|
|
|
|
|
|
|
|
if (currentTheme === 'light' && isDark) {
|
|
|
|
document.documentElement.setAttribute('data-theme', 'dark');
|
|
|
|
} else if (currentTheme === 'dark' && !isDark) {
|
|
|
|
document.documentElement.setAttribute('data-theme', 'light');
|
|
|
|
}
|
|
|
|
}, [isDark]);
|
|
|
|
|
2021-02-06 03:28:54 +01:00
|
|
|
return (
|
|
|
|
<Provider store={store}>
|
2021-05-25 02:20:09 +02:00
|
|
|
<Router history={history}>
|
|
|
|
<MetaMetricsProviderStorybook>
|
|
|
|
<I18nProvider
|
|
|
|
currentLocale={currentLocale}
|
|
|
|
current={current}
|
|
|
|
en={allLocales.en}
|
|
|
|
>
|
2021-10-29 19:22:07 +02:00
|
|
|
<LegacyI18nProvider>{story()}</LegacyI18nProvider>
|
2021-05-25 02:20:09 +02:00
|
|
|
</I18nProvider>
|
|
|
|
</MetaMetricsProviderStorybook>
|
|
|
|
</Router>
|
2021-02-06 03:28:54 +01:00
|
|
|
</Provider>
|
2021-02-08 16:45:06 +01:00
|
|
|
);
|
|
|
|
};
|
2018-05-03 19:51:15 +02:00
|
|
|
|
2021-02-08 16:45:06 +01:00
|
|
|
addDecorator(metamaskDecorator);
|