2021-02-08 16:45:06 +01:00
|
|
|
// import { useGlobals } from '@storybook/api';
|
2021-10-29 19:22:07 +02:00
|
|
|
const { useGlobals } = require('@storybook/api');
|
|
|
|
const React = require('react');
|
|
|
|
const { addons, types } = require('@storybook/addons');
|
|
|
|
const { Icons, IconButton } = require('@storybook/components');
|
|
|
|
const localeList = require('../../app/_locales/index.json');
|
|
|
|
const { useEffect } = React;
|
2021-02-08 16:45:06 +01:00
|
|
|
|
2021-10-29 19:22:07 +02:00
|
|
|
addons.register('i18n-party', () => {
|
|
|
|
addons.add('i18n-party', {
|
|
|
|
title: 'rotates through every i18n locale',
|
2021-02-08 16:45:06 +01:00
|
|
|
//👇 Sets the type of UI element in Storybook
|
|
|
|
type: types.TOOL,
|
|
|
|
match: () => true,
|
|
|
|
render: (...args) => {
|
|
|
|
// https://github.com/storybookjs/storybook/blob/6490a0d646dbaa293b76bbde477daca615efe789/addons/toolbars/src/components/MenuToolbar.tsx#L2
|
2021-10-29 19:22:07 +02:00
|
|
|
const [globals, updateGlobals] = useGlobals();
|
2021-02-08 16:45:06 +01:00
|
|
|
useEffect(() => {
|
2021-10-29 19:22:07 +02:00
|
|
|
if (!globals.localeParty) return;
|
2021-02-08 16:45:06 +01:00
|
|
|
const interval = setInterval((...args) => {
|
2021-10-29 19:22:07 +02:00
|
|
|
const currentIndex = localeList.findIndex(
|
|
|
|
({ code }) => code === globals.locale,
|
|
|
|
);
|
|
|
|
const nextIndex = (currentIndex + 1) % localeList.length;
|
|
|
|
const nextLocale = localeList[nextIndex].code;
|
|
|
|
updateGlobals({ locale: nextLocale });
|
|
|
|
}, 2000);
|
|
|
|
return () => clearInterval(interval);
|
|
|
|
});
|
2021-02-08 16:45:06 +01:00
|
|
|
|
|
|
|
return (
|
2021-10-29 19:22:07 +02:00
|
|
|
<IconButton
|
|
|
|
onClick={() => updateGlobals({ localeParty: !globals.localeParty })}
|
|
|
|
>
|
|
|
|
<Icons icon={globals.localeParty ? 'star' : 'starhollow'} />
|
|
|
|
<span> Shuffle i18n locale</span>
|
2021-02-08 16:45:06 +01:00
|
|
|
</IconButton>
|
2021-10-29 19:22:07 +02:00
|
|
|
);
|
2021-02-08 16:45:06 +01:00
|
|
|
},
|
2021-10-29 19:22:07 +02:00
|
|
|
});
|
|
|
|
});
|