mirror of
https://github.com/ichsankurnia/hats-app.git
synced 2026-12-06 22:06:28 +00:00
6ffe7746c8
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.
224 lines
9.8 KiB
TypeScript
224 lines
9.8 KiB
TypeScript
'use client';
|
|
|
|
import React, { FC } from 'react';
|
|
import { Checkbox } from 'antd';
|
|
import { ChevronDown, ChevronRight } from 'lucide-react';
|
|
import { Button } from '@/components/ui/button';
|
|
import RbacBadge from '@/components/rbac/rbac-badge';
|
|
import { cn } from '@/lib/utils';
|
|
|
|
export interface IRolePermissionNode {
|
|
id: number;
|
|
name: string;
|
|
path_url?: string;
|
|
icon?: string;
|
|
is_menu_admin?: boolean;
|
|
level: number;
|
|
hasChildren?: boolean;
|
|
isCollapsed?: boolean;
|
|
action?: string[];
|
|
}
|
|
|
|
export interface IRolePermissionRowProps {
|
|
node: IRolePermissionNode;
|
|
mode?: 'interactive' | 'readonly';
|
|
selectedActions?: string[];
|
|
index?: number;
|
|
onToggleCollapse?: () => void;
|
|
onPermissionToggle?: (actionName: string, checked: boolean) => void;
|
|
onSelectAll?: () => void;
|
|
onClearAll?: () => void;
|
|
}
|
|
|
|
export const RolePermissionRow: FC<IRolePermissionRowProps> = ({
|
|
node,
|
|
mode = 'interactive',
|
|
selectedActions = [],
|
|
index,
|
|
onToggleCollapse,
|
|
onPermissionToggle,
|
|
onSelectAll,
|
|
onClearAll,
|
|
}) => {
|
|
const isInteractive = mode === 'interactive';
|
|
const assignedActions = node.action || [];
|
|
const currentSelected = selectedActions;
|
|
|
|
const isAllSelected = assignedActions.length > 0 && assignedActions.every(a => currentSelected.includes(a));
|
|
const hasAnySelected = currentSelected.length > 0;
|
|
const isIndeterminate = hasAnySelected && !isAllSelected;
|
|
|
|
const indentPx = node.level * 22;
|
|
const actionsIndentPx = indentPx + (isInteractive ? 40 : 28);
|
|
|
|
return (
|
|
<div
|
|
className={cn(
|
|
'flex flex-col py-3 px-3 sm:px-4 border-b border-zinc-200/80 dark:border-zinc-800/80 transition-colors',
|
|
node.level === 0
|
|
? 'bg-zinc-50/70 dark:bg-zinc-900/50'
|
|
: 'bg-white dark:bg-zinc-950/40 hover:bg-zinc-50/40 dark:hover:bg-zinc-900/20'
|
|
)}
|
|
>
|
|
{/* Top row: Hierarchy, Menu Identifier & Row Actions */}
|
|
<div className="flex items-center justify-between gap-3 min-w-0">
|
|
<div
|
|
className="flex items-center gap-2.5 min-w-0 flex-1"
|
|
style={{ paddingLeft: `${indentPx}px` }}
|
|
>
|
|
{/* Expand / Collapse Control */}
|
|
{node.hasChildren ? (
|
|
<button
|
|
type="button"
|
|
onClick={onToggleCollapse}
|
|
aria-label={node.isCollapsed ? 'Expand menu' : 'Collapse menu'}
|
|
className="w-5 h-5 flex items-center justify-center text-zinc-400 hover:text-zinc-700 dark:hover:text-zinc-200 rounded transition-colors shrink-0"
|
|
>
|
|
{node.isCollapsed ? <ChevronRight size={15} /> : <ChevronDown size={15} />}
|
|
</button>
|
|
) : (
|
|
<span className="w-5 shrink-0" />
|
|
)}
|
|
|
|
{/* Submenu Level Marker */}
|
|
{node.level > 0 && (
|
|
<span className="text-zinc-400 dark:text-zinc-600 font-mono text-xs select-none shrink-0">
|
|
↳
|
|
</span>
|
|
)}
|
|
|
|
{/* Checkbox (interactive) or Row Index (readonly) */}
|
|
{isInteractive ? (
|
|
assignedActions.length > 0 && (
|
|
<Checkbox
|
|
checked={isAllSelected}
|
|
indeterminate={isIndeterminate}
|
|
onChange={(e) => {
|
|
if (e.target.checked) {
|
|
onSelectAll?.();
|
|
} else {
|
|
onClearAll?.();
|
|
}
|
|
}}
|
|
className="shrink-0"
|
|
/>
|
|
)
|
|
) : (
|
|
index !== undefined && (
|
|
<span className="text-xs font-mono font-medium text-muted-foreground w-6 text-center shrink-0">
|
|
{index + 1}
|
|
</span>
|
|
)
|
|
)}
|
|
|
|
{/* Icon, Title, Path & Admin Badge */}
|
|
<div className="flex items-center gap-2 min-w-0 flex-wrap">
|
|
{node.icon ? (
|
|
<span className="inline-flex items-center justify-center w-6 h-6 rounded bg-zinc-100 dark:bg-zinc-800 text-zinc-600 dark:text-zinc-300 text-xs shrink-0">
|
|
<i className={node.icon} />
|
|
</span>
|
|
) : (
|
|
<span className="inline-flex items-center justify-center w-6 h-6 rounded bg-zinc-50 dark:bg-zinc-900 text-zinc-400 text-xs font-mono shrink-0">
|
|
#
|
|
</span>
|
|
)}
|
|
|
|
<span className="text-sm font-semibold text-zinc-900 dark:text-zinc-100 truncate">
|
|
{node.name}
|
|
</span>
|
|
|
|
{node.path_url && (
|
|
<span className="text-xs font-mono text-muted-foreground truncate">
|
|
{node.path_url}
|
|
</span>
|
|
)}
|
|
|
|
{node.is_menu_admin && (
|
|
<span className="text-[10px] uppercase font-semibold px-1.5 py-0.5 rounded bg-indigo-500/10 text-indigo-600 border border-indigo-500/20 dark:bg-indigo-500/20 dark:text-indigo-300 dark:border-indigo-500/30 shrink-0">
|
|
Admin
|
|
</span>
|
|
)}
|
|
</div>
|
|
</div>
|
|
|
|
{/* Right: Summary Count & Row Select/Clear Action */}
|
|
<div className="flex items-center gap-2 shrink-0">
|
|
{isInteractive ? (
|
|
assignedActions.length > 0 && (
|
|
<>
|
|
<span className="hidden sm:inline-block text-xs font-mono text-muted-foreground">
|
|
{currentSelected.length} / {assignedActions.length} selected
|
|
</span>
|
|
<Button
|
|
type="button"
|
|
variant="outline"
|
|
size="sm"
|
|
className="h-7 text-xs px-2.5 font-medium"
|
|
onClick={() => {
|
|
if (isAllSelected) {
|
|
onClearAll?.();
|
|
} else {
|
|
onSelectAll?.();
|
|
}
|
|
}}
|
|
>
|
|
{isAllSelected ? 'Clear All' : 'Select All'}
|
|
</Button>
|
|
</>
|
|
)
|
|
) : (
|
|
<span className="text-xs font-mono px-2 py-0.5 rounded bg-zinc-100 dark:bg-zinc-800 text-zinc-600 dark:text-zinc-300">
|
|
{currentSelected.length} permissions
|
|
</span>
|
|
)}
|
|
</div>
|
|
</div>
|
|
|
|
{/* Bottom: Dedicated indented permission badges container */}
|
|
<div
|
|
className="mt-2.5"
|
|
style={{ paddingLeft: `${actionsIndentPx}px` }}
|
|
>
|
|
{assignedActions.length > 0 ? (
|
|
<div className="flex flex-wrap items-center gap-2">
|
|
{assignedActions.map(action => {
|
|
const isChecked = currentSelected.includes(action);
|
|
|
|
if (isInteractive) {
|
|
return (
|
|
<label
|
|
key={action}
|
|
className={cn(
|
|
'inline-flex items-center gap-1.5 px-2.5 py-1 rounded border text-xs cursor-pointer select-none transition-all',
|
|
isChecked
|
|
? 'bg-zinc-100/90 dark:bg-zinc-800/90 border-zinc-300 dark:border-zinc-700 shadow-xs'
|
|
: 'bg-transparent border-zinc-200/60 dark:border-zinc-800/80 hover:border-zinc-300 dark:hover:border-zinc-700 opacity-80 hover:opacity-100'
|
|
)}
|
|
>
|
|
<Checkbox
|
|
checked={isChecked}
|
|
onChange={(e) => onPermissionToggle?.(action, e.target.checked)}
|
|
/>
|
|
<RbacBadge permission={action} size="sm" />
|
|
</label>
|
|
);
|
|
}
|
|
|
|
// Readonly view
|
|
return (
|
|
<RbacBadge key={action} permission={action} size="sm" />
|
|
);
|
|
})}
|
|
</div>
|
|
) : (
|
|
<span className="text-xs text-muted-foreground italic block">
|
|
No permissions assigned
|
|
</span>
|
|
)}
|
|
</div>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default RolePermissionRow;
|