import { useState } from 'react' import { Link } from 'react-router-dom' import { useProducts } from '../hooks/useApi.ts' export function Products() { const [search, setSearch] = useState('') const { data: products = [], isLoading, error } = useProducts(search || undefined) const lowestPrice = (product: typeof products[0]) => Math.min(...product.prices.map((p) => p.price)) if (error) { return (

Failed to load products.

) } return (

Products

{/* Search input */}
setSearch(e.target.value)} className="min-h-12 w-full rounded-xl border border-gray-200 bg-white px-4 text-base shadow-sm focus:border-brand-blue focus:outline-none focus:ring-1 focus:ring-brand-blue" />
{/* Product list */}
{isLoading ? ( [1, 2, 3].map((i) => (
)) ) : products.length === 0 ? (

No products match “{search}”.

) : ( products.map((product) => { const low = lowestPrice(product) const cheapest = product.prices.find((p) => p.price === low) return (

{product.name}

{product.brand} · {product.category}

${low.toFixed(2)}

{cheapest?.storeName}

{product.prices.map((pp) => ( {pp.storeName} ${pp.price.toFixed(2)} ))}
) }) )}
) }