From 7185364e4bc7857712225c0aa91fa8168b23c39f Mon Sep 17 00:00:00 2001 From: MananTank Date: Mon, 28 Oct 2024 18:37:45 +0000 Subject: [PATCH] redirect /team/~/... to /team//... (#5158) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Problem solved Short description of the bug fixed or feature added --- ## PR-Codex overview This PR adds functionality to redirect requests from `/team/~/...` to `/team//...`, enhancing user navigation by ensuring that users are directed to the first team's slug instead of a placeholder. ### Detailed summary - Added a check for the path starting with `team` and `~`. - Implemented a call to `getTeams()` to retrieve the list of teams. - If a team exists, modified the path to use the first team's slug. - Redirects the request to the new path format. > ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your question}` --- apps/dashboard/src/middleware.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/apps/dashboard/src/middleware.ts b/apps/dashboard/src/middleware.ts index 1fd94957975..d74965c8a17 100644 --- a/apps/dashboard/src/middleware.ts +++ b/apps/dashboard/src/middleware.ts @@ -1,3 +1,4 @@ +import { getTeams } from "@/api/team"; import { isLoginRequired } from "@/constants/auth"; import { COOKIE_ACTIVE_ACCOUNT, COOKIE_PREFIX_TOKEN } from "@/constants/cookie"; import { type NextRequest, NextResponse } from "next/server"; @@ -93,6 +94,18 @@ export async function middleware(request: NextRequest) { return rewrite(request, `/published-contract${pathname}`); } } + + // redirect /team/~/... to /team//... + if (paths[0] === "team" && paths[1] === "~") { + // TODO - need an API to get the first team to avoid fetching all teams + const teams = await getTeams(); + const firstTeam = teams[0]; + if (firstTeam) { + const modifiedPaths = [...paths]; + modifiedPaths[1] = firstTeam.slug; + return redirect(request, `/${modifiedPaths.join("/")}`); + } + } // END /
/... case // all other cases are handled by the file system router so we just fall through }