import { useParams, Link } from 'react-router-dom' import { LineChart, Line, XAxis, YAxis, Tooltip, Legend, ResponsiveContainer, } from 'recharts' import { useProduct, usePriceHistory } from '../hooks/useApi.ts' const storeLineColors: Record = { meijer: '#e31837', kroger: '#0068a8', target: '#cc0000', } export function ProductDetail() { const { id } = useParams<{ id: string }>() const { data: product, isLoading: productLoading } = useProduct(id ?? '') const { data: history = [], isLoading: historyLoading } = usePriceHistory(id ?? '') if (productLoading || historyLoading) { return (
) } if (!product) { return (

Product not found.

Back to products
) } const lowestPrice = Math.min(...product.prices.map((p) => p.price)) // Reshape history for chart: { date, meijer, kroger, target } const chartData: Record[] = [] const dateMap = new Map>() for (const h of history) { if (!dateMap.has(h.date)) { dateMap.set(h.date, { date: h.date }) } dateMap.get(h.date)![h.storeId] = h.price } for (const entry of dateMap.values()) { chartData.push(entry) } return (
{/* Back link */} Products {/* Product header */}

{product.name}

{product.brand} · {product.category}

{/* Price history chart */}

Price History (90 days)

{ const dt = new Date(d) return `${dt.getMonth() + 1}/${dt.getDate()}` }} interval="preserveStartEnd" /> `$${v.toFixed(2)}`} /> `$${Number(value).toFixed(2)}`} labelFormatter={(label) => new Date(String(label)).toLocaleDateString('en-US', { month: 'short', day: 'numeric', }) } /> {['meijer', 'kroger', 'target'].map((store) => ( ))}
{/* Store comparison table */}

Store Comparison

{product.prices .slice() .sort((a, b) => a.price - b.price) .map((pp) => (
{pp.storeName.charAt(0)}

{pp.storeName}

Updated{' '} {new Date(pp.lastUpdated).toLocaleDateString('en-US', { month: 'short', day: 'numeric', })}

${pp.price.toFixed(2)}

{pp.price === lowestPrice && ( Best price )}
))}
{/* Actions */}
Set Price Alert Compare at Nearby Stores
) }