2022-12-08 17:44:49 +08:00
|
|
|
import React from 'react';
|
|
|
|
import { useOutlet, useSearchParams } from 'dumi';
|
2022-11-09 12:28:04 +08:00
|
|
|
import { ConfigProvider, theme as antdTheme } from 'antd';
|
2022-12-01 16:10:53 +08:00
|
|
|
import { createCache, StyleProvider } from '@ant-design/cssinjs';
|
2022-11-22 17:00:28 +08:00
|
|
|
import ThemeSwitch from '../common/ThemeSwitch';
|
2022-12-08 17:44:49 +08:00
|
|
|
import type { ThemeName } from '../common/ThemeSwitch';
|
2022-11-22 17:00:28 +08:00
|
|
|
import useLocation from '../../hooks/useLocation';
|
2022-11-09 12:28:04 +08:00
|
|
|
|
2022-12-01 16:10:53 +08:00
|
|
|
const styleCache = createCache();
|
|
|
|
if (typeof global !== 'undefined') {
|
|
|
|
(global as any).styleCache = styleCache;
|
|
|
|
}
|
|
|
|
|
2022-12-08 17:44:49 +08:00
|
|
|
const getAlgorithm = (themes: ThemeName[]) =>
|
|
|
|
(themes || []).map((theme) => {
|
|
|
|
if (theme === 'dark') {
|
|
|
|
return antdTheme.darkAlgorithm;
|
|
|
|
}
|
|
|
|
if (theme === 'compact') {
|
|
|
|
return antdTheme.compactAlgorithm;
|
|
|
|
}
|
|
|
|
return antdTheme.defaultAlgorithm;
|
|
|
|
});
|
2022-11-22 17:00:28 +08:00
|
|
|
|
2022-11-24 20:11:50 +08:00
|
|
|
const GlobalLayout: React.FC = () => {
|
2022-11-09 12:28:04 +08:00
|
|
|
const outlet = useOutlet();
|
2022-11-22 17:00:28 +08:00
|
|
|
const { pathname } = useLocation();
|
2022-12-08 17:44:49 +08:00
|
|
|
const [searchParams, setSearchParams] = useSearchParams();
|
|
|
|
const theme = searchParams.getAll('theme') as unknown as ThemeName[];
|
|
|
|
const handleThemeChange = (value: ThemeName[]) => {
|
|
|
|
setSearchParams({
|
|
|
|
...searchParams,
|
|
|
|
theme: value,
|
|
|
|
});
|
2022-11-22 17:00:28 +08:00
|
|
|
};
|
2022-11-09 12:28:04 +08:00
|
|
|
|
|
|
|
return (
|
2022-12-01 16:10:53 +08:00
|
|
|
<StyleProvider cache={styleCache}>
|
2022-12-08 17:44:49 +08:00
|
|
|
<ConfigProvider
|
|
|
|
theme={{
|
|
|
|
algorithm: getAlgorithm(theme),
|
|
|
|
}}
|
|
|
|
>
|
2022-12-01 17:55:20 +08:00
|
|
|
{outlet}
|
|
|
|
{!pathname.startsWith('/~demos') && (
|
2022-12-08 17:44:49 +08:00
|
|
|
<ThemeSwitch value={theme} onChange={handleThemeChange} />
|
2022-12-01 17:55:20 +08:00
|
|
|
)}
|
|
|
|
</ConfigProvider>
|
2022-12-01 16:10:53 +08:00
|
|
|
</StyleProvider>
|
2022-11-09 12:28:04 +08:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default GlobalLayout;
|