'use client'; import { format } from 'date-fns'; import { CartesianGrid, Line, LineChart, ResponsiveContainer, Tooltip, XAxis, YAxis } from 'recharts'; import { Panel } from '@/components/ui/panel'; import { formatCurrency } from '@/lib/format'; type PriceHistoryCardProps = { loading: boolean; priceHistory: Array<{ date: string; close: number }>; quote: number; }; const CHART_TEXT = '#f3f5f7'; const CHART_MUTED = '#a1a9b3'; const CHART_GRID = 'rgba(196, 202, 211, 0.18)'; const CHART_TOOLTIP_BG = 'rgba(31, 34, 39, 0.96)'; const CHART_TOOLTIP_BORDER = 'rgba(220, 226, 234, 0.24)'; function formatShortDate(value: string) { const parsed = new Date(value); return Number.isNaN(parsed.getTime()) ? value : format(parsed, 'MMM yy'); } function formatLongDate(value: string) { const parsed = new Date(value); return Number.isNaN(parsed.getTime()) ? value : format(parsed, 'MMM dd, yyyy'); } export function PriceHistoryCard(props: PriceHistoryCardProps) { const series = props.priceHistory.map((point) => ({ ...point, label: formatShortDate(point.date) })); const firstPoint = props.priceHistory[0] ?? null; const lastPoint = props.priceHistory[props.priceHistory.length - 1] ?? null; const change = firstPoint && lastPoint ? lastPoint.close - firstPoint.close : null; const changePct = firstPoint && lastPoint && firstPoint.close !== 0 ? ((lastPoint.close - firstPoint.close) / firstPoint.close) * 100 : null; return (
{props.loading ? (

Loading price history...

) : series.length === 0 ? (

No price history available.

) : (
`$${value.toFixed(0)}`} /> formatCurrency(Array.isArray(value) ? value[0] : value)} labelFormatter={(value) => String(value)} contentStyle={{ backgroundColor: CHART_TOOLTIP_BG, border: `1px solid ${CHART_TOOLTIP_BORDER}`, borderRadius: '0.75rem' }} labelStyle={{ color: CHART_TEXT }} itemStyle={{ color: CHART_TEXT }} cursor={{ stroke: 'rgba(220, 226, 234, 0.28)', strokeWidth: 1 }} />
)}

Current price

{formatCurrency(props.quote)}

1Y change

{change === null ? 'n/a' : formatCurrency(change)}

1Y return

{changePct === null ? 'n/a' : `${changePct.toFixed(2)}%`}

{firstPoint && lastPoint ? `${formatLongDate(firstPoint.date)} to ${formatLongDate(lastPoint.date)}` : 'No comparison range'}

); }