Files
hats-app/app/admin/rbac/user-role/[id]/page.tsx
T
Ichsan Kurniawan 751b28fab2 Add RBAC menu and role management
Adds permission, menu, and role administration with hierarchical access controls, plus admin breadcrumbs and a redirect from the legacy user-role page. Remove `credentials.md` before committing; it contains server credentials.
2026-11-05 19:01:40 +07:00

204 lines
12 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, type IRoleDetail } from '../component/type';
import RbacBadge from '@/components/ui/rbac-badge';
import { PencilLine } from 'lucide-react';
export const dynamic = 'force-dynamic';
export const metadata: Metadata = {
title: "Detail Hak Akses & Kewenangan | Admin",
description: "Detail of user role and authorized menu actions",
};
type Props = {
params: Promise<{ id: string }>;
};
const UserRoleDetailPage: FC<Props> = async ({ params }) => {
const { id } = await params;
const data: IRole | undefined = await fetchData(`/role/${id}`, 'no-cache');;
const isAdminAllowed = Boolean(data?.allow_admin_panel ?? data?.allow_admin);
const roleDetails: IRoleDetail[] = Array.isArray(data?.role_detail)
? data.role_detail
: Array.isArray(data?.privilege)
? data.privilege.map((p: any) => ({
menu_id: p.menu_id || p.id,
menu_name: p.menu_name || p.name,
menu_path: p.menu_path || p.path || '#',
is_admin_menu: Boolean(p.is_admin_menu),
action: Array.isArray(p.action) ? p.action : [],
}))
: [];
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">Access and Permissions</CardTitle>
<CardDescription className="mt-1">
Informasi detail konfigurasi hak akses dan izin menu untuk peran ini
</CardDescription>
</div>
</div>
<Link href={`/admin/rbac/user-role/edit/${id}`}>
<Button className="bg-his hover:bg-his/90 text-white font-medium">
<PencilLine className='size-3.5' /> Edit Role
</Button>
</Link>
</CardHeader>
<CardContent>
<div className="grid grid-cols-1 md:grid-cols-2 gap-6 p-4 rounded-lg bg-zinc-50/60 dark:bg-zinc-900/40">
<div className="flex flex-col space-y-1">
<span className="text-xs text-muted-foreground tracking-wider font-semibold">
Kode Peran
</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">
Nama Hak Akses
</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">
Akses Admin Panel
</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 || 'Aktif'}
</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">
Deskripsi
</span>
<span className="text-sm text-zinc-700 dark:text-zinc-300">
{data?.description || 'Tidak ada deskripsi.'}
</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">
<i className="ti ti-checklist text-lg text-his" />
<CardTitle className="text-base font-semibold">
Daftar Izin Menu & Aksi ({roleDetails.length} Menu Diizinkan)
</CardTitle>
</div>
</div>
</CardHeader>
<CardContent className="p-0">
{roleDetails.length > 0 ? (
<div className="overflow-x-auto">
<table className="w-full text-sm">
<thead>
<tr className="border-b border-zinc-200 dark:border-zinc-800 bg-zinc-50 dark:bg-zinc-900 text-left text-xs font-semibold text-muted-foreground tracking-wider">
<th className="py-3 px-4 w-14 text-center">No</th>
<th className="py-3 px-4">Menu Aplikasi (Hierarki)</th>
<th className="py-3 px-4">Aksi yang Diizinkan</th>
</tr>
</thead>
<tbody className="divide-y divide-zinc-200 dark:divide-zinc-800">
{roleDetails.map((detail, idx) => (
<tr key={idx} className="hover:bg-zinc-50/50 dark:hover:bg-zinc-900/30 transition-colors">
<td className="py-3.5 px-4 text-center text-xs text-muted-foreground font-medium">
{idx + 1}
</td>
<td className="py-3.5 px-4">
<div className="flex items-center gap-2">
<div>
<span className="font-semibold text-zinc-900 dark:text-zinc-100 text-xs sm:text-sm">
{detail.menu_name}
</span>
<span className="text-xs font-mono text-muted-foreground block">
{detail.menu_path}
</span>
</div>
{detail.is_admin_menu && (
<span className="text-[10px] font-semibold px-1.5 py-0.5 rounded bg-rose-500/10 text-rose-600 dark:text-rose-400">
Admin
</span>
)}
</div>
</td>
<td className="py-3.5 px-4">
<div className="flex flex-wrap gap-1.5">
{detail.action && detail.action.length > 0 ? (
detail.action.map(action => (
<RbacBadge key={action} permission={action} size="sm" />
))
) : (
<span className="text-xs text-muted-foreground italic">
Akses Menu Saja (Tanpa Aksi Spesifik)
</span>
)}
</div>
</td>
</tr>
))}
</tbody>
</table>
</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">Belum ada menu atau izin yang dikonfigurasi untuk role ini.</p>
<Link href={`/admin/rbac/user-role/edit/${id}`} className="mt-3 inline-block">
<Button size="sm" variant="outline">
Konfigurasi Hak Akses Sekarang
</Button>
</Link>
</div>
)}
</CardContent>
</Card>
</div>
);
};
export default UserRoleDetailPage;