"use client"; import Link from "next/link"; import { useMemo, useState } from "react"; import type { Poll } from "@/lib/api"; import { cls, dateShort, int, modeLabel } from "@/lib/format"; import { PARTY_IDS, partyLabel, partyVar } from "@/lib/parties"; import { PollRowMobile } from "./forecast-blocks"; export function PollsTable({ polls }: { polls: Poll[] }) { const [firm, setFirm] = useState("all"); const [year, setYear] = useState("all"); const [onlyPrimary, setOnlyPrimary] = useState(false); const [shown, setShown] = useState(25); const firms = useMemo(() => Array.from(new Map(polls.map((p) => [p.pollster.id, p.pollster.name])).entries()), [polls]); const years = useMemo(() => Array.from(new Set(polls.map((p) => p.fieldworkEnd.slice(0, 4)))).sort().reverse(), [polls]); const rows = polls.filter((p) => (firm === "all" || p.pollster.id === firm) && (year === "all" || p.fieldworkEnd.startsWith(year)) && (!onlyPrimary || p.sourceTier === "primary")); return (
{rows.length} sondages
{rows.slice(0, shown).map((p) => )} {rows.length > shown && }
{PARTY_IDS.map((id) => )} {rows.map((p) => { const lead = Object.entries(p.results).filter(([k]) => k !== "aut").sort((a, b) => b[1] - a[1])[0]?.[0]; return ( {PARTY_IDS.map((id) => )} ); })}
FirmeTerrainn{partyLabel(id)}AutresPoidsSource
{p.pollster.name}{p.sponsor &&
{p.sponsor}
}
{p.fieldworkStart ? `${dateShort(p.fieldworkStart)} – ` : ""}{dateShort(p.fieldworkEnd)} {int(p.sampleSize)}{p.results[id] !== undefined ? p.results[id].toFixed(p.results[id] % 1 ? 1 : 0).replace(".", ",") : "—"}{p.results["aut"] !== undefined ? p.results["aut"].toFixed(0) : "—"} {p.weight ? p.weight.weight.toFixed(2) : "—"} {p.sourceTier === "primary" ? PDF archivé : index}{p.excluded && exclu}
); }