mirror of
https://github.com/ichsankurnia/hats-app.git
synced 2026-12-06 11:46:27 +00:00
Improve RBAC management and permissions UI
Unify interactive and read-only role permission rows, refine role and menu forms and labels, and correct the access-permissions sidebar route. Update role payloads and admin-menu handling.
This commit is contained in:
@@ -1,11 +1,10 @@
|
||||
'use client';
|
||||
|
||||
import React, { FC, useState, useMemo } from 'react';
|
||||
import { Checkbox, Input } from 'antd';
|
||||
import { ChevronDown, ChevronRight, Search, Shield, ShieldCheck } from 'lucide-react';
|
||||
import { Input } from 'antd';
|
||||
import { Search, Shield, ShieldCheck } from 'lucide-react';
|
||||
import { type IMenu } from './type';
|
||||
import RbacBadge from '@/components/ui/rbac-badge';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import RolePermissionRow from '@/components/rbac/role-permission-row';
|
||||
|
||||
export interface IMenuPermissionTreeProps {
|
||||
menus: IMenu[];
|
||||
@@ -71,7 +70,7 @@ export const MenuPermissionTree: FC<IMenuPermissionTreeProps> = ({
|
||||
|
||||
// Separate Non-Admin and Admin menus
|
||||
const { nonAdminTree, adminTree } = useMemo(() => {
|
||||
const nonAdminList = menus.filter(m => !m.is_admin_menu);
|
||||
const nonAdminList = menus.filter(m => !(m.is_admin_menu));
|
||||
const adminList = menus.filter(m => Boolean(m.is_admin_menu));
|
||||
|
||||
return {
|
||||
@@ -106,7 +105,7 @@ export const MenuPermissionTree: FC<IMenuPermissionTreeProps> = ({
|
||||
return matches;
|
||||
}, [menus, searchQuery]);
|
||||
|
||||
// Renders a single menu row and recursively its children
|
||||
// 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)) {
|
||||
@@ -114,127 +113,33 @@ export const MenuPermissionTree: FC<IMenuPermissionTreeProps> = ({
|
||||
}
|
||||
|
||||
const isCollapsed = collapsedIds.has(node.id);
|
||||
const hasChildren = node.children && node.children.length > 0;
|
||||
const hasChildren = Boolean(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 (
|
||||
<React.Fragment key={node.id}>
|
||||
<div
|
||||
className={`flex flex-col sm:flex-row sm:items-center justify-between py-3 px-3 sm:px-4 border-b border-zinc-200 dark:border-zinc-800 transition-colors ${node.level === 0
|
||||
? 'bg-zinc-50/70 dark:bg-zinc-900/60 font-semibold'
|
||||
: 'bg-white dark:bg-zinc-950/40 hover:bg-zinc-50/50 dark:hover:bg-zinc-900/30'
|
||||
}`}
|
||||
>
|
||||
{/* Left: Menu Name, Hierarchy Indent & Parent indicator */}
|
||||
<div
|
||||
className="flex items-center gap-2 sm:gap-3 flex-1 min-w-0 mb-2 sm:mb-0"
|
||||
style={{ paddingLeft: `${node.level * 24}px` }}
|
||||
>
|
||||
{hasChildren ? (
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => toggleCollapse(node.id!)}
|
||||
className="w-5 h-5 flex items-center justify-center text-zinc-400 hover:text-zinc-700 dark:hover:text-zinc-200 rounded"
|
||||
>
|
||||
{isCollapsed ? <ChevronRight size={15} /> : <ChevronDown size={15} />}
|
||||
</button>
|
||||
) : (
|
||||
<span className="w-5" />
|
||||
)}
|
||||
|
||||
{node.level > 0 && (
|
||||
<span className="text-zinc-400 dark:text-zinc-600 font-mono text-xs select-none">
|
||||
↳
|
||||
</span>
|
||||
)}
|
||||
|
||||
<div className="flex items-center gap-2 min-w-0">
|
||||
{assignedActions.length > 0 && (
|
||||
<Checkbox
|
||||
checked={isAllSelected}
|
||||
indeterminate={isIndeterminate}
|
||||
onChange={(e) => {
|
||||
if (e.target.checked) {
|
||||
onMenuSelectAll(node.id!, assignedActions);
|
||||
} else {
|
||||
onMenuClearAll(node.id!);
|
||||
}
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
<div>
|
||||
<div className="flex items-center gap-2">
|
||||
{node.icon && <i className={`${node.icon} text-sm text-zinc-500`} />}
|
||||
<span className="text-xs sm:text-sm font-medium text-zinc-900 dark:text-zinc-100 truncate">
|
||||
{node.name}
|
||||
</span>
|
||||
{node.is_admin_menu && (
|
||||
<span className="text-[10px] uppercase font-semibold px-1.5 py-0.2 rounded bg-indigo-500/10 text-indigo-600 dark:text-indigo-400">
|
||||
Admin
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
<span className="text-[11px] font-mono text-zinc-400 dark:text-zinc-500 block truncate">
|
||||
{node.path_url || '#'}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Right: Available Actions Checkboxes & Row Select All button */}
|
||||
<div className="flex items-center flex-wrap gap-2 sm:gap-3 sm:ml-4">
|
||||
{assignedActions.length > 0 ? (
|
||||
<>
|
||||
<div className="flex flex-wrap items-center gap-2">
|
||||
{assignedActions.map(action => {
|
||||
const isChecked = currentSelectedActions.includes(action);
|
||||
return (
|
||||
<label
|
||||
key={action}
|
||||
className={`inline-flex items-center gap-1.5 px-2 py-1 rounded border text-xs cursor-pointer select-none transition-all ${isChecked
|
||||
? 'bg-zinc-100 dark:bg-zinc-800 border-zinc-300 dark:border-zinc-700 shadow-xs'
|
||||
: 'bg-transparent border-transparent hover:border-zinc-200 dark:hover:border-zinc-800 opacity-65 hover:opacity-100'
|
||||
}`}
|
||||
>
|
||||
<Checkbox
|
||||
checked={isChecked}
|
||||
onChange={(e) =>
|
||||
onPermissionToggle(node.id!, action, e.target.checked)
|
||||
}
|
||||
/>
|
||||
<RbacBadge permission={action} size="sm" />
|
||||
</label>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
size="sm"
|
||||
className="h-7 text-xs px-2.5 ml-2"
|
||||
onClick={() => {
|
||||
if (isAllSelected) {
|
||||
onMenuClearAll(node.id!);
|
||||
} else {
|
||||
onMenuSelectAll(node.id!, assignedActions);
|
||||
}
|
||||
}}
|
||||
>
|
||||
{isAllSelected ? 'Batal' : 'Semua'}
|
||||
</Button>
|
||||
</>
|
||||
) : (
|
||||
<span className="text-xs text-zinc-400 italic px-2">
|
||||
Tidak ada aksi terkonfigurasi
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
<RolePermissionRow
|
||||
node={{
|
||||
id: node.id,
|
||||
name: node.name,
|
||||
path_url: node.path_url,
|
||||
icon: node.icon,
|
||||
is_menu_admin: Boolean(node.is_admin_menu),
|
||||
level: node.level,
|
||||
hasChildren,
|
||||
isCollapsed,
|
||||
action: assignedActions,
|
||||
}}
|
||||
mode="interactive"
|
||||
selectedActions={currentSelectedActions}
|
||||
onToggleCollapse={() => 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))}
|
||||
@@ -244,22 +149,24 @@ export const MenuPermissionTree: FC<IMenuPermissionTreeProps> = ({
|
||||
|
||||
return (
|
||||
<div className="flex flex-col space-y-6">
|
||||
{/* Search Bar */}
|
||||
<div className="flex items-center justify-between gap-4">
|
||||
<div className="w-full sm:w-72">
|
||||
{/* Search Bar & Instruction */}
|
||||
<div className="flex flex-col sm:flex-row sm:items-center justify-between gap-3">
|
||||
<div className="w-full sm:w-64 md:w-72">
|
||||
<Input
|
||||
prefix={<Search size={14} className="text-muted-foreground mr-1" />}
|
||||
placeholder="Cari menu atau permission..."
|
||||
placeholder="Search menu or permission..."
|
||||
value={searchQuery}
|
||||
onChange={(e) => setSearchQuery(e.target.value)}
|
||||
allowClear
|
||||
className="h-9"
|
||||
/>
|
||||
</div>
|
||||
<div className="hidden sm:flex items-center gap-2 text-xs text-muted-foreground">
|
||||
<span>Gunakan tombol</span>
|
||||
<span className="px-1.5 py-0.5 rounded bg-zinc-100 dark:bg-zinc-800 font-mono text-[11px] font-medium">Semua</span>
|
||||
<span>atau centang permission per menu</span>
|
||||
<div className="text-xs text-muted-foreground flex items-center gap-1.5">
|
||||
<span>Tip: Click</span>
|
||||
<span className="px-1.5 py-0.5 rounded bg-zinc-100 dark:bg-zinc-800 font-mono text-[11px] font-semibold text-zinc-700 dark:text-zinc-300">
|
||||
Select All
|
||||
</span>
|
||||
<span>to quickly grant all permissions for that menu</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -272,19 +179,19 @@ export const MenuPermissionTree: FC<IMenuPermissionTreeProps> = ({
|
||||
Non-Admin Menus (General Application)
|
||||
</h4>
|
||||
</div>
|
||||
<span className="text-xs text-muted-foreground font-medium">
|
||||
{menus.filter(m => !m.is_admin_menu).length} menu
|
||||
<span className="text-xs text-muted-foreground font-mono font-medium">
|
||||
{menus.filter(m => !(m.is_admin_menu)).length} menus
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<div className="divide-y divide-zinc-200 dark:divide-zinc-800">
|
||||
{nonAdminTree.length > 0 ? (
|
||||
<div className="divide-y divide-zinc-200/80 dark:divide-zinc-800/80">
|
||||
{nonAdminTree.length > 0 ?
|
||||
nonAdminTree.map(node => renderNode(node))
|
||||
) : (
|
||||
:
|
||||
<div className="py-8 text-center text-xs text-muted-foreground">
|
||||
Belum ada menu Non-Admin yang terdaftar.
|
||||
No non-admin menus available.
|
||||
</div>
|
||||
)}
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -298,19 +205,19 @@ export const MenuPermissionTree: FC<IMenuPermissionTreeProps> = ({
|
||||
Admin Menus (Admin Panel)
|
||||
</h4>
|
||||
</div>
|
||||
<span className="text-xs text-muted-foreground font-medium">
|
||||
{menus.filter(m => Boolean(m.is_admin_menu)).length} menu
|
||||
<span className="text-xs text-muted-foreground font-mono font-medium">
|
||||
{menus.filter(m => Boolean(m.is_admin_menu)).length} menus
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<div className="divide-y divide-zinc-200 dark:divide-zinc-800">
|
||||
{adminTree.length > 0 ? (
|
||||
<div className="divide-y divide-zinc-200/80 dark:divide-zinc-800/80">
|
||||
{adminTree.length > 0 ?
|
||||
adminTree.map(node => renderNode(node))
|
||||
) : (
|
||||
:
|
||||
<div className="py-8 text-center text-xs text-muted-foreground">
|
||||
Belum ada menu Admin Panel yang terdaftar.
|
||||
No admin panel menus available.
|
||||
</div>
|
||||
)}
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
@@ -319,4 +226,3 @@ export const MenuPermissionTree: FC<IMenuPermissionTreeProps> = ({
|
||||
};
|
||||
|
||||
export default MenuPermissionTree;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user