import React, { useState, useEffect } from 'react'; import { useTranslation } from 'react-i18next'; import { XAxis, YAxis, Tooltip, ResponsiveContainer, Area, AreaChart } from 'recharts'; import { Card } from '@/components/ui/card'; import { Tabs, TabsList, TabsTrigger } from '@/components/ui/tabs'; import { TrendingUp, TrendingDown } from 'lucide-react'; interface PriceChartProps { fromToken: string; toToken: string; currentPrice: number; } // Helper: Convert backend token symbols to user-facing display names const getDisplayName = (token: string): string => { if (token === 'wUSDT') return 'USDT'; if (token === 'wHEZ') return 'HEZ'; return token; // HEZ, PEZ, etc. remain the same }; export const PriceChart: React.FC = ({ fromToken, toToken, currentPrice }) => { const { t } = useTranslation(); const [timeframe, setTimeframe] = useState<'1H' | '24H' | '7D' | '30D'>('24H'); const [chartData, setChartData] = useState>>([]); const [priceChange, setPriceChange] = useState<{ value: number; percent: number }>({ value: 0, percent: 0 }); useEffect(() => { // Generate mock historical data (in production, fetch from blockchain/oracle) const generateMockData = () => { const dataPoints = timeframe === '1H' ? 60 : timeframe === '24H' ? 24 : timeframe === '7D' ? 7 : 30; const basePrice = currentPrice || 1.0; const data = []; let price = basePrice * 0.95; // Start 5% below current for (let i = 0; i < dataPoints; i++) { // Random walk with slight upward trend const change = (Math.random() - 0.48) * 0.02; // Slight bullish bias price = price * (1 + change); let timeLabel = ''; const now = new Date(); if (timeframe === '1H') { now.setMinutes(now.getMinutes() - (dataPoints - i)); timeLabel = now.toLocaleTimeString('en-US', { hour: '2-digit', minute: '2-digit' }); } else if (timeframe === '24H') { now.setHours(now.getHours() - (dataPoints - i)); timeLabel = now.toLocaleTimeString('en-US', { hour: '2-digit' }); } else if (timeframe === '7D') { now.setDate(now.getDate() - (dataPoints - i)); timeLabel = now.toLocaleDateString('en-US', { month: 'short', day: 'numeric' }); } else { now.setDate(now.getDate() - (dataPoints - i)); timeLabel = now.toLocaleDateString('en-US', { month: 'short', day: 'numeric' }); } data.push({ time: timeLabel, price: parseFloat(price.toFixed(4)), timestamp: now.getTime() }); } // Add current price as last point data.push({ time: 'Now', price: basePrice, timestamp: Date.now() }); return data; }; const data = generateMockData(); setChartData(data); // Calculate price change if (data.length > 1) { const firstPrice = data[0].price; const lastPrice = data[data.length - 1].price; const change = lastPrice - firstPrice; const changePercent = (change / firstPrice) * 100; setPriceChange({ value: change, percent: changePercent }); } }, [timeframe, currentPrice]); const isPositive = priceChange.percent >= 0; return (
{t('priceChart.label', { from: getDisplayName(fromToken), to: getDisplayName(toToken) })}
${currentPrice.toFixed(4)}
{isPositive ? : } {isPositive ? '+' : ''}{priceChange.percent.toFixed(2)}%
setTimeframe(v as Record)}> {t('priceChart.1h')} {t('priceChart.24h')} {t('priceChart.7d')} {t('priceChart.30d')}
`$${value.toFixed(3)}`} /> [`$${value.toFixed(4)}`, 'Price']} />
{t('priceChart.footnote')}
); };