UploadFile / frontend /src /app /admin /page.tsx
ddosvn's picture
Deploy Upload ThTuanIT Full-stack (Go + Next.js + Python hf_xet + MongoDB)
7c5da52 verified
Raw History Blame Contribute Delete
11.1 kB
"use client";
import React, { useEffect } from "react";
import Link from "next/link";
import { UserPlus, Edit3, Trash2, Shield, Lock } from "lucide-react";
import { useCloud } from "@/context/CloudContext";
import { WorkspaceShell } from "@/components/WorkspaceShell";
function PermissionToggle({
checked,
disabled,
onChange,
}: {
checked: boolean;
disabled?: boolean;
onChange: () => void;
}) {
return (
<button
type="button"
disabled={disabled}
onClick={onChange}
className={`w-10 h-5 rounded-full transition relative inline-flex items-center p-0.5 ${
checked ? "bg-indigo-600" : "bg-slate-800 border border-white/10"
} ${disabled ? "opacity-60 cursor-not-allowed" : "cursor-pointer"}`}
>
<span
className={`w-4 h-4 rounded-full bg-white transition transform ${
checked ? "translate-x-5" : "translate-x-0"
}`}
/>
</button>
);
}
export default function AdminPage() {
const {
token,
currentUser,
isAdmin,
usersList,
loadUsersList,
setEditingUser,
handleQuickTogglePermission,
handleDeleteUser,
} = useCloud();
useEffect(() => {
if (isAdmin && token) {
loadUsersList(token);
}
}, [isAdmin, token]);
return (
<WorkspaceShell>
{currentUser && !isAdmin ? (
<div className="p-8 max-w-lg mx-auto text-center space-y-4 my-auto">
<div className="w-14 h-14 mx-auto rounded-2xl bg-rose-500/15 border border-rose-500/30 flex items-center justify-center text-rose-400">
<Lock className="w-7 h-7" />
</div>
<h1 className="text-xl font-extrabold text-white">Khu vực giới hạn cho Quản trị viên (Admin)</h1>
<p className="text-xs text-slate-400 leading-relaxed">
Tài khoản hiện tại của bạn (<code className="text-indigo-300">@{currentUser.username}</code>) là tài khoản
Thành viên. Vui lòng đăng nhập bằng tài khoản <code className="text-amber-300">admin</code> để quản lý phân
quyền.
</p>
<Link
href="/dashboard"
className="inline-flex px-5 py-2.5 rounded-xl bg-indigo-600 hover:bg-indigo-500 text-xs font-bold text-white transition"
>
Quay về Tổng quan (/dashboard)
</Link>
</div>
) : (
<div className="p-6 space-y-6 max-w-[1400px]">
<div className="glass-panel rounded-3xl p-6 sm:p-8 flex flex-wrap items-center justify-between gap-4 border border-amber-500/30">
<div>
<span className="px-2.5 py-0.5 rounded-full text-[10px] font-mono font-bold uppercase bg-amber-500/20 text-amber-300 border border-amber-500/30">
IAM & Role-Based Access Control • /admin
</span>
<h1 className="text-xl sm:text-2xl font-extrabold text-white mt-1 flex items-center gap-2">
<Shield className="w-6 h-6 text-amber-400" />
<span>Quản trị Người dùng & Phân quyền Hệ thống</span>
</h1>
<p className="text-xs sm:text-sm text-slate-300 mt-1">
Chỉnh sửa trực tiếp quyền <code className="text-amber-300">Tải lên</code>,{" "}
<code className="text-amber-300">Đổi tên/Sửa</code>,{" "}
<code className="text-amber-300">Xóa file</code> hoặc nâng quyền Admin cho bất kỳ tài khoản nào.
</p>
</div>
<button
onClick={() =>
setEditingUser({
isNew: true,
username: "",
display_name: "",
role: "user",
password: "",
permissions: { can_upload: true, can_rename: false, can_delete: false },
active: true,
})
}
className="flex items-center gap-2 px-5 py-3 rounded-2xl bg-amber-500 hover:bg-amber-400 text-slate-950 font-bold text-xs sm:text-sm shadow-lg shadow-amber-500/25 transition"
>
<UserPlus className="w-4 h-4" />
<span>Tạo tài khoản mới</span>
</button>
</div>
<div className="glass rounded-3xl overflow-hidden border border-white/10">
<div className="overflow-x-auto">
<table className="w-full text-left border-collapse">
<thead>
<tr className="border-b border-white/10 text-[11px] font-semibold uppercase tracking-wider text-slate-400 bg-slate-900/70">
<th className="py-4 px-5">Thành viên</th>
<th className="py-4 px-4">Vai trò</th>
<th className="py-4 px-4 text-center">Quyền Upload</th>
<th className="py-4 px-4 text-center">Quyền Sửa/Đổi tên</th>
<th className="py-4 px-4 text-center">Quyền Xóa file</th>
<th className="py-4 px-4 text-center">Trạng thái</th>
<th className="py-4 px-5 text-right">Thao tác</th>
</tr>
</thead>
<tbody className="divide-y divide-white/5 text-sm">
{usersList.map((u) => {
const isRootAdmin = u.username === "admin";
return (
<tr key={u.username} className="hover:bg-white/5 transition">
<td className="py-4 px-5">
<div className="flex items-center gap-3">
<div
className={`w-9 h-9 rounded-xl flex items-center justify-center font-bold text-xs ${
u.role === "admin"
? "bg-amber-500/20 text-amber-300 border border-amber-500/30"
: "bg-indigo-500/20 text-indigo-300 border border-indigo-500/30"
}`}
>
{u.display_name.charAt(0).toUpperCase()}
</div>
<div>
<p className="font-semibold text-white">{u.display_name}</p>
<p className="text-xs font-mono text-slate-400">@{u.username}</p>
</div>
</div>
</td>
<td className="py-4 px-4">
<button
disabled={isRootAdmin}
onClick={() => handleQuickTogglePermission(u, "role")}
className={`px-2.5 py-1 rounded-lg text-xs font-mono font-bold uppercase transition ${
u.role === "admin"
? "bg-amber-500/20 text-amber-300 border border-amber-500/40"
: "bg-slate-800 text-slate-300 border border-white/10 hover:border-indigo-400"
} ${isRootAdmin ? "opacity-80 cursor-not-allowed" : "cursor-pointer"}`}
>
{u.role}
</button>
</td>
<td className="py-4 px-4 text-center">
<PermissionToggle
checked={u.role === "admin" || u.permissions.can_upload}
disabled={u.role === "admin"}
onChange={() => handleQuickTogglePermission(u, "can_upload")}
/>
</td>
<td className="py-4 px-4 text-center">
<PermissionToggle
checked={u.role === "admin" || u.permissions.can_rename}
disabled={u.role === "admin"}
onChange={() => handleQuickTogglePermission(u, "can_rename")}
/>
</td>
<td className="py-4 px-4 text-center">
<PermissionToggle
checked={u.role === "admin" || u.permissions.can_delete}
disabled={u.role === "admin"}
onChange={() => handleQuickTogglePermission(u, "can_delete")}
/>
</td>
<td className="py-4 px-4 text-center">
<button
disabled={isRootAdmin}
onClick={() => handleQuickTogglePermission(u, "active")}
className={`px-2.5 py-1 rounded-full text-[11px] font-semibold transition ${
u.active
? "bg-emerald-500/15 text-emerald-300 border border-emerald-500/30"
: "bg-rose-500/15 text-rose-300 border border-rose-500/30"
}`}
>
{u.active ? "Hoạt động" : "Đã khóa"}
</button>
</td>
<td className="py-4 px-5 text-right">
<div className="inline-flex items-center gap-1.5">
<button
onClick={() =>
setEditingUser({
isNew: false,
username: u.username,
display_name: u.display_name,
role: u.role,
password: "",
permissions: { ...u.permissions },
active: u.active,
})
}
className="px-3 py-1.5 rounded-xl bg-white/5 hover:bg-white/10 text-xs font-medium text-slate-200 flex items-center gap-1.5"
>
<Edit3 className="w-3.5 h-3.5 text-amber-400" />
<span>Chỉnh sửa</span>
</button>
{!isRootAdmin && (
<button
onClick={() => handleDeleteUser(u.username)}
className="p-1.5 rounded-xl bg-rose-500/15 hover:bg-rose-500/30 text-rose-400"
>
<Trash2 className="w-4 h-4" />
</button>
)}
</div>
</td>
</tr>
);
})}
</tbody>
</table>
</div>
</div>
</div>
)}
</WorkspaceShell>
);
}