mirror of
https://github.com/ichsankurnia/hats-app.git
synced 2026-12-05 10:46:29 +00:00
7f8e6637e4
Extract NextAuth configuration into a shared library, load admin sidebar items from role permissions, and update related RBAC types and UI.
128 lines
5.2 KiB
TypeScript
128 lines
5.2 KiB
TypeScript
import { Metadata } from "next";
|
|
import Link from "next/link";
|
|
import { ToggleTheme } from "./components/ToggleTheme";
|
|
import { Button } from "@/components/ui/button";
|
|
import Notification from "./components/Notification";
|
|
import { UserNav } from "../(dashboard)/components/UserNav";
|
|
import { House } from "lucide-react";
|
|
import AdminBreadcrumb from "./components/Breadcrumb";
|
|
import { getAccessData } from "@/lib/get-access-data";
|
|
import { IRoleDetail } from "@/types/rbac";
|
|
import { ISideMenu } from "./components/SidebarItem";
|
|
import { lazy, Suspense } from "react";
|
|
import SkeletonSidebar from "./components/SkeletonSidebar";
|
|
|
|
const Sidebar = lazy(() => import("./components/Sidebar"));
|
|
|
|
export const metadata: Metadata = {
|
|
title: "Admin Panel",
|
|
description: "H.A.T.s admin panel page",
|
|
}
|
|
|
|
export default async function AdminLayout({ children }: { children: React.ReactNode }) {
|
|
const data = await getAccessData()
|
|
|
|
const cleanPath = (path: string): string => {
|
|
if (!path || path === '/') return ''
|
|
const sanitized = path.replace(/\/+/g, '/')
|
|
return sanitized.startsWith('/') ? sanitized : `/${sanitized}`
|
|
}
|
|
|
|
const buildSidebarMenu = (items: IRoleDetail[]): ISideMenu[] => {
|
|
// 1. Filter out deleted, inactive, or hidden items
|
|
const adminMenus = items.filter(item => item.menu?.is_admin_menu === true)
|
|
const activeItems = adminMenus.filter((item) => item.menu && !item.menu.deleted_at && item.menu.status_id === 1)
|
|
|
|
const parents: IRoleDetail[] = []
|
|
const childrenMap = new Map<number, IRoleDetail[]>()
|
|
|
|
// 2. Group into parent items and children
|
|
for (const item of activeItems) {
|
|
const parentId = item.menu?.menu_parent_id
|
|
if (parentId === null) {
|
|
parents.push(item)
|
|
} else {
|
|
const children = childrenMap.get(parentId!) || []
|
|
children.push(item)
|
|
childrenMap.set(parentId!, children)
|
|
}
|
|
}
|
|
|
|
// 3. Sorting helper (sort ascending, null values last, fallback to id)
|
|
const sortByOrder = (a: IRoleDetail, b: IRoleDetail) => {
|
|
const sortA = a.menu?.sort ?? Number.MAX_SAFE_INTEGER
|
|
const sortB = b.menu?.sort ?? Number.MAX_SAFE_INTEGER
|
|
if (sortA !== sortB) return sortA - sortB
|
|
return a.menu?.id! - b.menu?.id!
|
|
}
|
|
|
|
parents.sort(sortByOrder)
|
|
|
|
// 4. Map to target structure
|
|
// @ts-ignore
|
|
return parents.map((parent) => {
|
|
const children = childrenMap.get(parent.menu?.id!) || []
|
|
|
|
if (children.length > 0) {
|
|
children.sort(sortByOrder)
|
|
return {
|
|
title: parent.menu?.name!,
|
|
Icon: <i className={`${parent.menu?.icon} text-base`}></i>,
|
|
submenuId: parent.menu?.id,
|
|
submenuItems: children.map((child) => ({
|
|
title: child.menu?.name,
|
|
path: cleanPath(child.menu?.path_url!),
|
|
})),
|
|
}
|
|
}
|
|
|
|
return {
|
|
title: parent.menu?.name!,
|
|
Icon: <i className={`${parent.menu?.icon} text-base`}></i>,
|
|
path: cleanPath(parent.menu?.path_url!),
|
|
}
|
|
})
|
|
}
|
|
|
|
return (
|
|
<>
|
|
<main className="flex flex-col lg:flex-row h-screen overflow-hidden text-sm bg-soft dark:bg-zinc-900">
|
|
<Suspense fallback={<SkeletonSidebar />}>
|
|
<Sidebar menuList={buildSidebarMenu(data.role_detail) || []} />
|
|
</Suspense>
|
|
|
|
{/* CONTENT */}
|
|
<div className="overflow-x-hidden overflow-y-auto h-screen flex flex-col z-0 w-full px-5 pt-5">
|
|
|
|
{/* HEADER */}
|
|
<header className="sticky top-0 z-10 w-full hidden lg:flex justify-between items-center p-5 md:px-6 backdrop-blur-sm bg-white/70 dark:bg-zinc-950/80 rounded-xl border border-zinc-200 text-zinc-950 shadow dark:border-zinc-800 dark:text-zinc-50">
|
|
<AdminBreadcrumb />
|
|
<div className="flex items-center space-x-3">
|
|
<div className="flex items-center space-x-3 border-r pr-3 dark:border-zinc-800">
|
|
<Notification />
|
|
<ToggleTheme />
|
|
</div>
|
|
<Link href='/' title="Go to Homepage">
|
|
<Button variant='outline' size='icon'>
|
|
<House size={32} />
|
|
</Button>
|
|
</Link>
|
|
<UserNav />
|
|
</div>
|
|
</header>
|
|
|
|
<section className="-mt-5 sm:mt-5 mb-10">
|
|
{children}
|
|
</section>
|
|
|
|
<footer className="mt-auto flex items-center justify-between py-5 px-3 sm:px-0 text-xs md:text-sm font-medium text-zinc-400 text-center">
|
|
<p className="mx-auto w-9/12 sm:w-full">© {new Date().getFullYear()} - H.A.T.s | Design & Develop by H.I.S Dev Super Team 😎</p>
|
|
</footer>
|
|
|
|
</div>
|
|
|
|
</main>
|
|
</>
|
|
);
|
|
}
|