mirror of
https://github.com/ichsankurnia/hats-app.git
synced 2026-12-05 11:06:29 +00:00
b149903b2e
Make parent menus gate child permissions and cascade select or clear actions through descendants. Update role detail rendering for nested menus and the revised API shape, with related RBAC UI and sidebar adjustments.
322 lines
12 KiB
TypeScript
322 lines
12 KiB
TypeScript
'use client';
|
|
|
|
import { ChangeEvent, FC, useEffect, useMemo, useState } from 'react';
|
|
import { MoreHorizontalIcon } from 'lucide-react';
|
|
import { Table } from 'antd';
|
|
import { TableProps, type ColumnsType } from 'antd/es/table';
|
|
import { PlusCircledIcon } from '@radix-ui/react-icons';
|
|
import useAxiosAuth from '@/lib/axios/useAxiosAuth';
|
|
import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from '@/components/ui/dropdown-menu';
|
|
import { Button } from '@/components/ui/button';
|
|
import { AlertDialog, AlertDialogTrigger } from '@/components/ui/alert-dialog';
|
|
import { Input } from '@/components/ui/input';
|
|
import { Dialog } from '@/components/ui/dialog';
|
|
import MenuForm from './Form';
|
|
import ConfirmAsync from './ConfirmAsync';
|
|
import { type IMenu } from './type';
|
|
import RbacBadge from '@/components/ui/rbac-badge';
|
|
import { useRouter } from 'next/navigation';
|
|
|
|
type Props = {
|
|
data: IMenu[];
|
|
};
|
|
|
|
// Transform flat menu list into hierarchical tree for Antd Table
|
|
function buildMenuTree(items: IMenu[]): IMenu[] {
|
|
const itemMap = new Map<number, IMenu>();
|
|
const roots: IMenu[] = [];
|
|
|
|
// Clone all items and add empty children
|
|
items.forEach(item => {
|
|
if (item.id !== undefined) {
|
|
itemMap.set(item.id, { ...item, children: [] });
|
|
}
|
|
});
|
|
|
|
// Wire parents and children
|
|
items.forEach(item => {
|
|
if (item.id === undefined) return;
|
|
const mappedItem = itemMap.get(item.id)!;
|
|
if (item.menu_parent_id && itemMap.has(item.menu_parent_id)) {
|
|
const parent = itemMap.get(item.menu_parent_id)!;
|
|
if (!parent.children) parent.children = [];
|
|
parent.children.push(mappedItem);
|
|
} else {
|
|
roots.push(mappedItem);
|
|
}
|
|
});
|
|
|
|
// Remove empty children arrays so Antd doesn't render expand icons on leaf nodes
|
|
function cleanupEmptyChildren(list: IMenu[]) {
|
|
list.forEach(node => {
|
|
if (node.children && node.children.length === 0) {
|
|
delete node.children;
|
|
} else if (node.children && node.children.length > 0) {
|
|
cleanupEmptyChildren(node.children);
|
|
}
|
|
});
|
|
}
|
|
|
|
cleanupEmptyChildren(roots);
|
|
return roots;
|
|
}
|
|
|
|
const ComponentMenuPage: FC<Props> = ({ data }) => {
|
|
const [rawMenus, setRawMenus] = useState<IMenu[]>([]);
|
|
const [filterData, setFilterData] = useState<IMenu[]>([]);
|
|
const [selectedData, setSelectedData] = useState<IMenu | undefined>(undefined);
|
|
const [showFormDialog, setShowFormDialog] = useState(false);
|
|
const [searchText, setSearchText] = useState('');
|
|
|
|
const axios = useAxiosAuth();
|
|
const router = useRouter();
|
|
|
|
useEffect(() => {
|
|
if (data && Array.isArray(data)) {
|
|
setRawMenus(data);
|
|
setFilterData(data);
|
|
} else {
|
|
setRawMenus([]);
|
|
setFilterData([]);
|
|
}
|
|
}, [data]);
|
|
|
|
const handleDelete = async (id: string | number) => {
|
|
return await axios.delete(`/menu/${id}`).then((res) => res.data);
|
|
};
|
|
|
|
const handleResetForm = () => {
|
|
setSelectedData(undefined);
|
|
setShowFormDialog(false);
|
|
};
|
|
|
|
const handleSearch = (e: ChangeEvent<HTMLInputElement>) => {
|
|
const text = e.target.value;
|
|
setSearchText(text);
|
|
if (text && rawMenus.length > 0) {
|
|
const lower = text.toLowerCase();
|
|
const filtered = rawMenus.filter(
|
|
(item) =>
|
|
item.name?.toLowerCase().includes(lower) ||
|
|
item.path_url?.toLowerCase().includes(lower) ||
|
|
item.description?.toLowerCase().includes(lower) ||
|
|
item.icon?.toLowerCase().includes(lower) ||
|
|
(item.action && item.action.some(act => act.toLowerCase().includes(lower)))
|
|
);
|
|
setFilterData(filtered);
|
|
} else {
|
|
setFilterData(rawMenus);
|
|
}
|
|
};
|
|
|
|
// If searching, display flat filtered items; otherwise display structured tree
|
|
const displayDataSource = useMemo(() => {
|
|
if (searchText.trim().length > 0) {
|
|
return filterData;
|
|
}
|
|
return buildMenuTree(rawMenus);
|
|
}, [searchText, filterData, rawMenus]);
|
|
|
|
const columns: ColumnsType<IMenu> = [
|
|
{
|
|
title: 'Menu Name',
|
|
dataIndex: 'name',
|
|
key: 'name',
|
|
render: (text: string, record: IMenu) => (
|
|
<div className="inline-flex items-center gap-2">
|
|
{record.icon ? (
|
|
<span className="inline-flex items-center justify-center w-6 h-6 rounded bg-zinc-100 dark:bg-zinc-800 text-zinc-700 dark:text-zinc-200 text-sm">
|
|
<i className={record.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">
|
|
#
|
|
</span>
|
|
)}
|
|
<span className="font-semibold text-zinc-900 dark:text-zinc-100">
|
|
{text}
|
|
</span>
|
|
{record.menu_parent_id && searchText && (
|
|
<span className="text-[10px] text-muted-foreground bg-zinc-100 dark:bg-zinc-800 px-1.5 py-0.5 rounded">
|
|
child
|
|
</span>
|
|
)}
|
|
</div>
|
|
),
|
|
},
|
|
{
|
|
title: 'Menu Path',
|
|
dataIndex: 'path_url',
|
|
key: 'path_url',
|
|
render: (path: string) => (
|
|
<span className="font-mono text-xs text-zinc-600 dark:text-zinc-300">
|
|
{path || '/'}
|
|
</span>
|
|
),
|
|
},
|
|
{
|
|
title: 'Type',
|
|
dataIndex: 'is_menu_admin',
|
|
key: 'is_menu_admin',
|
|
width: 140,
|
|
align: 'center',
|
|
render: (_val: any, record: IMenu) => {
|
|
const isAdmin = Boolean(record.is_admin_menu);
|
|
return isAdmin ? (
|
|
<span className="inline-flex items-center px-2 py-0.5 rounded-sm text-[11px] font-semibold 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">
|
|
Admin Menu
|
|
</span>
|
|
) : (
|
|
<span className="inline-flex items-center px-2 py-0.5 rounded-sm text-[11px] font-semibold bg-zinc-500/10 text-zinc-600 border border-zinc-500/20 dark:bg-zinc-500/20 dark:text-zinc-400 dark:border-zinc-500/30">
|
|
Non-Admin Menu
|
|
</span>
|
|
);
|
|
},
|
|
},
|
|
{
|
|
title: 'Permissions List',
|
|
dataIndex: 'action',
|
|
key: 'action',
|
|
render: (actions?: string[]) => {
|
|
if (!actions || actions.length === 0) {
|
|
return <span className="text-xs text-muted-foreground italic">No permissions assigned</span>;
|
|
}
|
|
return (
|
|
<div className="flex flex-wrap gap-x-1 gap-y-1.5 max-w-md">
|
|
{actions.map((act) => (
|
|
<RbacBadge key={act} permission={act} size="sm" />
|
|
))}
|
|
</div>
|
|
);
|
|
},
|
|
},
|
|
{
|
|
title: 'Description',
|
|
dataIndex: 'description',
|
|
key: 'description',
|
|
render: (desc: string) => (
|
|
<span className="text-xs text-muted-foreground line-clamp-2 max-w-xs">
|
|
{desc || '-'}
|
|
</span>
|
|
),
|
|
},
|
|
{
|
|
title: 'Actions',
|
|
key: 'row_action',
|
|
width: 80,
|
|
align: 'center',
|
|
render: (_, record) => (
|
|
<AlertDialog>
|
|
<DropdownMenu>
|
|
<DropdownMenuTrigger asChild>
|
|
<Button variant="ghost" size="icon" className="h-8 w-8">
|
|
<MoreHorizontalIcon className="h-4 w-4" />
|
|
<span className="sr-only">Menu Actions</span>
|
|
</Button>
|
|
</DropdownMenuTrigger>
|
|
<DropdownMenuContent align="end">
|
|
<DropdownMenuItem
|
|
onClick={() => {
|
|
setSelectedData(record);
|
|
setShowFormDialog(true);
|
|
}}
|
|
>
|
|
Edit Menu
|
|
</DropdownMenuItem>
|
|
<AlertDialogTrigger asChild>
|
|
<DropdownMenuItem className="text-rose-600 dark:text-rose-400">
|
|
Delete Menu
|
|
</DropdownMenuItem>
|
|
</AlertDialogTrigger>
|
|
</DropdownMenuContent>
|
|
</DropdownMenu>
|
|
<ConfirmAsync
|
|
title="Delete Menu"
|
|
description={`Are you sure you want to delete menu "${record.name}"? If this menu has child menus or permissions assigned, access may be affected.`}
|
|
onConfirm={() => handleDelete(record.id!)}
|
|
onOkSuccess={() => router.refresh()}
|
|
/>
|
|
</AlertDialog>
|
|
),
|
|
},
|
|
];
|
|
|
|
const tableProps: TableProps<IMenu> = {
|
|
bordered: false,
|
|
size: 'small',
|
|
showHeader: true,
|
|
pagination: searchText ? {
|
|
pageSizeOptions: [10, 20, 50],
|
|
defaultPageSize: 10,
|
|
showSizeChanger: true,
|
|
size: 'middle' as any,
|
|
} : false, // In tree mode, keep full tree expanded/visible for clear hierarchy
|
|
scroll: { x: 'max-content' },
|
|
tableLayout: 'auto',
|
|
};
|
|
|
|
return (
|
|
<>
|
|
<div className="flex flex-col space-y-4">
|
|
<div className="flex flex-col sm:flex-row items-start sm:items-center justify-between gap-3">
|
|
<div className="flex flex-1 items-center space-x-2 w-full sm:w-auto">
|
|
<Input
|
|
placeholder="Search menu, path, or permissions..."
|
|
className="h-8 w-full sm:w-64 md:w-72"
|
|
value={searchText}
|
|
onChange={handleSearch}
|
|
/>
|
|
</div>
|
|
<Button
|
|
size="sm"
|
|
variant="ghost"
|
|
className="h-8 text-his hover:text-his hover:bg-his/10 font-semibold"
|
|
onClick={() => {
|
|
setSelectedData(undefined);
|
|
setShowFormDialog(true);
|
|
}}
|
|
>
|
|
<PlusCircledIcon className="mr-2 h-4 w-4" />
|
|
Create Menu
|
|
</Button>
|
|
</div>
|
|
|
|
<Table
|
|
{...tableProps}
|
|
rowKey="id"
|
|
columns={columns}
|
|
dataSource={displayDataSource}
|
|
locale={{
|
|
emptyText: (
|
|
<div className="py-8 text-center text-muted-foreground">
|
|
<i className="ti ti-category text-3xl mb-2 text-zinc-400 block" />
|
|
<p className="text-sm font-medium">No menus found</p>
|
|
<p className="text-xs text-zinc-400 mt-1">
|
|
{searchText ? 'No menu matching your search.' : 'Click "Create Menu" to register your first navigation menu.'}
|
|
</p>
|
|
</div>
|
|
),
|
|
}}
|
|
/>
|
|
</div>
|
|
|
|
<Dialog
|
|
open={showFormDialog}
|
|
onOpenChange={(state) => {
|
|
if (!state) setSelectedData(undefined);
|
|
setShowFormDialog(state);
|
|
}}
|
|
>
|
|
<MenuForm
|
|
data={selectedData}
|
|
menuList={rawMenus}
|
|
isUpdate={selectedData !== undefined}
|
|
onCancel={handleResetForm}
|
|
/>
|
|
</Dialog>
|
|
</>
|
|
);
|
|
};
|
|
|
|
export default ComponentMenuPage;
|