import React, { useEffect, useState, useMemo } from 'react'; import { X, TrendingUp, TrendingDown, Star } from 'lucide-react'; import { AreaChart, Area, XAxis, YAxis, Tooltip, ResponsiveContainer, ReferenceLine } from 'recharts'; import { getHistoricalData, fetchRealData } from '../data/historicalData'; import { Loader2 } from 'lucide-react'; import InvestmentCommittee from './InvestmentCommittee'; const TIMEFRAME_DAYS = { '1W': 7, '1M': 30, '3M': 90, '1Y': 365, 'ALL': 365 }; export default function StockPopup({ ticker, assetName, onClose, allHoldings = [], prices = {} }) { const [data, setData] = useState(null); const [loading, setLoading] = useState(false); const [isDebating, setIsDebating] = useState(false); const [timeframe, setTimeframe] = useState('1M'); useEffect(() => { async function loadData() { if (ticker) { setLoading(true); const realData = await fetchRealData(ticker); setData(realData); setLoading(false); setIsDebating(false); } } loadData(); }, [ticker]); // Calculate Top Performers const topPerformers = useMemo(() => { if (!allHoldings.length || !prices) return []; return allHoldings .map(asset => ({ ...asset, perf: prices[asset.ticker]?.percent || 0, currentPrice: prices[asset.ticker]?.price || 0 })) .sort((a, b) => b.perf - a.perf) .slice(0, 5); }, [allHoldings, prices]); const viewData = useMemo(() => { if (!data) return null; // Slice history based on timeframe const daysToShow = TIMEFRAME_DAYS[timeframe] || 30; const startIndex = Math.max(0, data.history.length - daysToShow); const slicedHistory = data.history.slice(startIndex); // Recalculate metrics for the specific timeframe if (slicedHistory.length === 0) return null; const currentPrice = slicedHistory[slicedHistory.length - 1].price; const oldPrice = slicedHistory[0].price; const change = Number((currentPrice - oldPrice).toFixed(2)); const percentChange = Number(((change / oldPrice) * 100).toFixed(2)); const isPositive = change >= 0; return { ...data, history: slicedHistory, change, percentChange, isPositive }; }, [data, timeframe]); if (!ticker) return null; if (loading || !viewData) { return (
Connecting to Yahoo Finance...
At close: {viewData.history[viewData.history.length-1].date}
{asset.ticker}
${asset.currentPrice.toLocaleString()}
{assetName} currently ranks #{topPerformers.findIndex(a => a.ticker === ticker) + 1} among your top-tier performers.