diff --git a/apps/admin/src/app/AdminRouter.ts b/apps/admin/src/app/AdminRouter.ts new file mode 100644 index 0000000..d128a7d --- /dev/null +++ b/apps/admin/src/app/AdminRouter.ts @@ -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) }), + ); +} diff --git a/apps/admin/src/app/Dashboard.tsx b/apps/admin/src/app/Dashboard.tsx index 7d3485b..b6b14ea 100644 --- a/apps/admin/src/app/Dashboard.tsx +++ b/apps/admin/src/app/Dashboard.tsx @@ -31,7 +31,8 @@ import { adminMessage as message } from "../components/AdminMessage.js"; import { AdminButton } from "../components/AdminButton.js"; import { AdminSelect } from "../components/AdminSelect.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"; export function Dashboard({ onLogout, role }: { onLogout: () => void; role: string }) { @@ -127,7 +128,7 @@ export function Dashboard({ onLogout, role }: { onLogout: () => void; role: stri
}> - } /> + } /> } /> void; role: stri } /> )} - } /> + } />
diff --git a/apps/admin/src/app/Root.tsx b/apps/admin/src/app/Root.tsx index b3c4c08..ca8a4aa 100644 --- a/apps/admin/src/app/Root.tsx +++ b/apps/admin/src/app/Root.tsx @@ -1,6 +1,7 @@ import React, { useState } from "react"; import { ChangePassword, Login } from "../features/auth/AuthPages.js"; import { Dashboard } from "./Dashboard.js"; +import { AdminRouter } from "./AdminRouter.js"; import { adminMessage as message } from "../components/AdminMessage.js"; export function Root() { @@ -34,5 +35,9 @@ export function Root() { /> ); if (mustChangePassword) return setMustChangePassword(false)} />; - return ; + return ( + + + + ); } diff --git a/apps/admin/test/admin-routing.test.mjs b/apps/admin/test/admin-routing.test.mjs new file mode 100644 index 0000000..2fe2ac5 --- /dev/null +++ b/apps/admin/test/admin-routing.test.mjs @@ -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, `

${page}

`); + } +}); + +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"); + } +});