import { useMemo } from 'react' import { GetStaticPropsContext, InferGetStaticPropsType } from 'next' import { getConfig } from '@lib/bigcommerce/api' import getAllProducts from '@lib/bigcommerce/api/operations/get-all-products' import getSiteInfo from '@lib/bigcommerce/api/operations/get-site-info' import getAllPages from '@lib/bigcommerce/api/operations/get-all-pages' import rangeMap from '@lib/range-map' import { getCategoryPath, getDesignerPath } from '@utils/search' import { Layout } from '@components/core' import { Grid, Marquee, Hero } from '@components/ui' import { ProductCard } from '@components/product' import Link from 'next/link' export async function getStaticProps({ preview, locale, }: GetStaticPropsContext) { const config = getConfig({ locale }) const { products: featuredProducts } = await getAllProducts({ variables: { field: 'featuredProducts', first: 6 }, config, }) const { products: bestSellingProducts } = await getAllProducts({ variables: { field: 'bestSellingProducts', first: 6 }, config, }) const { products: newestProducts } = await getAllProducts({ variables: { field: 'newestProducts', first: 12 }, config, }) const { categories, brands } = await getSiteInfo({ config }) const { pages } = await getAllPages({ config }) return { props: { featuredProducts, bestSellingProducts, newestProducts, categories, brands, pages, }, revalidate: 10, } } const nonNullable = (v: any) => v export default function Home({ featuredProducts, bestSellingProducts, newestProducts, categories, brands, }: InferGetStaticPropsType) { const { featured, bestSelling } = useMemo(() => { // Create a copy of products that we can mutate const products = [...newestProducts] // If the lists of featured and best selling products don't have enough // products, then fill them with products from the products list, this // is useful for new commerce sites that don't have a lot of products return { featured: rangeMap( 6, (i) => featuredProducts[i] ?? products.shift() ).filter(nonNullable), bestSelling: rangeMap( 6, (i) => bestSellingProducts[i] ?? products.shift() ).filter(nonNullable), } }, [newestProducts, featuredProducts, bestSellingProducts]) return (
{featured.slice(0, 3).map(({ node }, i) => ( ))} {bestSelling.slice(0, 3).map(({ node }) => ( ))} {featured.slice(3, 6).map(({ node }, i) => ( ))} {bestSelling.slice(3, 6).map(({ node }) => ( ))}
{newestProducts.map(({ node }) => ( ))}
) } Home.Layout = Layout