From 3f54cb04af8c4cbcef0862560680796fe3ccbdde Mon Sep 17 00:00:00 2001 From: KATT Date: Fri, 19 Nov 2021 11:50:01 +0100 Subject: [PATCH] tweak --- lib/hooks/useSearchParams.tsx | 47 -------------------------------- pages/[locale]/[user]/[type].tsx | 4 +-- 2 files changed, 2 insertions(+), 49 deletions(-) delete mode 100644 lib/hooks/useSearchParams.tsx diff --git a/lib/hooks/useSearchParams.tsx b/lib/hooks/useSearchParams.tsx deleted file mode 100644 index 8f613065e6..0000000000 --- a/lib/hooks/useSearchParams.tsx +++ /dev/null @@ -1,47 +0,0 @@ -import { useRouter } from "next/router"; -import { useMemo } from "react"; - -/** - * Ugly hack for using search params as `useRouter()` won't return it on first render - */ -export function useSearchParams() { - const router = useRouter(); - - const { asPath, query, pathname } = router; - const value = useMemo(() => { - // if we have query params from the router, let's return that - if (Object.keys(query).length > 0) { - return query; - } - // split path by `/` or `?` - const pathnameParts = pathname.split(/\/|\?/); - const asPathParts = asPath.split(/\/|\?/); - - // actual query object that we wanna use - const actualQuery: typeof query = {}; - for (let index = 0; index < pathnameParts.length; index++) { - const part = pathnameParts[index]; - if (!part.startsWith("[") || !part.endsWith("]")) { - continue; - } - // extract real query param from `post/[id]` style routes - - // removes first and last character - const key = part.slice(1, -1); - - // append to "actual query" - actualQuery[key] = asPathParts[index]; - } - - // get search params that are in the url - const searchParams = new URLSearchParams(asPath.split("?").pop() || ""); - const searchParamsAsObject = Object.fromEntries(searchParams); - - for (const key in searchParamsAsObject) { - actualQuery[key] = searchParamsAsObject[key]; - } - return actualQuery; - }, [asPath, query, pathname]); - - return value; -} diff --git a/pages/[locale]/[user]/[type].tsx b/pages/[locale]/[user]/[type].tsx index 015a6b0687..acc7d60924 100644 --- a/pages/[locale]/[user]/[type].tsx +++ b/pages/[locale]/[user]/[type].tsx @@ -1,7 +1,7 @@ /* eslint-disable @typescript-eslint/no-non-null-assertion */ import { GetStaticPaths, GetStaticPropsContext } from "next"; +import { useRouter } from "next/router"; -import { useSearchParams } from "@lib/hooks/useSearchParams"; import { inferQueryOutput, trpc } from "@lib/trpc"; import { inferSSRProps } from "@lib/types/inferSSRProps"; @@ -15,7 +15,7 @@ type TEventTypeByUsername = NonNullable) { - const searchParams = useSearchParams(); + const searchParams = useRouter().query; const username = (props.username || searchParams.username) as string; const slug = (props.slug || searchParams.slug) as string;