Skip to content

Commit 51a331a

Browse files
committed
refactor(platform): remove usePageTitle
1 parent fbed91f commit 51a331a

File tree

5 files changed

+46
-56
lines changed

5 files changed

+46
-56
lines changed

packages/platform/src/app/Routes.tsx

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
import type { Control, ControlMode } from './core/useACL';
22
import type { IndexRouteObject, NonIndexRouteObject, RouteMatch } from 'react-router-dom';
33

4-
import { nth } from 'lodash';
5-
import React from 'react';
4+
import { isFunction, isUndefined, nth } from 'lodash';
5+
import React, { useEffect } from 'react';
66
import { useTranslation } from 'react-i18next';
77
import { matchRoutes, Navigate, renderMatches, useLocation } from 'react-router-dom';
88

99
import { useACLGuard, useTokenGuard } from './Routes.guard';
1010
import { AppFCPLoader } from './components';
1111
import { ROUTES_ACL } from './config/acl';
12-
import { LOGIN_PATH } from './config/other';
13-
import { usePageTitle } from './hooks';
12+
import { APP_NAME, LOGIN_PATH } from './config/other';
1413
import AppHomeRoute from './routes/Home';
1514
import AppExceptionRoute from './routes/exception/Exception';
1615
import AppLayout from './routes/layout/Layout';
@@ -25,8 +24,20 @@ const ROUTES = {
2524
'/test/http': React.lazy(() => import('./routes/test/http/Http')),
2625
};
2726

27+
const TITLE_CONFIG: {
28+
default: string;
29+
separator: string;
30+
prefix?: string;
31+
suffix?: string;
32+
} = {
33+
default: APP_NAME,
34+
separator: ' - ',
35+
suffix: APP_NAME,
36+
};
37+
2838
export interface RouteStateContextData {
2939
matchRoutes: RouteMatch<string, RouteItem>[] | null;
40+
title?: string;
3041
}
3142
export const RouteStateContext = React.createContext<RouteStateContextData>({
3243
matchRoutes: null,
@@ -35,7 +46,7 @@ export const RouteStateContext = React.createContext<RouteStateContextData>({
3546
export type CanActivateFn = (route: RouteItem) => true | React.ReactElement;
3647

3748
export interface RouteData {
38-
title?: string;
49+
title?: string | ((params: any) => string);
3950
acl?:
4051
| {
4152
control: Control | Control[];
@@ -173,6 +184,9 @@ export const AppRoutes = React.memo(() => {
173184
{
174185
path: '/exception/:status',
175186
element: <AppExceptionRoute />,
187+
data: {
188+
title: (params) => params.status,
189+
},
176190
},
177191
{
178192
path: '*',
@@ -206,13 +220,37 @@ export const AppRoutes = React.memo(() => {
206220
return renderMatches(matches);
207221
})();
208222

209-
const { title } = nth(matches, -1)?.route.data ?? {};
210-
usePageTitle(title);
223+
const title = (() => {
224+
if (matches) {
225+
const match = nth(matches, -1)!;
226+
const { title } = match.route.data ?? {};
227+
return isFunction(title) ? title(match.params) : title;
228+
}
229+
return undefined;
230+
})();
231+
useEffect(() => {
232+
if (isUndefined(title)) {
233+
document.title = TITLE_CONFIG.default;
234+
} else {
235+
const arr = [title];
236+
if (TITLE_CONFIG.prefix) {
237+
arr.unshift(TITLE_CONFIG.prefix);
238+
}
239+
if (TITLE_CONFIG.suffix) {
240+
arr.push(TITLE_CONFIG.suffix);
241+
}
242+
document.title = arr.join(TITLE_CONFIG.separator ?? ' - ');
243+
}
244+
return () => {
245+
document.title = TITLE_CONFIG.default;
246+
};
247+
});
211248

212249
return (
213250
<RouteStateContext.Provider
214251
value={{
215252
matchRoutes: matches,
253+
title,
216254
}}
217255
>
218256
{element}

packages/platform/src/app/components/route-header/RouteHeaderHeader.tsx

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { nth } from 'lodash';
21
import React, { useContext } from 'react';
32
import { useNavigate } from 'react-router-dom';
43

@@ -21,18 +20,10 @@ export function AppRouteHeaderHeader(props: AppRouteHeaderHeaderProps): JSX.Elem
2120
...restProps
2221
} = props;
2322

24-
const { matchRoutes } = useContext(RouteStateContext);
23+
const { title } = useContext(RouteStateContext);
2524

2625
const navigate = useNavigate();
2726

28-
const title = (() => {
29-
if (matchRoutes) {
30-
const { title } = nth(matchRoutes, -1)!.route.data ?? {};
31-
return title;
32-
}
33-
return undefined;
34-
})();
35-
3627
return (
3728
<div {...restProps} className={getClassName(restProps.className, 'app-route-header__header')}>
3829
<div className="app-route-header__header-title-container">
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
export { useAPI } from './useAPI';
2-
export { usePageTitle } from './usePageTitle';
32
export { useQueryParams } from './useQueryParams';

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

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

packages/platform/src/app/routes/exception/Exception.tsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { useNavigate, useParams } from 'react-router-dom';
44

55
import { DButton } from '@react-devui/ui';
66

7-
import { usePageTitle } from '../../hooks';
87
import { ReactComponent as S403 } from './403.svg';
98
import { ReactComponent as S404 } from './404.svg';
109
import { ReactComponent as S500 } from './500.svg';
@@ -16,8 +15,6 @@ export default function Exception(): JSX.Element | null {
1615

1716
const { status } = useParams();
1817

19-
usePageTitle(status);
20-
2118
return (
2219
<div className={styles['app-exception']}>
2320
{React.createElement(status === '403' ? S403 : status === '404' ? S404 : S500, { className: styles['app-exception__bg'] })}

0 commit comments

Comments
 (0)