import React, { useEffect, useState } from 'react'; import { Users, GraduationCap, BookOpen } from 'lucide-react'; import { supabase } from '../../lib/supabase'; import { StatsCard } from '../../components/dashboard/StatsCard'; interface DashboardStats { totalClasses: number; totalTeachers: number; totalStudents: number; recentClasses: any[]; recentStories: any[]; } export function DashboardHome() { const [stats, setStats] = useState({ totalClasses: 0, totalTeachers: 0, totalStudents: 0, recentClasses: [], recentStories: [] }); const [loading, setLoading] = useState(true); useEffect(() => { const fetchDashboardStats = async () => { try { const { data: { session } } = await supabase.auth.getSession(); if (!session?.user?.id) return; const schoolId = session.user.id; // Buscar total de turmas const { count: classesCount } = await supabase .from('classes') .select('*', { count: 'exact', head: true }) .eq('school_id', schoolId); // Buscar total de professores const { count: teachersCount } = await supabase .from('teacher_classes') .select('teacher_id', { count: 'exact', head: true }) .eq('school_id', schoolId); // Buscar total de alunos const { count: studentsCount } = await supabase .from('students') .select('*', { count: 'exact', head: true }) .eq('school_id', schoolId); // Buscar turmas recentes const { data: recentClasses } = await supabase .from('classes') .select(` *, students:students(count) `) .eq('school_id', schoolId) .order('created_at', { ascending: false }) .limit(5); // Buscar histórias recentes const { data: recentStories } = await supabase .from('stories') .select(` *, students:students(name) `) .eq('school_id', schoolId) .order('created_at', { ascending: false }) .limit(5); setStats({ totalClasses: classesCount || 0, totalTeachers: teachersCount || 0, totalStudents: studentsCount || 0, recentClasses: recentClasses || [], recentStories: recentStories || [] }); } catch (error) { console.error('Erro ao buscar estatísticas:', error); } finally { setLoading(false); } }; fetchDashboardStats(); }, []); return (

Dashboard

Últimas Turmas

{loading ? (
{[...Array(3)].map((_, i) => (
))}
) : stats.recentClasses.length === 0 ? (

Nenhuma turma cadastrada

) : (
{stats.recentClasses.map((classItem) => (

{classItem.name}

{classItem.grade} • {classItem.students.count} alunos

))}
)}

Histórias Recentes

{loading ? (
{[...Array(3)].map((_, i) => (
))}
) : stats.recentStories.length === 0 ? (

Nenhuma história registrada

) : (
{stats.recentStories.map((story) => (

{story.title}

por {story.students.name}

))}
)}
); }