Browse Source

feat:pageCacheState优化

pull/43/head
cp3hnu 1 year ago
parent
commit
c9f454a299
3 changed files with 47 additions and 31 deletions
  1. +6
    -6
      react-ui/src/components/RightContent/AvatarDropdown.tsx
  2. +1
    -1
      react-ui/src/components/RightContent/index.tsx
  3. +40
    -24
      react-ui/src/hooks/pageCacheState.ts

+ 6
- 6
react-ui/src/components/RightContent/AvatarDropdown.tsx View File

@@ -2,7 +2,7 @@ import { clearSessionToken } from '@/access';
import { setRemoteMenu } from '@/services/session';
import { logout } from '@/services/system/auth';
import { gotoLoginPage } from '@/utils/ui';
import { LogoutOutlined, SettingOutlined, UserOutlined } from '@ant-design/icons';
import { LogoutOutlined, UserOutlined } from '@ant-design/icons';
import { setAlpha } from '@ant-design/pro-components';
import { useEmotionCss } from '@ant-design/use-emotion-css';
import { history, useModel } from '@umijs/max';
@@ -127,11 +127,11 @@ const AvatarDropdown: React.FC<GlobalHeaderRightProps> = ({ menu }) => {
icon: <UserOutlined />,
label: '个人中心',
},
{
key: 'settings',
icon: <SettingOutlined />,
label: '个人设置',
},
// {
// key: 'settings',
// icon: <SettingOutlined />,
// label: '个人设置',
// },
{
type: 'divider' as const,
},


+ 1
- 1
react-ui/src/components/RightContent/index.tsx View File

@@ -48,7 +48,7 @@ const GlobalHeaderRight: React.FC = () => {
>
<QuestionCircleOutlined />
</span> */}
<Avatar menu={false} />
<Avatar menu={true} />
{/* <SelectLang className={actionClassName} /> */}
</div>
);


+ 40
- 24
react-ui/src/hooks/pageCacheState.ts View File

@@ -4,39 +4,55 @@
* @Description: 页面状态缓存,pop 回到这个页面的时候,重新构建之前的状态
*/

const pageKeys: string[] = [];
import { useCallback, useState } from 'react';

export const useCacheState = () => {
const { pathname } = window.location;
const key = 'pagecache:' + pathname;
const pageKeys: string[] = [];
// let stateCaches: Record<string, any> = {};

const getState = () => {
const jsonStr = sessionStorage.getItem(key);
if (jsonStr) {
removeState();
// 获取页面缓存
const getCacheState = (key: string) => {
const jsonStr = sessionStorage.getItem(key);
if (jsonStr) {
removeCacheState(key);
try {
return JSON.parse(jsonStr);
} catch (error) {
return undefined;
}
return undefined;
};

const setState = (state: any) => {
pageKeys.push(key);
sessionStorage.setItem(key, JSON.stringify(state));
};

const removeState = () => {
sessionStorage.removeItem(key);
const index = pageKeys.indexOf(key);
if (index !== -1) {
pageKeys.splice(index, 1);
}
};
}
return undefined;
};

return [getState(), setState] as const;
// 移除页面 state 缓存
const removeCacheState = (key: string) => {
sessionStorage.removeItem(key);
const index = pageKeys.indexOf(key);
if (index !== -1) {
pageKeys.splice(index, 1);
}
};

// 移除所有页面 state 缓存
export const removeAllPageCacheState = () => {
pageKeys.forEach((key) => {
sessionStorage.removeItem(key);
});
};

export const useCacheState = () => {
const { pathname } = window.location;
const key = 'pagecache:' + pathname;

const setCacheState = useCallback(
(state: any) => {
if (state) {
pageKeys.push(key);
sessionStorage.setItem(key, JSON.stringify(state));
}
},
[key],
);

const [cacheState] = useState(() => getCacheState(key));
return [cacheState, setCacheState] as const;
};

Loading…
Cancel
Save