import { useState } from 'react'; import { LineChart, Line, XAxis, YAxis, CartesianGrid, Tooltip, ResponsiveContainer, ReferenceLine, } from 'recharts'; import { PriceHistory } from '../api/client'; interface PriceChartProps { prices: PriceHistory[]; currency: string; onRangeChange?: (days: number | undefined) => void; } const DATE_RANGES = [ { value: 7, label: '7 days' }, { value: 30, label: '30 days' }, { value: 90, label: '90 days' }, { value: undefined, label: 'All time' }, ]; export default function PriceChart({ prices, currency, onRangeChange, }: PriceChartProps) { const [selectedRange, setSelectedRange] = useState(30); const handleRangeChange = (days: number | undefined) => { setSelectedRange(days); onRangeChange?.(days); }; const currencySymbol = currency === 'EUR' ? '€' : currency === 'GBP' ? '£' : '$'; const chartData = prices.map((p) => ({ date: new Date(p.recorded_at).getTime(), price: parseFloat(p.price.toString()), })); const minPrice = Math.min(...chartData.map((d) => d.price)); const maxPrice = Math.max(...chartData.map((d) => d.price)); const avgPrice = chartData.reduce((sum, d) => sum + d.price, 0) / chartData.length; const formatDate = (timestamp: number) => { const date = new Date(timestamp); return date.toLocaleDateString('en-US', { month: 'short', day: 'numeric' }); }; const formatPrice = (value: number) => { return `${currencySymbol}${value.toFixed(2)}`; }; if (prices.length === 0) { return (

No price history available yet.

); } return (

Price History

{DATE_RANGES.map((range) => ( ))}
[formatPrice(value), 'Price']} labelFormatter={(label) => new Date(label).toLocaleDateString('en-US', { weekday: 'short', month: 'short', day: 'numeric', hour: '2-digit', minute: '2-digit', }) } contentStyle={{ background: 'white', border: '1px solid #e2e8f0', borderRadius: '0.5rem', boxShadow: '0 4px 6px -1px rgba(0, 0, 0, 0.1)', }} />
Lowest
{formatPrice(minPrice)}
Average
{formatPrice(avgPrice)}
Highest
{formatPrice(maxPrice)}
); }