// src/components/SenadoresWidget.tsx import { useState, useEffect, useMemo } from 'react'; import { useQuery } from '@tanstack/react-query'; import Select from 'react-select'; // Importamos react-select import { getMunicipios, getResultadosPorMunicipio, getConfiguracionPublica, assetBaseUrl } from '../apiService'; // Usamos las funciones genéricas import type { MunicipioSimple, ResultadoTicker } from '../types/types'; import { ImageWithFallback } from './ImageWithFallback'; import './TickerWidget.css'; const formatPercent = (num: number) => `${(num || 0).toFixed(2).replace('.', ',')}%`; // Estilos para el selector, podemos moverlos a un archivo común más adelante const customSelectStyles = { control: (base: any) => ({ ...base, minWidth: '220px', border: '1px solid #ced4da' }), menu: (base: any) => ({ ...base, zIndex: 10 }), }; // Constante para la categoría de este widget const CATEGORIA_ID = 5; // Senadores export const SenadoresWidget = () => { const [selectedMunicipio, setSelectedMunicipio] = useState<{ value: string; label: string } | null>(null); const { data: configData } = useQuery({ queryKey: ['configuracionPublica'], queryFn: getConfiguracionPublica, staleTime: 0, }); // Usamos la clave de configuración del Ticker, ya que es para Senadores/Diputados const cantidadAMostrar = parseInt(configData?.TickerResultadosCantidad || '5', 10); const { data: municipios = [], isLoading: isLoadingMunicipios } = useQuery({ queryKey: ['municipios', CATEGORIA_ID], // Key única para la caché queryFn: () => getMunicipios(CATEGORIA_ID), // Pasamos el ID de la categoría }); // useEffect para establecer "ALBERTI" por defecto useEffect(() => { if (municipios.length > 0 && !selectedMunicipio) { const laPlata = municipios.find(m => m.nombre.toUpperCase() === 'ALBERTI'); if (laPlata) { setSelectedMunicipio({ value: laPlata.id, label: laPlata.nombre }); } } }, [municipios, selectedMunicipio]); const municipioOptions = useMemo(() => municipios .map(m => ({ value: m.id, label: m.nombre })) .sort((a, b) => a.label.localeCompare(b.label)), [municipios]); const { data: resultados, isLoading: isLoadingResultados } = useQuery({ queryKey: ['resultadosMunicipio', selectedMunicipio?.value, CATEGORIA_ID], queryFn: () => getResultadosPorMunicipio(selectedMunicipio!.value, CATEGORIA_ID), enabled: !!selectedMunicipio, }); // Lógica para "Otros" let displayResults: ResultadoTicker[] = resultados || []; if (resultados && resultados.length > cantidadAMostrar) { const topParties = resultados.slice(0, cantidadAMostrar - 1); const otherParties = resultados.slice(cantidadAMostrar - 1); const otrosPorcentaje = otherParties.reduce((sum, party) => sum + (party.porcentaje || 0), 0); const otrosEntry: ResultadoTicker = { id: `otros-senadores-${selectedMunicipio?.value}`, nombre: 'Otros', nombreCorto: 'Otros', color: '#888888', logoUrl: null, votos: 0, porcentaje: otrosPorcentaje, }; displayResults = [...topParties, otrosEntry]; } else if (resultados) { displayResults = resultados.slice(0, cantidadAMostrar); } return (

SENADORES POR MUNICIPIO