'use client'; import React, { FC, useState, useMemo } from 'react'; import { Checkbox, Input } from 'antd'; import { ChevronDown, ChevronRight, Search, Shield, ShieldCheck } from 'lucide-react'; import { type IMenu } from './type'; import RbacBadge from '@/components/ui/rbac-badge'; import { Button } from '@/components/ui/button'; export interface IMenuPermissionTreeProps { menus: IMenu[]; allowAdminPanel: boolean; // Map of menu_id -> array of selected action names selectedPermissions: Record; onPermissionToggle: (menuId: number, actionName: string, checked: boolean) => void; onMenuSelectAll: (menuId: number, allActions: string[]) => void; onMenuClearAll: (menuId: number) => void; } interface TreeNode extends IMenu { children?: TreeNode[]; level: number; } // Transform flat list to tree function buildTree(items: IMenu[], parentId: number | null = null, level: number = 0): TreeNode[] { const result: TreeNode[] = []; const directChildren = items.filter(i => { if (parentId === null) { return i.menu_parent_id === null || i.menu_parent_id === undefined; } return i.menu_parent_id === parentId; }); for (const item of directChildren) { if (item.id === undefined) continue; const children = buildTree(items, item.id, level + 1); const node: TreeNode = { ...item, level, children: children.length > 0 ? children : undefined, }; result.push(node); } return result; } export const MenuPermissionTree: FC = ({ menus, allowAdminPanel, selectedPermissions, onPermissionToggle, onMenuSelectAll, onMenuClearAll, }) => { const [searchQuery, setSearchQuery] = useState(''); const [collapsedIds, setCollapsedIds] = useState>(new Set()); const toggleCollapse = (id: number) => { setCollapsedIds(prev => { const next = new Set(prev); if (next.has(id)) { next.delete(id); } else { next.add(id); } return next; }); }; // Separate Non-Admin and Admin menus const { nonAdminTree, adminTree } = useMemo(() => { const nonAdminList = menus.filter(m => !m.is_admin_menu); const adminList = menus.filter(m => Boolean(m.is_admin_menu)); return { nonAdminTree: buildTree(nonAdminList), adminTree: buildTree(adminList), }; }, [menus]); // Filter matching IDs when searching const searchMatchIds = useMemo(() => { if (!searchQuery.trim()) return null; const lower = searchQuery.toLowerCase(); const matches = new Set(); menus.forEach(m => { const matchName = m.name?.toLowerCase().includes(lower); const matchPath = m.path_url?.toLowerCase().includes(lower); const matchActions = m.action?.some(a => a.toLowerCase().includes(lower)); if (matchName || matchPath || matchActions) { if (m.id !== undefined) matches.add(m.id); // Also add parent IDs so parent stays visible let parentId = m.menu_parent_id; while (parentId) { matches.add(parentId); const parent = menus.find(p => p.id === parentId); parentId = parent?.menu_parent_id; } } }); return matches; }, [menus, searchQuery]); // Renders a single menu row and recursively its children const renderNode = (node: TreeNode) => { if (node.id === undefined) return null; if (searchMatchIds && !searchMatchIds.has(node.id)) { return null; } const isCollapsed = collapsedIds.has(node.id); const hasChildren = node.children && node.children.length > 0; const assignedActions = node.action || []; const currentSelectedActions = selectedPermissions[node.id] || []; const isAllSelected = assignedActions.length > 0 && assignedActions.every(a => currentSelectedActions.includes(a)); const hasAnySelected = currentSelectedActions.length > 0; const isIndeterminate = hasAnySelected && !isAllSelected; return (
{/* Left: Menu Name, Hierarchy Indent & Parent indicator */}
{hasChildren ? ( ) : ( )} {node.level > 0 && ( ↳ )}
{assignedActions.length > 0 && ( { if (e.target.checked) { onMenuSelectAll(node.id!, assignedActions); } else { onMenuClearAll(node.id!); } }} /> )}
{node.icon && } {node.name} {node.is_admin_menu && ( Admin )}
{node.path_url || '#'}
{/* Right: Available Actions Checkboxes & Row Select All button */}
{assignedActions.length > 0 ? ( <>
{assignedActions.map(action => { const isChecked = currentSelectedActions.includes(action); return ( ); })}
) : ( Tidak ada aksi terkonfigurasi )}
{/* Render children recursively if not collapsed */} {hasChildren && !isCollapsed && node.children!.map(child => renderNode(child))}
); }; return (
{/* Search Bar */}
} placeholder="Cari menu atau permission..." value={searchQuery} onChange={(e) => setSearchQuery(e.target.value)} allowClear className="h-9" />
Gunakan tombol Semua atau centang permission per menu
{/* Section 1: Non-Admin Menus */}

Non-Admin Menus (General Application)

{menus.filter(m => !m.is_admin_menu).length} menu
{nonAdminTree.length > 0 ? ( nonAdminTree.map(node => renderNode(node)) ) : (
Belum ada menu Non-Admin yang terdaftar.
)}
{/* Section 2: Admin Menus (Rendered ONLY if allowAdminPanel === true) */} {allowAdminPanel && (

Admin Menus (Admin Panel)

{menus.filter(m => Boolean(m.is_admin_menu)).length} menu
{adminTree.length > 0 ? ( adminTree.map(node => renderNode(node)) ) : (
Belum ada menu Admin Panel yang terdaftar.
)}
)}
); }; export default MenuPermissionTree;