fix: 修复后台菜单路由匹配和概览重定向循环
This commit is contained in:
1 parent
bf4bbf81c7
commit
34bd8142de
4 files changed
+78
-4
No files matched your search
@@ -0,0 +1,15 @@
|
|||||||
|
import { createElement, type ReactNode } from "react";
|
||||||
|
import { Navigate, Route, Routes } from "react-router-dom";
|
||||||
|
|
||||||
|
export function AdminOverviewRedirect() {
|
||||||
|
return createElement(Navigate, { to: "/admin/overview", replace: true });
|
||||||
|
}
|
||||||
|
|
||||||
|
export function AdminRouter({ children }: { children: ReactNode }) {
|
||||||
|
return createElement(
|
||||||
|
Routes,
|
||||||
|
null,
|
||||||
|
createElement(Route, { path: "/admin/*", element: children }),
|
||||||
|
createElement(Route, { path: "*", element: createElement(AdminOverviewRedirect) }),
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -31,7 +31,8 @@ import { adminMessage as message } from "../components/AdminMessage.js";
|
|||||||
import { AdminButton } from "../components/AdminButton.js";
|
import { AdminButton } from "../components/AdminButton.js";
|
||||||
import { AdminSelect } from "../components/AdminSelect.js";
|
import { AdminSelect } from "../components/AdminSelect.js";
|
||||||
import { AdminLayout } from "../components/AdminLayout.js";
|
import { AdminLayout } from "../components/AdminLayout.js";
|
||||||
import { Navigate, Route, Routes } from "react-router-dom";
|
import { Route, Routes } from "react-router-dom";
|
||||||
|
import { AdminOverviewRedirect } from "./AdminRouter.js";
|
||||||
import { Suspense } from "react";
|
import { Suspense } from "react";
|
||||||
|
|
||||||
export function Dashboard({ onLogout, role }: { onLogout: () => void; role: string }) {
|
export function Dashboard({ onLogout, role }: { onLogout: () => void; role: string }) {
|
||||||
@@ -127,7 +128,7 @@ export function Dashboard({ onLogout, role }: { onLogout: () => void; role: stri
|
|||||||
<div className="admin-content-inner">
|
<div className="admin-content-inner">
|
||||||
<Suspense fallback={<LoadingStack />}>
|
<Suspense fallback={<LoadingStack />}>
|
||||||
<Routes>
|
<Routes>
|
||||||
<Route path="/" element={<Navigate to="overview" replace />} />
|
<Route index element={<AdminOverviewRedirect />} />
|
||||||
<Route path="overview" element={<FeatureOverviewPage />} />
|
<Route path="overview" element={<FeatureOverviewPage />} />
|
||||||
<Route
|
<Route
|
||||||
path="publication"
|
path="publication"
|
||||||
@@ -144,7 +145,7 @@ export function Dashboard({ onLogout, role }: { onLogout: () => void; role: stri
|
|||||||
<Route path="members" element={<FeatureMembersPage />} />
|
<Route path="members" element={<FeatureMembersPage />} />
|
||||||
</>
|
</>
|
||||||
)}
|
)}
|
||||||
<Route path="*" element={<Navigate to="overview" replace />} />
|
<Route path="*" element={<AdminOverviewRedirect />} />
|
||||||
</Routes>
|
</Routes>
|
||||||
</Suspense>
|
</Suspense>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import React, { useState } from "react";
|
import React, { useState } from "react";
|
||||||
import { ChangePassword, Login } from "../features/auth/AuthPages.js";
|
import { ChangePassword, Login } from "../features/auth/AuthPages.js";
|
||||||
import { Dashboard } from "./Dashboard.js";
|
import { Dashboard } from "./Dashboard.js";
|
||||||
|
import { AdminRouter } from "./AdminRouter.js";
|
||||||
import { adminMessage as message } from "../components/AdminMessage.js";
|
import { adminMessage as message } from "../components/AdminMessage.js";
|
||||||
|
|
||||||
export function Root() {
|
export function Root() {
|
||||||
@@ -34,5 +35,9 @@ export function Root() {
|
|||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
if (mustChangePassword) return <ChangePassword onComplete={() => setMustChangePassword(false)} />;
|
if (mustChangePassword) return <ChangePassword onComplete={() => setMustChangePassword(false)} />;
|
||||||
return <Dashboard onLogout={logout} role={role} />;
|
return (
|
||||||
|
<AdminRouter>
|
||||||
|
<Dashboard onLogout={logout} role={role} />
|
||||||
|
</AdminRouter>
|
||||||
|
);
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
import assert from "node:assert/strict";
|
||||||
|
import { test } from "node:test";
|
||||||
|
import { createElement } from "react";
|
||||||
|
import { renderToString } from "react-dom/server";
|
||||||
|
import { MemoryRouter, Route, Routes, resolvePath } from "react-router-dom";
|
||||||
|
import { AdminOverviewRedirect, AdminRouter } from "../src/app/AdminRouter.ts";
|
||||||
|
|
||||||
|
test("后台父路由正确匹配所有菜单及深层链接", () => {
|
||||||
|
const pages = [
|
||||||
|
"overview",
|
||||||
|
"publication",
|
||||||
|
"brand",
|
||||||
|
"trust",
|
||||||
|
"services",
|
||||||
|
"inquiries",
|
||||||
|
"workspace",
|
||||||
|
"capabilities",
|
||||||
|
"members",
|
||||||
|
];
|
||||||
|
for (const page of pages) {
|
||||||
|
const html = renderToString(
|
||||||
|
createElement(
|
||||||
|
MemoryRouter,
|
||||||
|
{ initialEntries: [`/admin/${page}`] },
|
||||||
|
createElement(
|
||||||
|
AdminRouter,
|
||||||
|
null,
|
||||||
|
createElement(
|
||||||
|
Routes,
|
||||||
|
null,
|
||||||
|
...pages.map((key) =>
|
||||||
|
createElement(Route, { key, path: key, element: createElement("h1", null, key) }),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
assert.equal(html, `<h1>${page}</h1>`);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
test("默认与无效地址的概览重定向为绝对地址且替换历史记录", () => {
|
||||||
|
const redirect = AdminOverviewRedirect();
|
||||||
|
assert.equal(redirect.props.replace, true);
|
||||||
|
for (const pathname of [
|
||||||
|
"/",
|
||||||
|
"/admin",
|
||||||
|
"/admin/unknown",
|
||||||
|
`/admin/publication${"/overview".repeat(100)}`,
|
||||||
|
]) {
|
||||||
|
assert.equal(resolvePath(redirect.props.to, pathname).pathname, "/admin/overview");
|
||||||
|
}
|
||||||
|
});
|
||||||
Reference in new issue
Block a user