first commit
This commit is contained in:
22
calcom/packages/lib/hooks/useCompatSearchParams.tsx
Normal file
22
calcom/packages/lib/hooks/useCompatSearchParams.tsx
Normal file
@@ -0,0 +1,22 @@
|
||||
import { ReadonlyURLSearchParams, useParams, useSearchParams } from "next/navigation";
|
||||
|
||||
export const useCompatSearchParams = () => {
|
||||
const _searchParams = useSearchParams() ?? new URLSearchParams();
|
||||
const params = useParams() ?? {};
|
||||
|
||||
const searchParams = new URLSearchParams(_searchParams.toString());
|
||||
Object.getOwnPropertyNames(params).forEach((key) => {
|
||||
searchParams.delete(key);
|
||||
|
||||
// Though useParams is supposed to return a string/string[] as the key's value but it is found to return undefined as well.
|
||||
// Maybe it happens for pages dir when using optional catch-all routes.
|
||||
const param = params[key] || "";
|
||||
const paramArr = typeof param === "string" ? param.split("/") : param;
|
||||
|
||||
paramArr.forEach((p) => {
|
||||
searchParams.append(key, p);
|
||||
});
|
||||
});
|
||||
|
||||
return new ReadonlyURLSearchParams(searchParams);
|
||||
};
|
||||
Reference in New Issue
Block a user