"use client"; import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from "@/components/ui/card"; import { getSupabaseClient } from "@/lib/supabase"; import { Loader2 } from "lucide-react"; import { useEffect, useState } from "react"; type voteResult = { total: number; options: { [key: string]: { votes: number; percentage: number; }; }; }; export default function Home() { const [results, setResults] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); useEffect(() => { const fetchResults = async () => { try { setLoading(true); const supabase = getSupabaseClient(); const { data, error } = await supabase.from("votes").select("option"); if (error) throw new Error(error.message); if (!data || data.length === 0) { setResults({ total: 0, options: {} }); return; } const total = data.length; const count: { [key: string]: number } = {}; data.forEach((vote) => { const option = vote.option as string; count[option] = (count[option] || 0) + 1; }); const options: { [key: string]: { votes: number; percentage: number }; } = {}; Object.keys(count).forEach((option) => { if (option !== "NULO") { options[option] = { votes: count[option], percentage: Number.parseFloat( ((count[option] / total) * 100).toFixed(2) ), }; } }); setResults({ total, options }); } catch (error) { console.error("Erro ao buscar resultados:", error); setError("Ocorreu um erro ao carregar os resultados da votação."); } finally { setLoading(false); } }; fetchResults(); }, []); const getBarColor = (option: string) => { if (option === "SIE") return "bg-blue-500"; if (option === "Liderança Jovem") return "bg-green-500"; if (option === "NULO") return "bg-red-500"; return "bg-purple-500"; }; return (
JUSTIÇA ELEITORAL ESTUDANTIL
RESULTADOS DA VOTAÇÃO Estatísticas de participação e votos por candidato {loading ? (

Carregando resultados...

) : error ? (
{error}
) : (

Total de Votos

{results?.total || 0}

eleitores participaram da votação

Distribuição dos Votos

{results && results.total > 0 ? (
{Object.keys(results.options).map((opcao) => (
{opcao} {results.options[opcao].votes} votos ( {results.options[opcao].percentage}%)
))}
) : (

Nenhum voto registrado até o momento.

)}

Os resultados são atualizados automaticamente a cada vez que a página é carregada.

)}
© {new Date().getFullYear()} Justiça Eleitoral Estudantil
); }