2023-08-02 09:35:48 +00:00
|
|
|
import { useSearchParams } from "next/navigation";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An alternative to Object.fromEntries that allows duplicate keys.
|
|
|
|
*/
|
2023-10-10 21:50:15 +00:00
|
|
|
function fromEntriesWithDuplicateKeys(entries: IterableIterator<[string, string]> | null) {
|
2023-08-02 09:35:48 +00:00
|
|
|
const result: Record<string, string | string[]> = {};
|
2023-10-10 21:50:15 +00:00
|
|
|
|
|
|
|
if (entries === null) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-08-02 09:35:48 +00:00
|
|
|
// Consider setting atleast ES2015 as target
|
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
|
|
// @ts-ignore
|
|
|
|
for (const [key, value] of entries) {
|
|
|
|
if (result.hasOwnProperty(key)) {
|
|
|
|
let currentValue = result[key];
|
|
|
|
if (!Array.isArray(currentValue)) {
|
|
|
|
currentValue = [currentValue];
|
|
|
|
}
|
|
|
|
currentValue.push(value);
|
|
|
|
result[key] = currentValue;
|
|
|
|
} else {
|
|
|
|
result[key] = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This hook returns the query object from the router. It is an attempt to
|
|
|
|
* keep the original query object from the old useRouter hook.
|
|
|
|
* At least until everything is properly migrated to the new router.
|
|
|
|
* @returns {Object} routerQuery
|
|
|
|
*/
|
|
|
|
export const useRouterQuery = () => {
|
|
|
|
const searchParams = useSearchParams();
|
2023-10-10 21:50:15 +00:00
|
|
|
const routerQuery = fromEntriesWithDuplicateKeys(searchParams?.entries() ?? null);
|
2023-08-02 09:35:48 +00:00
|
|
|
return routerQuery;
|
|
|
|
};
|