"use client"; import { useTranslations } from "next-intl"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; interface QuestionResult { id: number; question: string; options: string; votes: string[]; // array of answer strings } interface Props { questions: QuestionResult[]; } export function PollResultsView({ questions }: Props) { const t = useTranslations("pages.admin.polls"); return ( {t("resultsTitle")} {questions.map((q) => { const options = q.options.split("\n").filter(Boolean); const totalVotes = q.votes.length; // Count votes per option const voteCounts = new Map(); for (const opt of options) voteCounts.set(opt, 0); for (const vote of q.votes) { voteCounts.set(vote, (voteCounts.get(vote) ?? 0) + 1); } return (

{q.question}

{options.length > 0 ? (
{options.map((opt) => { const count = voteCounts.get(opt) ?? 0; const pct = totalVotes > 0 ? Math.round((count / totalVotes) * 100) : 0; return (
{opt} {count} votes ({pct}%)
); })}
) : (
{totalVotes} free-text responses
)}

{totalVotes} total vote{totalVotes !== 1 ? "s" : ""}

); })} ); }