diff --git a/ui/app/(prowler)/lighthouse/_lib/redirect-with-search-params.ts b/ui/app/(prowler)/lighthouse/_lib/redirect-with-search-params.ts new file mode 100644 index 0000000000..efac742363 --- /dev/null +++ b/ui/app/(prowler)/lighthouse/_lib/redirect-with-search-params.ts @@ -0,0 +1,25 @@ +import { redirect } from "next/navigation"; + +type SearchParams = Promise>; + +export async function redirectWithSearchParams( + searchParams: SearchParams, + pathname: string, +) { + const params = await searchParams; + const query = new URLSearchParams(); + + Object.entries(params).forEach(([key, value]) => { + if (Array.isArray(value)) { + value.forEach((item) => query.append(key, item)); + return; + } + + if (typeof value === "string") { + query.set(key, value); + } + }); + + const queryString = query.toString(); + redirect(queryString ? `${pathname}?${queryString}` : pathname); +} diff --git a/ui/app/(prowler)/lighthouse/config/connect/page.tsx b/ui/app/(prowler)/lighthouse/config/connect/page.tsx index 145e62627d..a4dd9f1106 100644 --- a/ui/app/(prowler)/lighthouse/config/connect/page.tsx +++ b/ui/app/(prowler)/lighthouse/config/connect/page.tsx @@ -1,4 +1,4 @@ -import { redirect } from "next/navigation"; +import { redirectWithSearchParams } from "@/app/(prowler)/lighthouse/_lib/redirect-with-search-params"; export default async function LighthouseConfigConnectRedirectPage({ searchParams, @@ -7,25 +7,3 @@ export default async function LighthouseConfigConnectRedirectPage({ }) { await redirectWithSearchParams(searchParams, "/lighthouse/settings/connect"); } - -async function redirectWithSearchParams( - searchParams: Promise>, - pathname: string, -) { - const params = await searchParams; - const query = new URLSearchParams(); - - Object.entries(params).forEach(([key, value]) => { - if (Array.isArray(value)) { - value.forEach((item) => query.append(key, item)); - return; - } - - if (typeof value === "string") { - query.set(key, value); - } - }); - - const queryString = query.toString(); - redirect(queryString ? `${pathname}?${queryString}` : pathname); -} diff --git a/ui/app/(prowler)/lighthouse/config/select-model/page.tsx b/ui/app/(prowler)/lighthouse/config/select-model/page.tsx index a8a61366a1..3c9d74e909 100644 --- a/ui/app/(prowler)/lighthouse/config/select-model/page.tsx +++ b/ui/app/(prowler)/lighthouse/config/select-model/page.tsx @@ -1,4 +1,4 @@ -import { redirect } from "next/navigation"; +import { redirectWithSearchParams } from "@/app/(prowler)/lighthouse/_lib/redirect-with-search-params"; export default async function LighthouseConfigSelectModelRedirectPage({ searchParams, @@ -10,25 +10,3 @@ export default async function LighthouseConfigSelectModelRedirectPage({ "/lighthouse/settings/select-model", ); } - -async function redirectWithSearchParams( - searchParams: Promise>, - pathname: string, -) { - const params = await searchParams; - const query = new URLSearchParams(); - - Object.entries(params).forEach(([key, value]) => { - if (Array.isArray(value)) { - value.forEach((item) => query.append(key, item)); - return; - } - - if (typeof value === "string") { - query.set(key, value); - } - }); - - const queryString = query.toString(); - redirect(queryString ? `${pathname}?${queryString}` : pathname); -}