import React, { useEffect, useMemo, useState } from 'react'; import { Loader2, RefreshCw, ShieldCheck, UserCheck, UserX, Users } from 'lucide-react'; import { adminApi, ManagedUser } from '../services/api'; import { useAuth } from '../context/AuthContext'; import { useToast } from '../context/ToastContext'; import { useNavigate } from 'react-router-dom'; const roleLabel: Record = { admin: 'Superadmin', instructor: 'Instrutor', student: 'Aluno', }; export const SuperAdmin: React.FC = () => { const { user } = useAuth(); const { showToast } = useToast(); const navigate = useNavigate(); const [users, setUsers] = useState([]); const [isLoading, setIsLoading] = useState(true); const [updatingUserId, setUpdatingUserId] = useState(null); const [query, setQuery] = useState(''); const loadUsers = async () => { setIsLoading(true); try { const response = await adminApi.listUsers(); setUsers(response.data); } catch { showToast('Não foi possível carregar os usuários.', 'error'); } finally { setIsLoading(false); } }; useEffect(() => { void loadUsers(); }, []); const filteredUsers = useMemo(() => { const normalized = query.trim().toLowerCase(); if (!normalized) return users; return users.filter((account) => account.name.toLowerCase().includes(normalized) || account.email.toLowerCase().includes(normalized), ); }, [query, users]); const updateUser = async (account: ManagedUser, update: Partial>) => { setUpdatingUserId(account.id); try { const response = await adminApi.updateUser(account.id, update); setUsers((current) => current.map((item) => item.id === account.id ? response.data : item)); showToast('Usuário atualizado.', 'success'); } catch { showToast('Não foi possível atualizar este usuário.', 'error'); } finally { setUpdatingUserId(null); } }; const activeUsers = users.filter((account) => account.isActive).length; const instructorUsers = users.filter((account) => account.role === 'instructor' && account.isActive).length; return (
Superadmin

Painel Administrativo

Gerencie os acessos da plataforma. Cursos e aulas continuam no Painel do Instrutor.

} label="Usuários cadastrados" value={users.length} /> } label="Contas ativas" value={activeUsers} /> } label="Instrutores ativos" value={instructorUsers} />

Usuários

setQuery(event.target.value)} placeholder="Buscar por nome ou e-mail" className="w-full sm:w-80 rounded-xl bg-zinc-900 border border-white/10 px-4 py-2.5 text-sm text-white placeholder:text-gray-500 focus:outline-none focus:border-orange-500" />
{isLoading ? (
) : filteredUsers.length === 0 ? (
Nenhum usuário encontrado.
) : (
{filteredUsers.map((account) => { const isCurrentUser = account.id === user?.id; const isUpdating = updatingUserId === account.id; return ( ); })}
Usuário Perfil Status Cadastro
{account.name}
{account.email}
{new Intl.DateTimeFormat('pt-BR').format(new Date(account.createdAt))}
)}
); }; const StatCard: React.FC<{ icon: React.ReactNode; label: string; value: number }> = ({ icon, label, value }) => (
{icon}{label}
{value}
);