import ErrorPage from "hooks/Error";
import { useLocation } from "react-router-dom";
import post from "themes/echoes/post";
import React, { memo, useCallback } from "react";
import adminLayout from "~/dashboard/layout";
import dashboard from "~/dashboard/index";
import comments from "~/dashboard/comments";
import categories from "~/dashboard/categories";
import settings from "~/dashboard/settings";
import files from "~/dashboard/files";
import themes from "~/dashboard/themes";
import users from "~/dashboard/users";
import layout from "~/dashboard/layout";
// 创建布局渲染器的工厂函数
const createLayoutRenderer = (layoutComponent: any) => {
return (children: React.ReactNode) => {
return layoutComponent.render({
children,
args,
});
};
};
// 创建组件的工厂函数
const createComponentRenderer = (path: string) => {
return React.lazy(async () => {
const module = await import(/* @vite-ignore */ path);
return {
default: (props: any) => {
if (typeof module.default.render === "function") {
return module.default.render(props);
}
},
};
});
};
// 使用工厂函数创建不同的布局渲染器
const renderLayout = createLayoutRenderer(layout);
const renderDashboardLayout = createLayoutRenderer(adminLayout);
const Login = createComponentRenderer("./dashboard/login");
const posts = createComponentRenderer("themes/echoes/posts");
const Routes = memo(() => {
const location = useLocation();
const [mainPath, subPath] = location.pathname.split("/").filter(Boolean);
// 使用 useCallback 缓存渲染函数
const renderContent = useCallback((Component: any) => {
if (React.isValidElement(Component)) {
return renderLayout(Component);
}
return renderLayout(