2024-12-08 00:55:12 +08:00
|
|
|
import ErrorPage from "hooks/Error";
|
2024-12-04 19:57:59 +08:00
|
|
|
import layout from "themes/echoes/layout";
|
2024-12-05 02:13:54 +08:00
|
|
|
import article from "themes/echoes/article";
|
|
|
|
import about from "themes/echoes/about";
|
2024-12-05 16:58:57 +08:00
|
|
|
import { useLocation } from "react-router-dom";
|
2024-12-05 22:46:29 +08:00
|
|
|
import post from "themes/echoes/post";
|
2024-12-08 00:55:12 +08:00
|
|
|
import { memo, useCallback } from "react";
|
2024-12-08 19:19:54 +08:00
|
|
|
import login from "~/dashboard/login";
|
|
|
|
import adminLayout from "~/dashboard/layout";
|
2024-11-27 01:02:05 +08:00
|
|
|
|
2024-12-08 00:55:12 +08:00
|
|
|
const args = {
|
|
|
|
title: "我的页面",
|
|
|
|
theme: "dark",
|
|
|
|
nav: '<a href="/">index</a><a href="/error">error</a><a href="/about">about</a><a href="/post">post</a>',
|
|
|
|
} as const;
|
|
|
|
|
2024-12-08 19:19:54 +08:00
|
|
|
// 创建布局渲染器的工厂函数
|
|
|
|
const createLayoutRenderer = (layoutComponent: any) => {
|
|
|
|
return (children: React.ReactNode) => {
|
|
|
|
return layoutComponent.render({
|
|
|
|
children,
|
|
|
|
args,
|
|
|
|
});
|
|
|
|
};
|
2024-12-08 00:55:12 +08:00
|
|
|
};
|
2024-12-05 16:58:57 +08:00
|
|
|
|
2024-12-08 19:19:54 +08:00
|
|
|
// 使用工厂函数创建不同的布局渲染器
|
|
|
|
const renderLayout = createLayoutRenderer(layout);
|
|
|
|
const renderAdminLayout = createLayoutRenderer(adminLayout);
|
|
|
|
|
2024-12-08 00:55:12 +08:00
|
|
|
const Routes = memo(() => {
|
|
|
|
const location = useLocation();
|
|
|
|
const path = location.pathname.split("/")[1];
|
2024-12-05 02:13:54 +08:00
|
|
|
|
2024-12-08 00:55:12 +08:00
|
|
|
// 使用 useCallback 缓存渲染函数
|
|
|
|
const renderContent = useCallback((Component: any) => {
|
|
|
|
return renderLayout(Component.render(args));
|
|
|
|
}, []);
|
2024-12-05 16:58:57 +08:00
|
|
|
|
2024-12-08 19:19:54 +08:00
|
|
|
// 添加管理后台内容渲染函数
|
|
|
|
const renderAdminContent = useCallback((Component: any) => {
|
|
|
|
return renderAdminLayout(Component.render(args));
|
|
|
|
}, []);
|
|
|
|
|
2024-12-08 00:55:12 +08:00
|
|
|
// 根据路径返回对应组件
|
2024-12-05 22:46:29 +08:00
|
|
|
if (path === "error") {
|
2024-12-08 00:55:12 +08:00
|
|
|
return renderContent(ErrorPage);
|
2024-12-05 16:58:57 +08:00
|
|
|
}
|
|
|
|
|
2024-12-05 22:46:29 +08:00
|
|
|
if (path === "about") {
|
2024-12-08 00:55:12 +08:00
|
|
|
return renderContent(about);
|
2024-12-05 16:58:57 +08:00
|
|
|
}
|
|
|
|
|
2024-12-05 22:46:29 +08:00
|
|
|
if (path === "post") {
|
2024-12-08 00:55:12 +08:00
|
|
|
return renderContent(post);
|
2024-12-05 22:46:29 +08:00
|
|
|
}
|
|
|
|
|
2024-12-08 19:19:54 +08:00
|
|
|
if (path === "login") {
|
|
|
|
return login.render(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 添加管理后台路由判断
|
|
|
|
if (path === "admin") {
|
|
|
|
// 这里可以根据实际需要添加不同的管理页面组件
|
|
|
|
const subPath = location.pathname.split("/")[2];
|
|
|
|
|
|
|
|
// 如果没有子路径,显示仪表盘
|
|
|
|
if (!subPath) {
|
|
|
|
return renderAdminLayout(<div>仪表盘内容</div>);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 根据子路径返回对应的管理页面
|
|
|
|
switch (subPath) {
|
|
|
|
case "posts":
|
|
|
|
return renderAdminLayout(<div>文章管理</div>);
|
|
|
|
case "media":
|
|
|
|
return renderAdminLayout(<div>媒体管理</div>);
|
|
|
|
case "comments":
|
|
|
|
return renderAdminLayout(<div>评论管理</div>);
|
|
|
|
case "categories":
|
|
|
|
return renderAdminLayout(<div>分类管理</div>);
|
|
|
|
case "settings":
|
|
|
|
return renderAdminLayout(<div>系统设置</div>);
|
|
|
|
default:
|
|
|
|
return renderAdminLayout(<div>404 未找到页面</div>);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-08 00:55:12 +08:00
|
|
|
return renderContent(article);
|
|
|
|
});
|
|
|
|
|
|
|
|
export default Routes;
|