import { useEffect, useState, useCallback } from 'react' import { useAuth } from '../hooks/useAuth' import { getUsers, updateUser, approveUser } from '../api/users' import Table, { type TableColumn } from '../components/ui/Table' import Button from '../components/ui/Button' import Badge from '../components/ui/Badge' import toast from '../components/ui/Toast' interface UserRecord { id: string username: string email: string role: string is_active: boolean is_approved: boolean created_at: string } const ROLE_OPTIONS = ['owner', 'operator', 'viewer'] export default function UserManagementPage() { const { user: me } = useAuth() const [users, setUsers] = useState([]) const [page, setPage] = useState(1) const [hasMore, setHasMore] = useState(true) const [loading, setLoading] = useState(false) const fetchUsers = useCallback(async (p: number) => { setLoading(true) try { const res = await getUsers({ page: p, page_size: 20 }) const data: UserRecord[] = res.data setUsers(data) setHasMore(data.length === 20) } catch { toast.error('加载用户列表失败') } finally { setLoading(false) } }, []) useEffect(() => { fetchUsers(page) }, [page, fetchUsers]) const handleRoleChange = async (userId: string, newRole: string) => { try { await updateUser(userId, { role: newRole }) toast.success('角色已更新') fetchUsers(page) } catch { toast.error('更新角色失败') } } const handleApprove = async (userId: string) => { try { await approveUser(userId) toast.success('用户已审批通过') fetchUsers(page) } catch { toast.error('审批失败') } } const isOwner = me?.role === 'owner' const columns: TableColumn[] = [ { key: 'username', label: '用户名' }, { key: 'email', label: '邮箱' }, { key: 'role', label: '角色' }, { key: 'status', label: '状态' }, { key: 'approve', label: '审批状态' }, { key: 'created_at', label: '创建时间' }, ...(isOwner ? [{ key: 'actions', label: '操作' }] : []), ] const data = users.map((u) => ({ username: u.username, email: u.email, role: isOwner ? ( ) : ( {u.role} ), status: u.is_active ? ( active ) : ( inactive ), approve: u.is_approved ? ( 已审批 ) : ( 待审批 ), created_at: new Date(u.created_at).toLocaleString('zh-CN'), actions: isOwner ? (
{!u.is_approved && ( )}
) : null, })) return (

用户管理

用户管理与权限

{/* Pagination */}
第 {page} 页
) }