| 
									
										
										
										
											2025-07-01 16:05:26 -03:00
										 |  |  | import { useState } from 'react'; | 
					
						
							| 
									
										
										
										
											2025-07-01 13:42:16 -03:00
										 |  |  | import { | 
					
						
							| 
									
										
										
										
											2025-07-01 16:05:26 -03:00
										 |  |  |   Box, CircularProgress, Alert, Table, TableBody, TableCell, TableContainer, | 
					
						
							|  |  |  |   TableHead, TableRow, Paper, Typography, Dialog, DialogTitle, | 
					
						
							|  |  |  |   DialogContent, IconButton | 
					
						
							| 
									
										
										
										
											2025-07-01 13:42:16 -03:00
										 |  |  | } from '@mui/material'; | 
					
						
							| 
									
										
										
										
											2025-07-01 16:05:26 -03:00
										 |  |  | import CloseIcon from '@mui/icons-material/Close'; | 
					
						
							| 
									
										
										
										
											2025-07-01 13:42:16 -03:00
										 |  |  | import ArrowUpwardIcon from '@mui/icons-material/ArrowUpward'; | 
					
						
							|  |  |  | import ArrowDownwardIcon from '@mui/icons-material/ArrowDownward'; | 
					
						
							|  |  |  | import RemoveIcon from '@mui/icons-material/Remove'; | 
					
						
							| 
									
										
										
										
											2025-07-03 11:44:10 -03:00
										 |  |  | import { formatFullDateTime, formatCurrency } from '../utils/formatters'; | 
					
						
							| 
									
										
										
										
											2025-07-01 16:05:26 -03:00
										 |  |  | import type { CotizacionBolsa } from '../models/mercadoModels'; | 
					
						
							|  |  |  | import { useApiData } from '../hooks/useApiData'; | 
					
						
							|  |  |  | import { HistoricalChartWidget } from './HistoricalChartWidget'; | 
					
						
							| 
									
										
										
										
											2025-07-03 11:44:10 -03:00
										 |  |  | import { PiChartLineUpBold } from 'react-icons/pi'; | 
					
						
							| 
									
										
										
										
											2025-07-01 13:42:16 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 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 ( | 
					
						
							|  |  |  |     <Box component="span" sx={{ display: 'flex', alignItems: 'center', justifyContent: 'center', color }}> | 
					
						
							|  |  |  |       <Icon sx={{ fontSize: '1rem', mr: 0.5 }} /> | 
					
						
							| 
									
										
										
										
											2025-07-03 11:44:10 -03:00
										 |  |  |       <Typography variant="body2" component="span" sx={{ fontWeight: 'bold' }}>{value.toFixed(2)}%</Typography> | 
					
						
							| 
									
										
										
										
											2025-07-01 13:42:16 -03:00
										 |  |  |     </Box> | 
					
						
							|  |  |  |   ); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export const BolsaUsaWidget = () => { | 
					
						
							|  |  |  |   const { data, loading, error } = useApiData<CotizacionBolsa[]>('/mercados/bolsa/eeuu'); | 
					
						
							| 
									
										
										
										
											2025-07-01 16:05:26 -03:00
										 |  |  |   const [selectedTicker, setSelectedTicker] = useState<string | null>(null); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-03 11:44:10 -03:00
										 |  |  |   const handleRowClick = (ticker: string) => setSelectedTicker(ticker); | 
					
						
							|  |  |  |   const handleCloseDialog = () => setSelectedTicker(null); | 
					
						
							| 
									
										
										
										
											2025-07-01 16:05:26 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-03 11:44:10 -03:00
										 |  |  |   const otherStocks = data?.filter(d => d.ticker !== '^GSPC') || []; | 
					
						
							| 
									
										
										
										
											2025-07-01 13:42:16 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  |   if (loading) { | 
					
						
							|  |  |  |     return <Box sx={{ display: 'flex', justifyContent: 'center', p: 4 }}><CircularProgress /></Box>; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (error) { | 
					
						
							|  |  |  |     return <Alert severity="error">{error}</Alert>; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (!data || data.length === 0) { | 
					
						
							| 
									
										
										
										
											2025-07-03 11:44:10 -03:00
										 |  |  |     return <Alert severity="info">No hay datos disponibles para el mercado de EEUU.</Alert>; | 
					
						
							| 
									
										
										
										
											2025-07-01 13:42:16 -03:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return ( | 
					
						
							| 
									
										
										
										
											2025-07-01 16:05:26 -03:00
										 |  |  |     <> | 
					
						
							| 
									
										
										
										
											2025-07-03 11:44:10 -03:00
										 |  |  |       {/* Renderizamos la tabla solo si hay otras acciones */} | 
					
						
							|  |  |  |       {otherStocks.length > 0 && ( | 
					
						
							|  |  |  |         <TableContainer component={Paper}> | 
					
						
							|  |  |  |           <Box sx={{ p: 1, m: 0 }}> | 
					
						
							|  |  |  |             <Typography variant="caption" sx={{ fontStyle: 'italic', color: 'text.secondary' }}> | 
					
						
							|  |  |  |               Última actualización de acciones: {formatFullDateTime(otherStocks[0].fechaRegistro)} | 
					
						
							|  |  |  |             </Typography> | 
					
						
							|  |  |  |           </Box> | 
					
						
							|  |  |  |           <Table size="small" aria-label="panel principal eeuu"> | 
					
						
							|  |  |  |             <TableHead> | 
					
						
							|  |  |  |               <TableRow> | 
					
						
							|  |  |  |                 <TableCell>Símbolo</TableCell> | 
					
						
							|  |  |  |                 <TableCell align="right">Precio Actual</TableCell> | 
					
						
							|  |  |  |                  | 
					
						
							|  |  |  |                 <TableCell align="right" sx={{ display: { xs: 'none', md: 'table-cell' } }}>Apertura</TableCell> | 
					
						
							|  |  |  |                 <TableCell align="right" sx={{ display: { xs: 'none', sm: 'table-cell' } }}>Cierre Anterior</TableCell> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 <TableCell align="center">% Cambio</TableCell> | 
					
						
							|  |  |  |                 <TableCell align="center">Historial</TableCell> | 
					
						
							| 
									
										
										
										
											2025-07-01 16:05:26 -03:00
										 |  |  |               </TableRow> | 
					
						
							| 
									
										
										
										
											2025-07-03 11:44:10 -03:00
										 |  |  |             </TableHead> | 
					
						
							|  |  |  |             <TableBody> | 
					
						
							|  |  |  |               {otherStocks.map((row) => ( | 
					
						
							|  |  |  |                 <TableRow key={row.ticker} hover> | 
					
						
							|  |  |  |                   <TableCell component="th" scope="row"><Typography variant="body2" sx={{ fontWeight: 'bold' }}>{row.ticker}</Typography></TableCell> | 
					
						
							|  |  |  |                   <TableCell align="right">{formatCurrency(row.precioActual, 'USD')}</TableCell> | 
					
						
							|  |  |  |                    | 
					
						
							|  |  |  |                   <TableCell align="right" sx={{ display: { xs: 'none', md: 'table-cell' } }}>{formatCurrency(row.apertura, 'USD')}</TableCell> | 
					
						
							|  |  |  |                   <TableCell align="right" sx={{ display: { xs: 'none', sm: 'table-cell' } }}>{formatCurrency(row.cierreAnterior, 'USD')}</TableCell> | 
					
						
							|  |  |  |                    | 
					
						
							|  |  |  |                   <TableCell align="center"><Variacion value={row.porcentajeCambio} /></TableCell> | 
					
						
							|  |  |  |                   <TableCell align="center"> | 
					
						
							|  |  |  |                     <IconButton | 
					
						
							|  |  |  |                       aria-label={`ver historial de ${row.ticker}`} size="small" | 
					
						
							|  |  |  |                       onClick={() => handleRowClick(row.ticker)} | 
					
						
							|  |  |  |                       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)' } }} | 
					
						
							|  |  |  |                     ><PiChartLineUpBold size="18" /></IconButton> | 
					
						
							|  |  |  |                   </TableCell> | 
					
						
							|  |  |  |                 </TableRow> | 
					
						
							|  |  |  |               ))} | 
					
						
							|  |  |  |             </TableBody> | 
					
						
							|  |  |  |           </Table> | 
					
						
							|  |  |  |         </TableContainer> | 
					
						
							|  |  |  |       )} | 
					
						
							| 
									
										
										
										
											2025-07-01 16:05:26 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-03 11:44:10 -03:00
										 |  |  |       <Dialog open={Boolean(selectedTicker)} onClose={handleCloseDialog} maxWidth="md" fullWidth sx={{ '& .MuiDialog-paper': { overflow: 'visible' } }}> | 
					
						
							|  |  |  |         <IconButton aria-label="close" onClick={handleCloseDialog} sx={{ position: 'absolute', top: -15, right: -15, color: (theme) => theme.palette.grey[500], backgroundColor: 'white', boxShadow: 3, '&:hover': { backgroundColor: 'grey.100' }, }}> | 
					
						
							|  |  |  |           <CloseIcon /> | 
					
						
							|  |  |  |         </IconButton> | 
					
						
							|  |  |  |         <DialogTitle sx={{ m: 0, p: 2 }}>Historial de 30 días para: {selectedTicker}</DialogTitle> | 
					
						
							| 
									
										
										
										
											2025-07-01 16:05:26 -03:00
										 |  |  |         <DialogContent dividers> | 
					
						
							| 
									
										
										
										
											2025-07-03 11:44:10 -03:00
										 |  |  |           {selectedTicker && <HistoricalChartWidget ticker={selectedTicker} mercado="EEUU" dias={30} />} | 
					
						
							| 
									
										
										
										
											2025-07-01 16:05:26 -03:00
										 |  |  |         </DialogContent> | 
					
						
							|  |  |  |       </Dialog> | 
					
						
							|  |  |  |     </> | 
					
						
							| 
									
										
										
										
											2025-07-01 13:42:16 -03:00
										 |  |  |   ); | 
					
						
							|  |  |  | }; |