import { GET_BUNDLES } from "@/graphql/queries/getBundles"; import createApolloServerClient from "@/graphql/server-client"; import { Product } from "@/graphql/types/product"; import ProductSwiper from "./productSwiper"; type MetadataItem = { key: string; value: string }; type BundleCollection = { id: string; name: string; slug: string; metadata?: MetadataItem[]; products?: { edges: { node: Product }[] }; }; type Props = { count?: number; collection?: string; }; export const BundleProducts = async ({ count = 10, collection }: Props) => { let f_products: Product[] = []; let bundleName: string | undefined; let bundlePriceText: string | undefined; let selectedBundle: BundleCollection | null = null; if (!process.env.NEXT_PUBLIC_API_URL) return null; try { const client = createApolloServerClient(); const channel = process.env.NEXT_PUBLIC_SALEOR_CHANNEL || "default-channel"; const { data } = await client.query({ query: GET_BUNDLES, variables: { channel, first: count, }, fetchPolicy: "cache-first", errorPolicy: "all", }); const bundles: BundleCollection[] = (data?.collections?.edges ?? []).map( (e: { node: BundleCollection }) => e.node ); if (bundles.length) { if (collection) { const exact = bundles.find( (b: BundleCollection) => b.slug === collection ); selectedBundle = exact ?? bundles[0]; if (!exact) { console.warn( `[BundleProducts] Bundle slug "${collection}" not found. Falling back to first bundle "${bundles[0].slug}".` ); } } else { selectedBundle = bundles[0]; } } if (selectedBundle) { bundleName = selectedBundle.name; const meta: Record = Object.fromEntries( (selectedBundle.metadata ?? []).map((m: MetadataItem) => [ m.key, m.value, ]) ); bundlePriceText = meta.price || undefined; f_products = selectedBundle.products?.edges?.map((e: { node: Product }) => e.node) ?? []; } } catch (error) { console.error(`[BundleProducts] Error fetching bundle products:`, error); // f_products remains empty array, will show empty state } if (!f_products.length) return null; return (
{/* Left promotional section */}
RECOMMENDED

{bundleName || "Bundle"}

{bundlePriceText ? (

{bundlePriceText}

) : null}
{/* Right products section */}
); };