// Importaciones de React y Material-UI import React, { useState, useRef } from 'react'; import { Box, CircularProgress, Alert, Table, TableBody, TableCell, TableContainer, TableHead, TableRow, Paper, Typography, Dialog, DialogTitle, DialogContent, IconButton } from '@mui/material'; 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 { PiChartLineUpBold } from 'react-icons/pi'; // Importaciones de nuestros modelos, hooks y utilidades import type { CotizacionBolsa } from '../models/mercadoModels'; import { useApiData } from '../hooks/useApiData'; import { useIsHoliday } from '../hooks/useIsHoliday'; import { formatFullDateTime, formatCurrency } from '../utils/formatters'; import { HistoricalChartWidget } from './HistoricalChartWidget'; import { HolidayAlert } from './common/HolidayAlert'; /** * Sub-componente para mostrar la variación porcentual con un icono y color apropiado. */ const Variacion = ({ value }: { value: number }) => { const color = value > 0 ? 'success.main' : value < 0 ? 'error.main' : 'text.secondary'; const Icon = value > 0 ? ArrowUpwardIcon : value < 0 ? ArrowDownwardIcon : RemoveIcon; return ( {value.toFixed(2)}% ); }; /** * Sub-componente que renderiza la tabla de acciones detalladas. * Se extrae para mantener el componente principal más limpio. */ const RenderContent = ({ data, handleOpenModal }: { data: CotizacionBolsa[], handleOpenModal: (ticker: string, event: React.MouseEvent) => void, }) => { // Filtramos para obtener solo las acciones, excluyendo el índice. const panelPrincipal = data.filter(d => d.ticker !== '^MERV'); if (panelPrincipal.length === 0) { return No hay acciones líderes para mostrar en este momento.; } return ( Última actualización de acciones: {formatFullDateTime(panelPrincipal[0].fechaRegistro)} Símbolo Precio Actual Apertura Cierre Anterior % Cambio Historial {panelPrincipal.map((row) => ( {row.ticker} ${formatCurrency(row.precioActual)} ${formatCurrency(row.apertura)} ${formatCurrency(row.cierreAnterior)} handleOpenModal(row.ticker, event)} sx={{ boxShadow: '0 1px 3px rgba(0,0,0,0.1)', transition: 'all 0.2s ease-in-out', '&:hover': { transform: 'scale(1.1)', boxShadow: '0 2px 6px rgba(0,0,0,0.2)' } }} > ))}
); }; /** * Widget principal para la sección "Bolsa Local". * Muestra una tarjeta de héroe para el MERVAL y una tabla detallada para las acciones líderes. */ export const BolsaLocalWidget = () => { // Hooks para obtener los datos y el estado de feriado. Las llamadas se disparan en paralelo. const { data, loading: dataLoading, error: dataError } = useApiData('/mercados/bolsa/local'); const isHoliday = useIsHoliday('BA'); // Estado y referencia para manejar el modal del gráfico. const [selectedTicker, setSelectedTicker] = useState(null); const triggerButtonRef = useRef(null); const handleOpenModal = (ticker: string, event: React.MouseEvent) => { triggerButtonRef.current = event.currentTarget; setSelectedTicker(ticker); }; const handleCloseDialog = () => { setSelectedTicker(null); // Devuelve el foco al botón que abrió el modal para mejorar la accesibilidad. setTimeout(() => { triggerButtonRef.current?.focus(); }, 0); }; // Estado de carga unificado: el componente está "cargando" si los datos principales // o la información del feriado todavía no han llegado. const isLoading = dataLoading || isHoliday === null; if (isLoading) { return ; } if (dataError) { return {dataError}; } // Si no hay ningún dato en absoluto, mostramos un mensaje final. if (!data || data.length === 0) { // Si sabemos que es feriado, la alerta de feriado tiene prioridad. if (isHoliday) { return ; } return No hay datos disponibles para el mercado local.; } return ( <> {/* Si es feriado, mostramos la alerta informativa en la parte superior. */} {isHoliday && ( )} {/* La tabla de acciones detalladas se muestra siempre que haya datos para ella. */} {/* El Dialog para mostrar el gráfico histórico. */} theme.palette.grey[500], backgroundColor: 'white', boxShadow: 3, '&:hover': { backgroundColor: 'grey.100' }, }} > Historial de 30 días para: {selectedTicker} {selectedTicker && } ); };