diff --git a/src/global.d.ts b/src/global.d.ts
index e64304b..36438e0 100644
--- a/src/global.d.ts
+++ b/src/global.d.ts
@@ -10,9 +10,10 @@ interface ImportMeta {
readonly env: ImportMetaEnv
}
-type ToolsJsonObject = {
+type RouteJsonObject = {
path: string
- id: string
+ id?: string
+ element?: React.JSX.Element
component?: React.ComponentType
name?: string
titlePrefix?: string
@@ -21,7 +22,7 @@ type ToolsJsonObject = {
icon?: IconComponent
menu?: boolean
auth?: boolean
- children?: ToolsJsonObject[]
+ children?: RouteJsonObject[]
}
type RouteHandle = {
diff --git a/src/pages/ToolsFramework.tsx b/src/pages/ToolsFramework.tsx
index c997eff..27d51cc 100644
--- a/src/pages/ToolsFramework.tsx
+++ b/src/pages/ToolsFramework.tsx
@@ -2,7 +2,7 @@ import React from 'react'
import FitFullScreen from '@/components/common/FitFullScreen'
import '@/assets/css/pages/tools-framework.scss'
import Icon from '@ant-design/icons'
-import { toolsJsonObjects } from '@/router/tools'
+import { tools } from '@/router/tools'
import HideScrollbar, { HideScrollbarElement } from '@/components/common/HideScrollbar'
import { getLocalStorage, getRedirectUrl, setLocalStorage } from '@/utils/common'
import { getLoginStatus, logout } from '@/utils/auth'
@@ -88,14 +88,14 @@ const ToolsFramework: React.FC = () => {
}
>
- {toolsJsonObjects[0].icon ? (
+ {tools[0].icon ? (
) : undefined}
- {toolsJsonObjects[0].name}
+ {tools[0].name}
@@ -108,14 +108,14 @@ const ToolsFramework: React.FC = () => {
}
>
- {toolsJsonObjects[1].icon ? (
+ {tools[1].icon ? (
) : undefined}
- {toolsJsonObjects[1].name}
+ {tools[1].name}
@@ -132,7 +132,7 @@ const ToolsFramework: React.FC = () => {
ref={hideScrollbarRef}
>
- {toolsJsonObjects.map((tool) => {
+ {tools.map((tool) => {
return tool.menu &&
tool.id !== 'tools' &&
tool.id !== 'tools-all' ? (
diff --git a/src/router/home.tsx b/src/router/home.tsx
new file mode 100644
index 0000000..50792a6
--- /dev/null
+++ b/src/router/home.tsx
@@ -0,0 +1,34 @@
+import React from 'react'
+
+const home: RouteJsonObject[] = [
+ {
+ path: '',
+ id: 'home',
+ component: React.lazy(() => import('@/pages/home')),
+ name: '主页',
+ menu: true,
+ auth: false
+ },
+ {
+ path: 'https://blog.fatweb.top',
+ id: 'url-blog',
+ name: '博客',
+ menu: true
+ },
+ {
+ path: '/tools',
+ id: 'url-tools',
+ children: [
+ {
+ path: 'translation',
+ id: 'url-tools-translation',
+ name: '翻译',
+ menu: true
+ }
+ ],
+ name: '工具',
+ menu: true
+ }
+]
+
+export default home
diff --git a/src/router/index.tsx b/src/router/index.tsx
index ea360d9..2259f62 100644
--- a/src/router/index.tsx
+++ b/src/router/index.tsx
@@ -1,74 +1,36 @@
import React from 'react'
import tools from '@/router/tools'
+import home from '@/router/home'
-const routes: RouteObject[] = [
+const root: RouteJsonObject[] = [
{
path: '/',
- Component: React.lazy(() => import('@/AuthRoute')),
+ component: React.lazy(() => import('@/AuthRoute')),
children: [
{
path: '/login',
id: 'login',
- Component: React.lazy(() => import('@/pages/Login'))
+ component: React.lazy(() => import('@/pages/Login'))
},
{
path: '/loading',
id: 'loading',
- Component: React.lazy(() => import('@/components/common/LoadingMask'))
+ component: React.lazy(() => import('@/components/common/LoadingMask'))
},
{
path: '/tools',
id: 'toolsFramework',
- Component: React.lazy(() => import('@/pages/ToolsFramework')),
+ component: React.lazy(() => import('@/pages/ToolsFramework')),
children: tools,
- handle: {
- name: '工具',
- title: '工具',
- auth: false
- }
+ name: '工具',
+ title: '工具',
+ auth: false
},
{
path: '',
id: 'homeFramework',
- Component: React.lazy(() => import('@/pages/HomeFramework')),
- children: [
- {
- path: '',
- id: 'home',
- Component: React.lazy(() => import('@/pages/home')),
- handle: {
- name: '主页',
- menu: true,
- auth: false
- }
- },
- {
- path: 'https://blog.fatweb.top',
- id: 'url-blog',
- handle: {
- name: '博客',
- menu: true
- }
- },
- {
- path: '/tools',
- id: 'url-tools',
- children: [
- {
- path: 'translation',
- id: 'url-tools-translation',
- handle: {
- name: '翻译',
- menu: true
- }
- }
- ],
- handle: {
- name: '工具',
- menu: true
- }
- }
- ]
+ component: React.lazy(() => import('@/pages/HomeFramework')),
+ children: home
},
{
path: '*',
@@ -78,5 +40,34 @@ const routes: RouteObject[] = [
}
]
+const mapJsonToRoute = (jsonObject: RouteJsonObject[]): RouteObject[] => {
+ return jsonObject.map((value) => ({
+ path: value.path,
+ id: value.id,
+ element: value.element,
+ Component: value.component,
+ handle: {
+ name: value.name,
+ titlePrefix: value.titlePrefix,
+ title: value.title,
+ titlePostfix: value.titlePostfix,
+ icon: value.icon,
+ menu: value.menu,
+ auth: value.auth
+ },
+ children:
+ value.children &&
+ mapJsonToRoute(value.children).map((sub) => {
+ const handle = sub.handle as RouteHandle
+ if (!handle.title) {
+ handle.title = value.title
+ }
+ return sub
+ })
+ }))
+}
+
+const routes = mapJsonToRoute(root)
+
const router = createBrowserRouter(routes)
export default router
diff --git a/src/router/tools.tsx b/src/router/tools.tsx
index 997809a..89a38ff 100644
--- a/src/router/tools.tsx
+++ b/src/router/tools.tsx
@@ -1,8 +1,6 @@
import React from 'react'
-const defaultTitle = '氮工具'
-
-export const toolsJsonObjects: ToolsJsonObject[] = [
+export const tools: RouteJsonObject[] = [
{
path: '',
id: 'tools',
@@ -277,41 +275,11 @@ export const toolsJsonObjects: ToolsJsonObject[] = [
auth: false
}
]
+ },
+ {
+ path: '*',
+ element:
}
]
-const tools: RouteObject[] = toolsJsonObjects.map((value) => ({
- path: value.path,
- id: value.id,
- Component: value.component,
- handle: {
- name: value.name,
- titlePrefix: value.titlePrefix,
- title: value.title ?? defaultTitle,
- titlePostfix: value.titlePostfix,
- icon: value.icon,
- menu: value.menu,
- auth: value.auth
- },
- children: value.children?.map((value) => ({
- path: value.path,
- id: value.id,
- Component: value.component,
- handle: {
- name: value.name,
- titlePrefix: value.titlePrefix,
- title: value.title ?? defaultTitle,
- titlePostfix: value.titlePostfix,
- icon: value.icon,
- menu: value.menu,
- auth: value.auth
- }
- }))
-}))
-
-tools.push({
- path: '*',
- element:
-})
-
export default tools