'use client'; import React, { FC, useState, useMemo } from 'react'; import { Input } from 'antd'; import { Search, Shield, ShieldCheck } from 'lucide-react'; import { type IMenu } from './type'; import RolePermissionRow from '@/components/rbac/role-permission-row'; 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 using unified RolePermissionRow 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 = Boolean(node.children && node.children.length > 0); const assignedActions = node.action || []; const currentSelectedActions = selectedPermissions[node.id] || []; return ( toggleCollapse(node.id!)} onPermissionToggle={(actionName, checked) => onPermissionToggle(node.id!, actionName, checked) } onSelectAll={() => onMenuSelectAll(node.id!, assignedActions)} onClearAll={() => onMenuClearAll(node.id!)} /> {/* Render children recursively if not collapsed */} {hasChildren && !isCollapsed && node.children!.map(child => renderNode(child))} ); }; return (
{/* Search Bar & Instruction */}
} placeholder="Search menu or permission..." value={searchQuery} onChange={(e) => setSearchQuery(e.target.value)} allowClear className="h-9" />
Tip: Click Select All to quickly grant all permissions for that menu
{/* Section 1: Non-Admin Menus */}

Non-Admin Menus (General Application)

{menus.filter(m => !(m.is_admin_menu)).length} menus
{nonAdminTree.length > 0 ? nonAdminTree.map(node => renderNode(node)) :
No non-admin menus available.
}
{/* Section 2: Admin Menus (Rendered ONLY if allowAdminPanel === true) */} {allowAdminPanel && (

Admin Menus (Admin Panel)

{menus.filter(m => Boolean(m.is_admin_menu)).length} menus
{adminTree.length > 0 ? adminTree.map(node => renderNode(node)) :
No admin panel menus available.
}
)}
); }; export default MenuPermissionTree;