@premieroctet/next-admin
Version:
Next-Admin provides a customizable and turnkey admin dashboard for applications built with Next.js and powered by the Prisma ORM. It aims to simplify the development process by providing a turnkey admin system that can be easily integrated into your proje
54 lines (53 loc) • 2.12 kB
JavaScript
"use client";
import { usePathname, useRouter, useSearchParams } from "next/navigation";
import { useRouter as router_useRouter } from "next/router";
import { useConfig } from "../context/ConfigContext.mjs";
import { createRouterAdapter } from "./context.mjs";
import { createNextAdminComponents } from "./components.mjs";
const useNextRouter = ()=>{
const { isAppDir } = useConfig();
const router = isAppDir ? useRouter() : router_useRouter();
const query = isAppDir ? useSearchParams() : "undefined" != typeof window ? new URLSearchParams(location.search) : new URLSearchParams(router.query);
const pathname = isAppDir ? usePathname() : router.asPath.split("?")[0];
const push = ({ pathname, query })=>{
if (isAppDir) router.push(pathname + (query ? "?" + new URLSearchParams(query).toString() : ""));
else router.push({
pathname,
query
});
};
const replace = ({ pathname, query })=>{
if (isAppDir) router.replace(pathname + (query ? "?" + new URLSearchParams(query).toString() : ""));
else router.replace({
pathname,
query
});
};
const refresh = ()=>{
if (isAppDir) router.refresh();
else router.replace(router.asPath);
};
const setQuery = (queryArg, merge = false)=>{
const currentQuery = Object.fromEntries(query);
const newQuery = merge ? {
...currentQuery,
...queryArg
} : queryArg;
const searchParams = new URLSearchParams();
for (const [key, value] of Object.entries(newQuery))if (value) searchParams.set(key, value);
location.search = searchParams.toString();
};
return {
router: {
push,
replace,
refresh,
setQuery
},
query: Object.fromEntries(query),
pathname
};
};
const NextAdminRouterAdapter = createRouterAdapter(useNextRouter);
const { NextAdmin, MainLayout } = createNextAdminComponents(NextAdminRouterAdapter);
export { MainLayout, NextAdmin, NextAdminRouterAdapter, useNextRouter };