import { useEffect, useState } from 'react' import { Link } from 'react-router-dom' import { fetchProducts, Product, ProductQuery } from '../api' import { IconArrowRight } from './Icons' import ProductCard from './ProductCard' import { SkeletonRow } from './Skeleton' interface RailProps { title: string query: ProductQuery seeAllHref?: string } export default function Rail({ title, query, seeAllHref }: RailProps) { const [products, setProducts] = useState(null) const [error, setError] = useState(false) useEffect(() => { const controller = new AbortController() setProducts(null) setError(false) fetchProducts({ per_page: 12, ...query }, controller.signal) .then((res) => setProducts(res.items)) .catch((err: unknown) => { if (err instanceof DOMException && err.name === 'AbortError') return setError(true) setProducts([]) }) return () => controller.abort() // eslint-disable-next-line react-hooks/exhaustive-deps }, [JSON.stringify(query)]) if (error || (products !== null && products.length === 0)) { return null } return (

{title}

{seeAllHref && ( Tout voir )}
{products === null ? ( ) : (
{products.map((p) => (
))}
)}
) }