import { ProductCard } from "../reuseableUI/productCard"; import { ProductSkeleton } from "../reuseableUI/productSkeleton"; import { Suspense } from "react"; import createApolloServerClient from "@/graphql/server-client"; import { GET_FEATURED_PRODUCTS } from "@/graphql/queries/getFeaturedProducts"; import { Product } from "@/graphql/types/product"; import Heading from "../reuseableUI/heading"; import EmptyState from "../reuseableUI/emptyState"; type Props = { count?: number; collection?: string; heading?: string; }; export const ProductGrid = async ({ count = 10, collection, heading = "", }: Props) => { let f_products: Product[] = []; if (!process.env.NEXT_PUBLIC_API_URL) { // Keep homepage renderable in template mode even when Saleor isn't configured. f_products = []; } else { try { const client = createApolloServerClient(); const { data } = await client.query({ query: GET_FEATURED_PRODUCTS, variables: { slug: collection, first: count, }, fetchPolicy: "cache-first", errorPolicy: "all", // Continue even with network errors }); f_products = data?.collection?.products?.edges?.map((e: { node: Product }) => e.node) ?? []; } catch (error) { console.error(`[ProductGrid] Error fetching products for ${heading}:`, error); // f_products remains empty array, will show empty state } } return (
{!f_products?.length ? (
) : (
{f_products.map((p) => ( }> ))}
)}
); };