import { useEffect, useState } from 'react'; import { Button } from '@/components/ui/button'; import { Badge } from '@/components/ui/badge'; import { Spinner } from '@/components/ui/spinner'; import { notify } from '@/lib/notify'; import { api } from '../../api/client'; import { useAdminGuard } from '../../layouts/AdminLayout'; import type { User } from '../../api/types'; export default function AdminUsersPage() { const { ready } = useAdminGuard(); const [users, setUsers] = useState([]); const [loading, setLoading] = useState(true); const [page, setPage] = useState(1); const [totalPages, setTotalPages] = useState(1); const load = (p = page) => { setLoading(true); api.adminUsers(p) .then(d => { setUsers(d.users ?? []); setPage(d.page); setTotalPages(d.total_pages); }) .catch(e => notify.error(e.message)) .finally(() => setLoading(false)); }; useEffect(() => { if (ready) load(1); }, [ready]); const toggleBan = async (user: User) => { if (user.role === 'admin') { notify.warning('不能禁言管理员'); return; } try { const r = await api.adminBanUser(user.id, !user.banned); notify.success(r.message); load(); } catch (e: unknown) { notify.error(e instanceof Error ? e.message : '操作失败'); } }; if (!ready) return null; return (

用户管理

查看注册用户,禁言或解除禁言

{loading ? (
) : ( <> {users.map(u => ( ))}
ID 用户名 昵称 角色 状态 注册时间 操作
{u.id} {u.username} {u.nickname} {u.role === 'admin' ? 管理员 : 用户} {u.banned ? 已禁言 : '正常'} {u.created_at ? new Date(u.created_at).toLocaleString('zh-CN') : '—'} {u.role !== 'admin' && ( )}
{users.length === 0 &&
暂无用户
} {totalPages > 1 && (
第 {page} / {totalPages} 页
)} )}
); }