mirror of
https://github.com/ichsankurnia/hats-app.git
synced 2026-12-05 11:06:29 +00:00
Move RBAC permission row into user-role feature
Keep the role-permission row with the user-role components and update imports to use the canonical UI badge.
This commit is contained in:
@@ -15,9 +15,9 @@ import { Icons } from "@/components/Icons";
|
||||
import { type IMenu } from "./type";
|
||||
import { toast } from "sonner";
|
||||
import { TABLER_ICON_OPTIONS } from "@/lib/tabler-icons";
|
||||
import RbacBadge from "@/components/rbac/rbac-badge";
|
||||
import { Plus, X } from "lucide-react";
|
||||
import PermissionForm from "../../action-permissions/component/Form";
|
||||
import RbacBadge from "@/components/ui/rbac-badge";
|
||||
|
||||
type Props = {
|
||||
isUpdate?: boolean;
|
||||
|
||||
@@ -5,7 +5,7 @@ import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/com
|
||||
import fetchData from '@/lib/fetchData';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { type IRole, type IRoleDetail } from '../component/type';
|
||||
import RolePermissionRow from '@/components/rbac/role-permission-row';
|
||||
import RolePermissionRow from "../component/role-permission-row";
|
||||
|
||||
export const dynamic = 'force-dynamic';
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ 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';
|
||||
import RolePermissionRow from './role-permission-row';
|
||||
|
||||
export interface IMenuPermissionTreeProps {
|
||||
menus: IMenu[];
|
||||
|
||||
@@ -0,0 +1,223 @@
|
||||
'use client';
|
||||
|
||||
import React, { FC } from 'react';
|
||||
import { Checkbox } from 'antd';
|
||||
import { ChevronDown, ChevronRight } from 'lucide-react';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { cn } from '@/lib/utils';
|
||||
import RbacBadge from '../../../../../components/ui/rbac-badge';
|
||||
|
||||
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;
|
||||
Reference in New Issue
Block a user