import React, { useState, useRef } from 'react'; import { Box, CircularProgress, Alert, Paper, Typography, Dialog, DialogTitle, DialogContent, IconButton } from '@mui/material'; import { PiChartLineUpBold } from "react-icons/pi"; import CloseIcon from '@mui/icons-material/Close'; import ArrowUpwardIcon from '@mui/icons-material/ArrowUpward'; import ArrowDownwardIcon from '@mui/icons-material/ArrowDownward'; import RemoveIcon from '@mui/icons-material/Remove'; import { GiSunflower, GiWheat, GiCorn, GiGrain } from "react-icons/gi"; import { TbGrain } from "react-icons/tb"; import type { CotizacionGrano } from '../models/mercadoModels'; import { useApiData } from '../hooks/useApiData'; import { formatInteger, formatDateOnly } from '../utils/formatters'; import { GrainsHistoricalChartWidget } from './GrainsHistoricalChartWidget'; const getGrainIcon = (nombre: string) => { switch (nombre.toLowerCase()) { case 'girasol': return ; case 'trigo': return ; case 'sorgo': return ; case 'maiz': return ; default: return ; } }; // Subcomponente para una única tarjeta de grano const GranoCard = ({ grano, onChartClick }: { grano: CotizacionGrano, onChartClick: (event: React.MouseEvent) => void }) => { const isPositive = grano.variacionPrecio > 0; const isNegative = grano.variacionPrecio < 0; const color = isPositive ? 'success.main' : isNegative ? 'error.main' : 'text.secondary'; const Icon = isPositive ? ArrowUpwardIcon : isNegative ? ArrowDownwardIcon : RemoveIcon; return ( {getGrainIcon(grano.nombre)} {grano.nombre} ${formatInteger(grano.precio)} por Tonelada {formatInteger(grano.variacionPrecio)} Operación: {formatDateOnly(grano.fechaOperacion)} ); }; export const GranosCardWidget = () => { const { data, loading, error } = useApiData('/mercados/granos'); const [selectedGrano, setSelectedGrano] = useState(null); const triggerButtonRef = useRef(null); const handleChartClick = (nombreGrano: string, event: React.MouseEvent) => { triggerButtonRef.current = event.currentTarget; setSelectedGrano(nombreGrano); }; const handleCloseDialog = () => { setSelectedGrano(null); setTimeout(() => { triggerButtonRef.current?.focus(); }, 0); }; if (loading) { return ; } if (error) { return {error}; } if (!data || data.length === 0) { return No hay datos de granos disponibles.; } return ( <> {data.map((grano) => ( handleChartClick(grano.nombre, event)} /> ))} theme.palette.grey[500], backgroundColor: 'white', boxShadow: 3, // Añade una sombra para que destaque '&:hover': { backgroundColor: 'grey.100', // Un leve cambio de color al pasar el mouse }, }} > Mensual de {selectedGrano} {selectedGrano && } ); };