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() // 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: , submenuId: parent.menu?.id, submenuItems: children.map((child) => ({ title: child.menu?.name, path: cleanPath(child.menu?.path_url!), })), } } return { title: parent.menu?.name!, Icon: , path: cleanPath(parent.menu?.path_url!), } }) } return ( <>
}> {/* CONTENT */}
{/* HEADER */}
{children}
); }