Skip to content

Commit f1fa947

Browse files
committed
chore: update style
1 parent 364cf80 commit f1fa947

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+385
-330
lines changed

packages/platform/src/app/App.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import { STORAGE_KEY } from '../config/storage';
1414
import { AppRoutes } from './Routes';
1515
import { useHttp, useInit } from './hooks';
1616

17+
export type AppTheme = 'light' | 'dark';
18+
1719
export function App() {
1820
const { i18n } = useTranslation();
1921
const createHttp = useHttp();
@@ -22,6 +24,7 @@ export function App() {
2224
const async = useAsync();
2325
const [language] = useLocalStorage<DLang>(...STORAGE_KEY.language);
2426
const [loading, setLoading] = useState(true);
27+
const [theme] = useLocalStorage<AppTheme>(...STORAGE_KEY.theme);
2528

2629
useMount(() => {
2730
i18n.changeLanguage(language);
@@ -56,6 +59,17 @@ export function App() {
5659
}
5760
}, [async, loading]);
5861

62+
useEffect(() => {
63+
for (const t of ['light', 'dark']) {
64+
document.body.classList.toggle(t, theme === t);
65+
}
66+
const colorScheme = document.documentElement.style.colorScheme;
67+
document.documentElement.style.colorScheme = theme;
68+
return () => {
69+
document.documentElement.style.colorScheme = colorScheme;
70+
};
71+
}, [theme]);
72+
5973
const rootContext = useMemo<DConfigContextData>(
6074
() => ({
6175
i18n: { lang: language },

packages/platform/src/app/Routes.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import React, { useEffect } from 'react';
55
import { useTranslation } from 'react-i18next';
66
import { matchRoutes, Navigate, renderMatches, useLocation } from 'react-router-dom';
77

8+
import { ROUTES_ACL } from '../config/acl';
89
import { LOGIN_PATH, TITLE_CONFIG } from '../config/other';
910
import { useACLGuard, useTokenGuard } from './Routes.guard';
1011
import AppExceptionRoute from './routes/Exception';
@@ -82,7 +83,7 @@ export function AppRoutes() {
8283
),
8384
data: {
8485
title: 'AMap',
85-
acl: 'page_amap',
86+
acl: ROUTES_ACL.dashboard.amap,
8687
canActivate: [ACLGuard],
8788
},
8889
},
@@ -95,7 +96,7 @@ export function AppRoutes() {
9596
),
9697
data: {
9798
title: 'ECharts',
98-
acl: 'page_echarts',
99+
acl: ROUTES_ACL.dashboard.echarts,
99100
canActivate: [ACLGuard],
100101
},
101102
},

packages/platform/src/app/hooks/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,3 @@ export { useHttp } from './useHttp';
22
export { useACL } from './useACL';
33
export { useDeviceQuery } from './useDeviceQuery';
44
export { useInit } from './useInit';
5-
export { useMenu } from './useMenu';
6-
export { usePrevRoute } from './usePrevRoute';

packages/platform/src/app/hooks/useHttp/useHttp.ts renamed to packages/platform/src/app/hooks/useHttp.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import { Subject, takeUntil } from 'rxjs';
99

1010
import { useUnmount } from '@react-devui/hooks';
1111

12-
import { TOKEN } from '../../../config/token';
13-
import { environment } from '../../../environments';
14-
import './mock';
12+
import '../../config/mock';
13+
import { TOKEN } from '../../config/token';
14+
import { environment } from '../../environments';
1515

1616
export function useHttp() {
1717
const abortFns = useRef(new Set<() => void>());

packages/platform/src/app/hooks/useHttp/index.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

packages/platform/src/app/hooks/useInit.ts

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1+
import type { MenuItem } from '../../config/menu';
2+
import type { PREV_ROUTE_KEY } from '../../config/other';
13
import type { NotificationItem, UserState } from '../../config/state';
4+
import type { DMenuItem } from '@react-devui/ui/components/menu';
25

3-
import { isNull } from 'lodash';
6+
import { isNull, isObject, isUndefined } from 'lodash';
7+
import React from 'react';
8+
import { useTranslation } from 'react-i18next';
9+
import { Link, useLocation, useNavigate } from 'react-router-dom';
410

11+
import { MENU } from '../../config/menu';
12+
import { useMenuState } from '../../config/state';
513
import { useNotificationState } from '../../config/state';
614
import { useUserState } from '../../config/state';
715
import { TOKEN, TOKEN_REFRESH, TOKEN_REFRESH_OFFSET } from '../../config/token';
@@ -11,10 +19,16 @@ import { useHttp } from './useHttp';
1119
let CLEAR_TOKEN_REFRESH: (() => void) | undefined;
1220

1321
export function useInit() {
22+
const { t } = useTranslation();
23+
const navigate = useNavigate();
24+
const location = useLocation();
25+
const from = (location.state as null | { [PREV_ROUTE_KEY]?: Location })?.from?.pathname;
1426
const createHttp = useHttp();
27+
const acl = useACL();
28+
1529
const [, setUser] = useUserState();
30+
const [, setMenu] = useMenuState();
1631
const [, setNotification] = useNotificationState();
17-
const acl = useACL();
1832

1933
const refreshToken = () => {
2034
CLEAR_TOKEN_REFRESH?.();
@@ -49,6 +63,65 @@ export function useInit() {
4963
acl.setFull(user.role === 'admin');
5064
acl.set([]);
5165
//#endregion
66+
67+
//#region Menu
68+
const allItem: { id: string; parentSub: string[] }[] = [];
69+
const reduceMenu = (arr: MenuItem[], parentSub: string[] = []): DMenuItem<string>[] => {
70+
const newArr: DMenuItem<string>[] = [];
71+
for (const item of arr) {
72+
if (item.acl) {
73+
const params =
74+
isObject(item.acl) && 'control' in item.acl
75+
? item.acl
76+
: {
77+
control: item.acl,
78+
};
79+
if (!acl.can(params.control, params.mode)) {
80+
continue;
81+
}
82+
}
83+
84+
const title = item.title ?? t(item.titleI18n!);
85+
const obj: DMenuItem<string> = {
86+
id: item.path,
87+
title:
88+
item.type === 'item'
89+
? React.createElement(Link, { className: 'app-layout-sidebar__link', tabIndex: -1, to: item.path }, title)
90+
: title,
91+
icon: item.icon ? React.createElement(item.icon) : undefined,
92+
type: item.type,
93+
};
94+
95+
if (item.children) {
96+
obj.children = reduceMenu(item.children, parentSub.concat(item.type === 'sub' ? [item.path] : []));
97+
if (obj.children.length > 0) {
98+
newArr.push(obj);
99+
}
100+
} else {
101+
newArr.push(obj);
102+
allItem.push({ id: item.path, parentSub });
103+
}
104+
}
105+
return newArr;
106+
};
107+
108+
const menu = reduceMenu(MENU);
109+
const toFirstItem = () => {
110+
setMenu({ menu, expands: allItem[0].parentSub });
111+
navigate(allItem[0].id, { replace: true });
112+
};
113+
if (isUndefined(from)) {
114+
toFirstItem();
115+
} else {
116+
const findIndex = allItem.findIndex((item) => item.id === from);
117+
if (findIndex === -1) {
118+
toFirstItem();
119+
} else {
120+
setMenu({ menu, expands: allItem[findIndex].parentSub });
121+
navigate(from, { replace: true });
122+
}
123+
}
124+
//#endregion
52125
};
53126

54127
const getNotification = () => {

packages/platform/src/app/hooks/useMenu.ts

Lines changed: 0 additions & 80 deletions
This file was deleted.

packages/platform/src/app/hooks/usePrevRoute.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.

packages/platform/src/app/routes/Login.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import type { UserState } from '../../config/state';
22

33
import { useEffect, useState } from 'react';
44
import { useTranslation } from 'react-i18next';
5-
import { useNavigate } from 'react-router-dom';
65

76
import { useAsync } from '@react-devui/hooks';
87
import { LockOutlined, MobileOutlined, UserOutlined } from '@react-devui/icons';
@@ -11,15 +10,13 @@ import { getClassName } from '@react-devui/utils';
1110

1211
import { TOKEN } from '../../config/token';
1312
import { AppLanguage } from '../components';
14-
import { useDeviceQuery, useHttp, useInit, usePrevRoute } from '../hooks';
13+
import { useDeviceQuery, useHttp, useInit } from '../hooks';
1514

1615
export default function Login(): JSX.Element | null {
1716
const { t } = useTranslation();
1817
const createHttp = useHttp();
1918
const [loginloading, setLoginLoading] = useState(false);
2019
const init = useInit();
21-
const navigate = useNavigate();
22-
const from = usePrevRoute();
2320
const async = useAsync();
2421
const deviceMatched = useDeviceQuery();
2522

@@ -37,7 +34,7 @@ export default function Login(): JSX.Element | null {
3734
const [accountForm, updateAccountForm] = useForm(
3835
() =>
3936
new FormGroup({
40-
username: new FormControl('', [
37+
username: new FormControl('admin', [
4138
Validators.required,
4239
(control) => {
4340
return !control.value || control.value === 'admin' ? null : { checkValue: true };
@@ -79,7 +76,6 @@ export default function Login(): JSX.Element | null {
7976
setLoginLoading(false);
8077
TOKEN.token = res.token;
8178
init(res.user);
82-
navigate(from, { replace: true });
8379
},
8480
});
8581
}}
Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
import React from 'react';
2+
import { useLocation } from 'react-router-dom';
3+
14
import { DDrawer, DMenu } from '@react-devui/ui';
25

3-
import { useDeviceQuery, useMenu } from '../../hooks';
6+
import { useMenuState } from '../../../config/state';
7+
import { useDeviceQuery } from '../../hooks';
48

59
export interface AppSidebarProps {
610
menuOpen: boolean;
@@ -12,15 +16,27 @@ export function AppSidebar(props: AppSidebarProps): JSX.Element | null {
1216

1317
const deviceMatched = useDeviceQuery();
1418

15-
const menu = useMenu();
19+
const [{ menu, expands }, setMenu] = useMenuState();
20+
const location = useLocation();
21+
const menuNode = (
22+
<DMenu
23+
className="app-layout-sidebar__menu"
24+
dList={menu}
25+
dActive={location.pathname}
26+
dExpands={expands}
27+
onExpandsChange={(ids) => {
28+
setMenu((draft) => {
29+
draft.expands = ids;
30+
});
31+
}}
32+
></DMenu>
33+
);
1634

1735
return deviceMatched === 'phone' ? (
1836
<DDrawer className="app-layout-sidebar__drawer" dVisible={menuOpen} dWidth={200} dPlacement="left" onVisibleChange={onMenuOpenChange}>
19-
<DMenu className="app-layout-sidebar__menu" dList={menu}></DMenu>
37+
{menuNode}
2038
</DDrawer>
2139
) : (
22-
<div className="app-layout-sidebar">
23-
<DMenu className="app-layout-sidebar__menu" dList={menu} dMode={menuOpen ? 'vertical' : 'icon'}></DMenu>
24-
</div>
40+
<div className="app-layout-sidebar">{React.cloneElement(menuNode, { dMode: menuOpen ? 'vertical' : 'icon' })}</div>
2541
);
2642
}

0 commit comments

Comments
 (0)