modal redirect fixes for [id]s

This commit is contained in:
makearmy 2025-09-27 16:52:05 -04:00
parent f40f9a4092
commit 7b5f66e946
9 changed files with 1276 additions and 1023 deletions

View file

@ -10,7 +10,7 @@ export function middleware(req: NextRequest) {
const url = req.nextUrl.clone();
const { pathname } = url;
// ── 1) Legacy → Portal mapping (runs before auth gating)
// ── 1) Legacy → Portal / Canonical mapping (runs before auth gating)
const mapped = legacyMap(pathname);
if (mapped) {
url.pathname = mapped.pathname;
@ -47,38 +47,51 @@ export function middleware(req: NextRequest) {
type MapResult = { pathname: string; query?: Record<string, string> };
function legacyMap(pathname: string): MapResult | null {
switch (pathname) {
// Laser settings (old links)
case "/fiber-settings":
return { pathname: "/portal/laser-settings", query: { t: "fiber" } };
case "/uv-settings":
return { pathname: "/portal/laser-settings", query: { t: "uv" } };
case "/co2-galvo-settings":
return { pathname: "/portal/laser-settings", query: { t: "co2-galvo" } };
case "/co2-gantry-settings":
return { pathname: "/portal/laser-settings", query: { t: "co2-gantry" } };
// Materials (both legacy structures)
case "/materials":
return { pathname: "/portal/materials", query: { t: "materials" } };
case "/materials/materials":
return { pathname: "/portal/materials", query: { t: "materials" } };
case "/materials/materials-coatings":
return { pathname: "/portal/materials", query: { t: "materials-coatings" } };
case "/materials-coatings":
return { pathname: "/portal/materials", query: { t: "materials-coatings" } };
// Lasers / Projects / Rigs (legacy)
case "/lasers":
return { pathname: "/portal/laser-sources" };
case "/projects":
return { pathname: "/portal/projects" };
case "/my/rigs":
return { pathname: "/portal/rigs", query: { t: "my" } };
default:
return null;
// 1) DETAIL PAGES: legacy [id] → existing canonical [id] pages
// (keeps working now; we can later switch these to open inside /portal once wrappers exist)
const detailRules: Array<[RegExp, (m: RegExpExecArray) => MapResult]> = [
[/^\/fiber-settings\/([^/]+)\/?$/i, (m) => ({ pathname: `/settings/fiber/${m[1]}` })],
[/^\/uv-settings\/([^/]+)\/?$/i, (m) => ({ pathname: `/settings/uv/${m[1]}` })],
[/^\/co2-galvo-settings\/([^/]+)\/?$/i, (m) => ({ pathname: `/settings/co2-galvo/${m[1]}` })],
[/^\/co2-gantry-settings\/([^/]+)\/?$/i, (m) => ({ pathname: `/settings/co2-gantry/${m[1]}` })],
[/^\/co2gantry-settings\/([^/]+)\/?$/i, (m) => ({ pathname: `/settings/co2-gantry/${m[1]}` })], // old alias
[/^\/materials\/([^/]+)\/?$/i, (m) => ({ pathname: `/materials/materials/${m[1]}` })],
[/^\/materials-coatings\/([^/]+)\/?$/i, (m) => ({ pathname: `/materials/materials-coatings/${m[1]}` })],
// Lasers / Projects detail already live under their canonical routes
// (keep as-is; no redirect needed). If you still want to map legacy, uncomment:
// [/^\/lasers\/([^/]+)\/?$/i, (m) => ({ pathname: `/lasers/${m[1]}` })],
// [/^\/projects\/([^/]+)\/?$/i, (m) => ({ pathname: `/projects/${m[1]}` })],
];
for (const [re, to] of detailRules) {
const m = re.exec(pathname);
if (m) return to(m);
}
// 2) LIST PAGES: legacy lists → portal lists (with tab param) or portal sections
// Accept optional trailing slash variants.
const listRules: Array<[RegExp, MapResult]> = [
[/^\/fiber-settings\/?$/i, { pathname: "/portal/laser-settings", query: { t: "fiber" } }],
[/^\/uv-settings\/?$/i, { pathname: "/portal/laser-settings", query: { t: "uv" } }],
[/^\/co2-galvo-settings\/?$/i, { pathname: "/portal/laser-settings", query: { t: "co2-galvo" } }],
[/^\/co2-ganry-settings\/?$/i, { pathname: "/portal/laser-settings", query: { t: "co2-gantry" } }], // just in case of typos
[/^\/co2-gantry-settings\/?$/i, { pathname: "/portal/laser-settings", query: { t: "co2-gantry" } }],
[/^\/co2gantry-settings\/?$/i, { pathname: "/portal/laser-settings", query: { t: "co2-gantry" } }], // old alias
[/^\/materials\/?$/i, { pathname: "/portal/materials", query: { t: "materials" } }],
[/^\/materials\/materials\/?$/i, { pathname: "/portal/materials", query: { t: "materials" } }],
[/^\/materials\/materials-coatings\/?$/i,
{ pathname: "/portal/materials", query: { t: "materials-coatings" } }],
[/^\/materials-coatings\/?$/i, { pathname: "/portal/materials", query: { t: "materials-coatings" } }],
[/^\/lasers\/?$/i, { pathname: "/portal/laser-sources" }],
[/^\/projects\/?$/i, { pathname: "/portal/projects" }],
[/^\/my\/rigs\/?$/i, { pathname: "/portal/rigs", query: { t: "my" } }],
];
for (const [re, dest] of listRules) {
if (re.test(pathname)) return dest;
}
return null;
}
function isPublicPath(pathname: string): boolean {