Skip to content

Commit 6301b20

Browse files
committed
refactor(platform): update logic of initialization
1 parent ef57069 commit 6301b20

File tree

15 files changed

+781
-786
lines changed

15 files changed

+781
-786
lines changed

packages/platform/src/app/Routes.tsx

Lines changed: 11 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@ import type { DId } from '@react-devui/ui/utils/types';
44
import type { IndexRouteObject, NonIndexRouteObject, RouteMatch } from 'react-router-dom';
55

66
import { isUndefined, nth } from 'lodash';
7-
import React, { useEffect } from 'react';
7+
import React from 'react';
88
import { useTranslation } from 'react-i18next';
99
import { matchRoutes, Navigate, renderMatches, useLocation } from 'react-router-dom';
1010

1111
import { ROUTES_ACL } from '../config/acl';
12-
import { LOGIN_PATH, TITLE_CONFIG } from '../config/other';
13-
import { useMenu } from '../core';
12+
import { LOGIN_PATH } from '../config/other';
1413
import { useACLGuard, useTokenGuard } from './Routes.guard';
1514
import { AppFCPLoader } from './components';
15+
import { usePageTitle } from './hooks';
16+
import AppHomeRoute from './routes/Home';
1617
import AppExceptionRoute from './routes/exception/Exception';
1718
import AppLayout from './routes/layout/Layout';
1819
import AppLoginRoute from './routes/login/Login';
@@ -62,7 +63,6 @@ export function AppRoutes() {
6263
const ACLGuard = useACLGuard();
6364
const tokenGuard = useTokenGuard();
6465
const location = useLocation();
65-
const [, allItem] = useMenu();
6666
const { t } = useTranslation();
6767

6868
const matches = matchRoutes(
@@ -83,19 +83,12 @@ export function AppRoutes() {
8383
},
8484
children: [
8585
{
86-
index: true,
87-
element: '/',
86+
path: '',
87+
element: <AppHomeRoute />,
8888
},
8989
{
9090
path: 'dashboard',
91-
data: {
92-
titleI18n: 'dashboard.',
93-
},
9491
children: [
95-
{
96-
index: true,
97-
element: '/dashboard',
98-
},
9992
{
10093
path: 'amap',
10194
element: (
@@ -126,14 +119,7 @@ export function AppRoutes() {
126119
},
127120
{
128121
path: 'test',
129-
data: {
130-
titleI18n: 'test.',
131-
},
132122
children: [
133-
{
134-
index: true,
135-
element: '/test',
136-
},
137123
{
138124
path: 'acl',
139125
element: (
@@ -165,37 +151,8 @@ export function AppRoutes() {
165151
],
166152
},
167153
{
168-
path: 'exception',
169-
data: {
170-
titleI18n: 'exception.',
171-
},
172-
children: [
173-
{
174-
index: true,
175-
element: '/exception',
176-
},
177-
{
178-
path: '403',
179-
element: <AppExceptionRoute status={403} />,
180-
data: {
181-
titleI18n: 'exception.403',
182-
},
183-
},
184-
{
185-
path: '404',
186-
element: <AppExceptionRoute status={404} />,
187-
data: {
188-
titleI18n: 'exception.404',
189-
},
190-
},
191-
{
192-
path: '500',
193-
element: <AppExceptionRoute status={500} />,
194-
data: {
195-
titleI18n: 'exception.500',
196-
},
197-
},
198-
],
154+
path: 'exception/:status',
155+
element: <AppExceptionRoute />,
199156
},
200157
{
201158
path: '*',
@@ -226,30 +183,12 @@ export function AppRoutes() {
226183
}
227184
}
228185

229-
const currentRoute = matches[matches.length - 1].route;
230-
if (currentRoute.index === true) {
231-
const firstMenu = allItem.find((item) => item.id.startsWith(currentRoute.element as string));
232-
return <Navigate to={isUndefined(firstMenu) ? '/exception/404' : firstMenu.id} replace />;
233-
}
234186
return renderMatches(matches);
235187
})();
236188

237-
useEffect(() => {
238-
const { title: _title, titleI18n } = nth(matches, -1)?.route.data ?? {};
239-
const title = _title ?? (isUndefined(titleI18n) ? undefined : t(titleI18n, { ns: 'title' }));
240-
if (isUndefined(title)) {
241-
document.title = TITLE_CONFIG.default;
242-
} else {
243-
const arr = [title];
244-
if (TITLE_CONFIG.prefix) {
245-
arr.unshift(TITLE_CONFIG.prefix);
246-
}
247-
if (TITLE_CONFIG.suffix) {
248-
arr.push(TITLE_CONFIG.suffix);
249-
}
250-
document.title = arr.join(TITLE_CONFIG.separator ?? ' - ');
251-
}
252-
});
189+
const { title: _title, titleI18n } = nth(matches, -1)?.route.data ?? {};
190+
const title = _title ?? (isUndefined(titleI18n) ? undefined : t(titleI18n, { ns: 'title' }));
191+
usePageTitle(title);
253192

254193
return (
255194
<RouteStateContext.Provider
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { usePageTitle } from './usePageTitle';
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { isUndefined } from 'lodash';
2+
import { useEffect } from 'react';
3+
4+
export const TITLE_CONFIG: {
5+
default: string;
6+
separator: string;
7+
prefix?: string;
8+
suffix?: string;
9+
} = {
10+
default: 'Platform',
11+
separator: ' - ',
12+
suffix: 'Platform',
13+
};
14+
15+
export function usePageTitle(title?: string) {
16+
useEffect(() => {
17+
if (isUndefined(title)) {
18+
document.title = TITLE_CONFIG.default;
19+
} else {
20+
const arr = [title];
21+
if (TITLE_CONFIG.prefix) {
22+
arr.unshift(TITLE_CONFIG.prefix);
23+
}
24+
if (TITLE_CONFIG.suffix) {
25+
arr.push(TITLE_CONFIG.suffix);
26+
}
27+
document.title = arr.join(TITLE_CONFIG.separator ?? ' - ');
28+
}
29+
return () => {
30+
document.title = TITLE_CONFIG.default;
31+
};
32+
});
33+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { isUndefined } from 'lodash';
2+
import { Navigate } from 'react-router-dom';
3+
4+
import { useMenu } from '../../core';
5+
6+
export default function Home(): JSX.Element | null {
7+
const [{ firstPath }] = useMenu();
8+
9+
return isUndefined(firstPath) ? null : <Navigate to={firstPath} replace />;
10+
}

0 commit comments

Comments
 (0)