Skip to content

Commit 1dd5afc

Browse files
committed
feat: 菜单
1 parent 19c0f29 commit 1dd5afc

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

src/app/template.tsx

+8-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { AppConfig } from '../utils/config/app';
2121
import { MonacoEditor } from '../utils/config/editor';
2222

2323
import { menu } from '../utils/menu';
24+
import { storageStringifyParseValue } from '../utils/storage/menu';
2425

2526
// console.log('menu', menu);
2627

@@ -30,8 +31,9 @@ export default function Template({
3031
// @ts-ignore
3132
children,
3233
}) {
34+
const cacheMenuStatus = storageStringifyParseValue();
3335
const pathname = usePathname();
34-
const [collapsed, setCollapsed] = useState(false);
36+
const [collapsed, setCollapsed] = useState(cacheMenuStatus.getItem());
3537
const [appConfig, setAppConfig] = useState<
3638
Partial<AppConfigParams> | undefined
3739
>({
@@ -134,7 +136,11 @@ export default function Template({
134136
icon={
135137
collapsed ? <MenuUnfoldOutlined /> : <MenuFoldOutlined />
136138
}
137-
onClick={() => setCollapsed(!collapsed)}
139+
onClick={() => {
140+
const status = !collapsed;
141+
cacheMenuStatus.setItem(status);
142+
setCollapsed(status);
143+
}}
138144
style={{
139145
fontSize: '16px',
140146
}}

src/utils/storage/menu.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { getStorageKey, storageTools } from './tools';
2+
3+
export const storageStringifyParseValue = (key: string = '') => {
4+
return storageTools(getStorageKey(`menu_status_${key}`));
5+
};

src/utils/storage/tools.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,21 @@ export const storageTools = (key: string) => {
99
return getStorageKey(key);
1010
},
1111
getItem() {
12-
const value = localStorage.getItem(key);
12+
const value = window?.localStorage?.getItem?.(key);
1313
if (value) {
1414
return JSON.parse(value);
1515
}
1616

1717
return null;
1818
},
1919
setItem(value: any) {
20-
localStorage.setItem(key, JSON.stringify(value));
20+
window?.localStorage?.setItem?.(key, JSON.stringify(value));
2121
},
2222
removeItem() {
23-
localStorage.removeItem(key);
23+
window?.localStorage?.removeItem?.(key);
2424
},
2525
clear() {
26-
localStorage.clear();
26+
window?.localStorage?.clear?.();
2727
},
2828
};
2929
};

0 commit comments

Comments
 (0)