openpanel/packages/nextjs-router/src/legacy-app/use-location.ts
Stefan Pejcic 8595a9f4e5 back
2024-05-08 19:58:53 +02:00

25 lines
724 B
TypeScript

import { usePathname, useSearchParams } from "next/navigation";
import type { IRouterProvider } from "@refinedev/core";
export const useLocation: IRouterProvider["useLocation"] = () => {
const pathname = usePathname();
const query = useSearchParams();
const sliceLength = Math.min(
...[
(pathname ?? "").indexOf("?") > 0
? (pathname ?? "").indexOf("?")
: (pathname ?? "").length,
(pathname ?? "").indexOf("#") > 0
? (pathname ?? "").indexOf("#")
: (pathname ?? "").length,
],
);
return {
pathname: (pathname ?? "").slice(0, sliceLength),
search: `?${query.toString()}`,
};
};