"use client"; import { useEffect, useState } from "react"; import { Button, Chip, Input, Label, ListBox, Modal, Select, Spinner, Table, TextField, } from "@heroui/react"; import { Pencil } from "@gravity-ui/icons"; import { api, type UserRow } from "@/lib/api"; import { useSession } from "@/lib/auth-client"; type CreateForm = { name: string; email: string; username: string; password: string; role: string; }; type EditForm = { name: string; username: string; role: string; }; const emptyCreate: CreateForm = { name: "", email: "", username: "", password: "", role: "user" }; const ROLE_OPTIONS = [ { key: "user", label: "用户" }, { key: "admin", label: "管理员" }, ]; export default function UsersPage() { const { data: session } = useSession(); const currentUserId = session?.user?.id; const [users, setUsers] = useState([]); const [loading, setLoading] = useState(true); const [updating, setUpdating] = useState(null); const [createForm, setCreateForm] = useState(emptyCreate); const [editTarget, setEditTarget] = useState(null); const [editForm, setEditForm] = useState({ name: "", username: "", role: "user" }); const [saving, setSaving] = useState(false); const load = async () => { setLoading(true); try { setUsers(await api.users.list()); } catch { // possibly not admin — show empty } finally { setLoading(false); } }; useEffect(() => { load(); }, []); const openEdit = (u: UserRow) => { setEditTarget(u); setEditForm({ name: u.name ?? "", username: u.username ?? "", role: u.role ?? "user" }); }; const handleCreate = async () => { setSaving(true); try { await api.users.create({ name: createForm.name, email: createForm.email, password: createForm.password, username: createForm.username || undefined, role: createForm.role, }); setCreateForm(emptyCreate); await load(); } finally { setSaving(false); } }; const handleEdit = async () => { if (!editTarget) return; setSaving(true); try { await api.users.update(editTarget.id, { name: editForm.name || undefined, username: editForm.username || null, role: editForm.role, }); await load(); } finally { setSaving(false); } }; const toggleBan = async (u: UserRow) => { setUpdating(u.id); try { await api.users.update(u.id, { banned: !u.banned, banReason: u.banned ? null : "管理员封禁", }); await load(); } finally { setUpdating(null); } }; const isEditingSelf = editTarget?.id === currentUserId; return (

用户管理

共 {users.length} 位用户(仅管理员可见)

新增用户 setCreateForm({ ...createForm, name: e.target.value })} /> setCreateForm({ ...createForm, email: e.target.value })} /> setCreateForm({ ...createForm, username: e.target.value })} /> setCreateForm({ ...createForm, password: e.target.value })} />
用户 邮箱 登录名 角色 状态 注册时间 操作 (
{loading ? "加载中…" : "暂无用户或无权限"}
)} > {users.map((u) => (
{u.name ?? "—"}
{u.email} {u.username ?? "—"} {u.role === "admin" ? "管理员" : "用户"} {u.banned ? ( 已封禁 ) : ( 正常 )} {new Date(u.createdAt).toLocaleString("zh-CN")}
{/* Edit button */} 编辑用户 setEditForm({ ...editForm, name: e.target.value }) } /> setEditForm({ ...editForm, username: e.target.value }) } /> {!isEditingSelf && (
)}
{/* Ban / Unban button — disabled for current account */}
))}
); }