Skip to content

Commit fab0cc7

Browse files
committed
optimize: 优化主题设置和tabs缓存的相关代码
1 parent 97454a9 commit fab0cc7

File tree

4 files changed

+38
-46
lines changed

4 files changed

+38
-46
lines changed

src/features/antdConfig/AntdContextHolder.tsx

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import { App } from 'antd';
22
import type { PropsWithChildren } from 'react';
3-
43
import '@ant-design/v5-patch-for-react-19';
5-
import { cacheTabs } from '@/store/slice/tab';
6-
import { cacheThemeSettings } from '@/store/slice/theme';
74

85
function ContextHolder() {
96
const { message, modal, notification } = App.useApp();
@@ -14,17 +11,6 @@ function ContextHolder() {
1411
}
1512

1613
const AppProvider = ({ children }: PropsWithChildren) => {
17-
const dispatch = useAppDispatch();
18-
19-
useEventListener(
20-
'beforeunload',
21-
() => {
22-
dispatch(cacheTabs());
23-
dispatch(cacheThemeSettings());
24-
},
25-
{ target: window }
26-
);
27-
2814
return (
2915
<App className="h-full">
3016
<ContextHolder />

src/features/antdConfig/AntdProvider.tsx

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ import type { WatermarkProps } from 'antd';
22
import type { PropsWithChildren } from 'react';
33

44
import { info } from '@/constants/app';
5+
import { useThemeSettings } from '@/hooks/common/theme';
56
import { antdLocales } from '@/locales/antd';
6-
import { getThemeSettings, themeColors } from '@/store/slice/theme';
7+
import { selectTabs } from '@/store/slice/tab';
8+
import { themeColors } from '@/store/slice/theme';
79
import { getAntdTheme, setupThemeVarsToHtml } from '@/store/slice/theme/shared';
810
import { localStg } from '@/utils/storage';
911

@@ -22,10 +24,12 @@ const WATERMARK_CONFIG = {
2224
} satisfies WatermarkProps;
2325

2426
function useAntdTheme() {
25-
const themeSettings = useAppSelector(getThemeSettings);
27+
const themeSettings = useThemeSettings();
2628

2729
const colors = useAppSelector(themeColors);
2830

31+
const tabs = useAppSelector(selectTabs);
32+
2933
const { darkMode } = useTheme();
3034

3135
const antdTheme = getAntdTheme(colors, darkMode, themeSettings.tokens);
@@ -35,6 +39,32 @@ function useAntdTheme() {
3539
localStg.set('themeColor', colors.primary);
3640
}, [colors, themeSettings]);
3741

42+
useMount(() => {
43+
function cacheThemeSettings() {
44+
const isProd = import.meta.env.PROD;
45+
46+
if (!isProd) return;
47+
localStg.set('themeSettings', themeSettings);
48+
}
49+
50+
function cacheTabs() {
51+
if (!themeSettings.tab.cache) return;
52+
localStg.set('globalTabs', tabs);
53+
}
54+
55+
window.addEventListener('beforeunload', () => {
56+
cacheThemeSettings();
57+
cacheTabs();
58+
});
59+
60+
return () => {
61+
window.removeEventListener('beforeunload', () => {
62+
cacheThemeSettings();
63+
cacheTabs();
64+
});
65+
};
66+
});
67+
3868
console.info(`%c${info}`, `color: ${colors.primary}`);
3969

4070
return { antdTheme, watermarkText: themeSettings.watermark.text, watermarkVisible: themeSettings.watermark.visible };

src/layouts/modules/GlobalContent.tsx

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,26 @@ import clsx from 'clsx';
22
import KeepAlive, { useKeepAliveRef } from 'keepalive-for-react';
33

44
import { getReloadFlag } from '@/store/slice/app';
5-
import { getRemoveCacheKey, selectCacheRoutes } from '@/store/slice/route';
6-
import { getThemeSettings } from '@/store/slice/theme';
5+
import { selectCacheRoutes, selectRemoveCacheKey } from '@/store/slice/route';
76
import './transition.css';
87

98
interface Props {
109
/** Show padding for content */
1110
closePadding?: boolean;
1211
}
1312

14-
const useGetCacheKey = () => {
15-
const { pathname, search } = useLocation();
16-
17-
const cacheKey = useMemo(() => {
18-
return (pathname + search).slice(1).split('/').join('_');
19-
}, [pathname, search]);
20-
21-
return cacheKey;
22-
};
23-
2413
const GlobalContent: FC<Props> = memo(({ closePadding }) => {
2514
const currentOutlet = useOutlet();
26-
15+
const { pathname } = useLocation();
2716
const aliveRef = useKeepAliveRef();
2817

29-
const removeCacheKey = useAppSelector(getRemoveCacheKey);
18+
const removeCacheKey = useAppSelector(selectRemoveCacheKey);
3019

3120
const cacheKeys = useAppSelector(selectCacheRoutes);
3221

33-
const reload = useAppSelector(getReloadFlag);
22+
console.log(cacheKeys, 'cacheKeys');
3423

35-
const cacheKey = useGetCacheKey();
24+
const reload = useAppSelector(getReloadFlag);
3625

3726
// const themeSetting = useAppSelector(getThemeSettings);
3827

@@ -51,7 +40,7 @@ const GlobalContent: FC<Props> = memo(({ closePadding }) => {
5140
return (
5241
<div className={clsx('h-full flex-grow bg-layout', { 'p-16px': !closePadding })}>
5342
<KeepAlive
54-
activeCacheKey={cacheKey}
43+
activeCacheKey={pathname}
5544
aliveRef={aliveRef}
5645
include={cacheKeys}
5746
>

src/store/slice/theme/index.ts

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@ import { createSelector, createSlice } from '@reduxjs/toolkit';
22
import type { PayloadAction } from '@reduxjs/toolkit';
33
import { getPaletteColorByNumber } from '@sa/color';
44

5-
import { localStg } from '@/utils/storage';
6-
7-
import type { AppThunk } from '../..';
8-
95
import { initThemeSettings, toggleAuxiliaryColorModes, toggleGrayscaleMode } from './shared';
106

117
interface InitialStateType {
@@ -157,15 +153,6 @@ export const themeColors = createSelector([getThemeSettings], ({ isInfoFollowPri
157153
return colors;
158154
});
159155

160-
/** Cache theme settings */
161-
export const cacheThemeSettings = (): AppThunk => (_, getState) => {
162-
const isProd = import.meta.env.PROD;
163-
164-
if (!isProd) return;
165-
166-
localStg.set('themeSettings', getThemeSettings(getState()));
167-
};
168-
169156
export const settingsJson = createSelector([getThemeSettings], settings => {
170157
return JSON.stringify(settings);
171158
});

0 commit comments

Comments
 (0)