mirror of
https://github.com/ichsankurnia/hats-app.git
synced 2026-12-05 10:46:29 +00:00
7f8e6637e4
Extract NextAuth configuration into a shared library, load admin sidebar items from role permissions, and update related RBAC types and UI.
229 lines
11 KiB
TypeScript
229 lines
11 KiB
TypeScript
import { FC } from 'react';
|
|
import { type Metadata } from 'next';
|
|
import Link from 'next/link';
|
|
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
|
|
import fetchData from '@/lib/fetchData';
|
|
import { Button } from '@/components/ui/button';
|
|
import { type IRole } from '../component/type';
|
|
import RolePermissionRow from "../component/role-permission-row";
|
|
import { PencilLine, ShieldCheck } from 'lucide-react';
|
|
|
|
export const dynamic = 'force-dynamic';
|
|
|
|
export const metadata: Metadata = {
|
|
title: "Role Permission Details | Admin",
|
|
description: "Detailed overview of role permissions and authorized menu actions",
|
|
};
|
|
|
|
type Props = {
|
|
params: Promise<{ id: string }>;
|
|
};
|
|
|
|
async function getRoleData(id: string): Promise<IRole | undefined> {
|
|
try {
|
|
const res = await fetchData(`/role/${id}`, 'no-cache');
|
|
if (res) {
|
|
if (res.role) return res.role;
|
|
return res;
|
|
}
|
|
} catch {
|
|
try {
|
|
const list = await fetchData('/role', 'no-cache');
|
|
const roles = Array.isArray(list) ? list : (list?.role || list?.roles || []);
|
|
return roles.find((r: any) => String(r.id) === String(id));
|
|
} catch {
|
|
return undefined;
|
|
}
|
|
}
|
|
return undefined;
|
|
}
|
|
|
|
const UserRoleDetailPage: FC<Props> = async ({ params }) => {
|
|
const { id } = await params;
|
|
const data: IRole | undefined = await getRoleData(id);
|
|
|
|
const isAdminAllowed = Boolean(data?.allow_admin_panel);
|
|
const rawList = Array.isArray(data?.role_detail) ? data.role_detail : [];
|
|
|
|
const normalizedDetails = rawList.map((item: any) => {
|
|
const menuObj = item.menu || {};
|
|
return {
|
|
pemrission_id: item.id,
|
|
id: menuObj.id,
|
|
name: menuObj.name ?? '',
|
|
path_url: menuObj.path_url ?? '#',
|
|
icon: menuObj.icon ?? '',
|
|
is_admin_menu: Boolean(menuObj.is_admin_menu),
|
|
menu_parent_id: menuObj.menu_parent_id ?? null,
|
|
action: Array.isArray(item.action) ? item.action : [],
|
|
};
|
|
});
|
|
|
|
// Build hierarchy map so parent-child relationships are preserved with indentation
|
|
const detailMap = new Map<number, any>();
|
|
normalizedDetails.forEach(d => { if (d.id !== undefined) detailMap.set(d.id, d); });
|
|
|
|
const getLevel = (item: any): number => {
|
|
if (!item.menu_parent_id || !detailMap.has(item.menu_parent_id)) return 0;
|
|
return getLevel(detailMap.get(item.menu_parent_id)) + 1;
|
|
};
|
|
|
|
// Arrange items hierarchically: root menus first, followed immediately by their children
|
|
function sortHierarchical(items: any[]): any[] {
|
|
const roots = items.filter(i => !i.menu_parent_id || !detailMap.has(i.menu_parent_id));
|
|
const result: any[] = [];
|
|
|
|
function appendNode(node: any) {
|
|
result.push({ ...node, level: getLevel(node) });
|
|
const children = items.filter(c => c.menu_parent_id === node.id);
|
|
children.forEach(appendNode);
|
|
}
|
|
|
|
roots.forEach(appendNode);
|
|
items.forEach(item => {
|
|
if (!result.some(r => r.id === item.id)) {
|
|
result.push({ ...item, level: getLevel(item) });
|
|
}
|
|
});
|
|
|
|
return result;
|
|
}
|
|
|
|
const orderedDetails = sortHierarchical(normalizedDetails);
|
|
|
|
return (
|
|
<div className="flex flex-col space-y-6 pb-12">
|
|
{/* Header & Overview Card */}
|
|
<Card>
|
|
<CardHeader className="flex flex-col sm:flex-row items-start sm:items-center justify-between gap-4">
|
|
<div className="flex items-center space-x-4">
|
|
<Link href="/admin/rbac/user-role">
|
|
<Button variant="outline" size="icon" className="h-9 w-9">
|
|
<i className="ti ti-arrow-left text-lg" />
|
|
</Button>
|
|
</Link>
|
|
<div>
|
|
<CardTitle className="text-xl">Role Permission Details</CardTitle>
|
|
<CardDescription className="mt-1">
|
|
Detailed overview of role permissions and authorized menu actions
|
|
</CardDescription>
|
|
</div>
|
|
</div>
|
|
<Link href={`/admin/rbac/user-role/edit/${id}`}>
|
|
<Button className="bg-his hover:bg-his/90 text-white font-medium">
|
|
<PencilLine /> Edit Role
|
|
</Button>
|
|
</Link>
|
|
</CardHeader>
|
|
<CardContent>
|
|
<div className="grid grid-cols-1 md:grid-cols-2 gap-6 p-4 md:p-6 rounded-lg bg-zinc-50/60 dark:bg-zinc-900/40 border border-zinc-200 dark:border-zinc-800">
|
|
<div className="flex flex-col space-y-1">
|
|
<span className="text-xs text-muted-foreground tracking-wider font-semibold">
|
|
Role Code
|
|
</span>
|
|
<span className="font-mono font-bold text-sm text-zinc-900 dark:text-zinc-100">
|
|
{data?.code || `ROLE-${id}`}
|
|
</span>
|
|
</div>
|
|
<div className="flex flex-col space-y-1">
|
|
<span className="text-xs text-muted-foreground tracking-wider font-semibold">
|
|
Role Name
|
|
</span>
|
|
<span className="font-bold text-sm text-zinc-900 dark:text-zinc-100">
|
|
{data?.name || '-'}
|
|
</span>
|
|
</div>
|
|
<div className="flex flex-col space-y-1">
|
|
<span className="text-xs text-muted-foreground tracking-wider font-semibold">
|
|
Admin Panel Access
|
|
</span>
|
|
<div>
|
|
{isAdminAllowed ? (
|
|
<span className="inline-flex items-center gap-1.5 px-2.5 py-0.5 rounded-full text-xs font-semibold bg-emerald-500/10 text-emerald-600 border border-emerald-500/20 dark:bg-emerald-500/20 dark:text-emerald-300">
|
|
<span className="w-1.5 h-1.5 rounded-full bg-emerald-500" />
|
|
Admin Panel
|
|
</span>
|
|
) : (
|
|
<span className="inline-flex items-center gap-1.5 px-2.5 py-0.5 rounded-full text-xs font-semibold bg-zinc-500/10 text-zinc-500 border border-zinc-500/20 dark:bg-zinc-500/20 dark:text-zinc-400">
|
|
<span className="w-1.5 h-1.5 rounded-full bg-zinc-400" />
|
|
General Access Only
|
|
</span>
|
|
)}
|
|
</div>
|
|
</div>
|
|
<div className="flex flex-col space-y-1">
|
|
<span className="text-xs text-muted-foreground tracking-wider font-semibold">
|
|
Status
|
|
</span>
|
|
<div>
|
|
<span className="inline-flex items-center gap-1.5 px-2.5 py-0.5 rounded-full text-xs font-semibold bg-green-500/10 text-green-600 border border-green-500/20">
|
|
<span className="w-1.5 h-1.5 rounded-full bg-green-500" />
|
|
{data?.status?.name}
|
|
</span>
|
|
</div>
|
|
</div>
|
|
<div className="flex flex-col space-y-1 md:col-span-2">
|
|
<span className="text-xs text-muted-foreground tracking-wider font-semibold">
|
|
Description
|
|
</span>
|
|
<span className="text-sm text-zinc-700 dark:text-zinc-300">
|
|
{data?.description || 'No description provided.'}
|
|
</span>
|
|
</div>
|
|
</div>
|
|
</CardContent>
|
|
</Card>
|
|
|
|
{/* Menu Permissions List */}
|
|
<Card>
|
|
<CardHeader className="pb-3 border-b border-zinc-200 dark:border-zinc-800">
|
|
<div className="flex items-center justify-between">
|
|
<div className="flex items-center gap-2">
|
|
<ShieldCheck className='text-his' />
|
|
<CardTitle className="text-base font-semibold">
|
|
Authorized Menu Permissions ({orderedDetails.length} Menus Allowed)
|
|
</CardTitle>
|
|
</div>
|
|
</div>
|
|
</CardHeader>
|
|
<CardContent className="p-0">
|
|
{orderedDetails.length > 0 ? (
|
|
<div className="divide-y divide-zinc-200/80 dark:divide-zinc-800/80">
|
|
{orderedDetails.map((detail, idx) => (
|
|
<RolePermissionRow
|
|
key={detail.id || idx}
|
|
node={{
|
|
id: detail.id!,
|
|
name: detail.name!,
|
|
path_url: detail.path_url,
|
|
icon: detail.icon,
|
|
is_menu_admin: detail.is_admin_menu,
|
|
level: detail.level,
|
|
hasChildren: normalizedDetails.some(c => c.menu_parent_id === detail.id),
|
|
action: detail.action,
|
|
}}
|
|
mode="readonly"
|
|
index={idx}
|
|
selectedActions={detail.action || []}
|
|
/>
|
|
))}
|
|
</div>
|
|
) : (
|
|
<div className="py-12 text-center text-muted-foreground">
|
|
<i className="ti ti-lock-open text-3xl mb-2 text-zinc-400 block" />
|
|
<p className="text-sm font-medium">No menus or permissions have been configured for this role.</p>
|
|
<Link href={`/admin/rbac/user-role/edit/${id}`} className="mt-3 inline-block">
|
|
<Button size="sm" variant="outline">
|
|
Configure Permissions Now
|
|
</Button>
|
|
</Link>
|
|
</div>
|
|
)}
|
|
</CardContent>
|
|
</Card>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default UserRoleDetailPage;
|