import { useEffect, useState } from 'react' import { GetStaticPropsContext, InferGetStaticPropsType } from 'next' import { useRouter } from 'next/router' import Link from 'next/link' import getSiteInfo from '@lib/bigcommerce/api/operations/get-site-info' import useSearch from '@lib/bigcommerce/products/use-search' import { Layout } from '@components/core' import { Container, Grid } from '@components/ui' import { ProductCard } from '@components/product' export async function getStaticProps({ preview }: GetStaticPropsContext) { const { categories, brands } = await getSiteInfo() return { props: { categories, brands }, } } export default function Home({ categories, brands, }: InferGetStaticPropsType) { const router = useRouter() const { q } = router.query const { data } = useSearch({ search: typeof q === 'string' ? q : '', }) const { category, brand } = useSearchMeta(router.asPath) console.log('Q', category, brand) return (
  • All Categories
  • {categories.map((cat) => (
  • {cat.name}
  • ))}
  • All Designers
  • {brands.flatMap(({ node }) => (
  • {node.name}
  • ))}
{data ? ( <> {q && (
{data.found ? ( <>Showing {data.products.length} results for ) : ( <>There are no products that match )}{' '} "{q}"
)} ) : (
Searching...
)}
  • Relevance
  • Latest arrivals
  • Trending
  • Price: Low to high
  • Price: High to low
) } Home.Layout = Layout function useSearchMeta(asPath: string) { const [category, setCategory] = useState() const [brand, setBrand] = useState() useEffect(() => { const parts = asPath.split('/') // console.log('parts', parts) let c = parts[2] let b = parts[3] if (c === 'designers') { c = parts[4] } if (c !== category) setCategory(c) if (b !== brand) setBrand(b) }, [asPath]) return { category, brand } } function getCategoryPath(path: string, designer?: string) { return designer ? `/search/designers/${designer}${path}` : `/search${path}` } function getDesignerPath(path: string, category?: string) { // Remove the trailing slash and replace /brands with /designers const designer = path.replace(/^\/brands/, '/designers').replace(/\/$/, '') const href = `/search${designer}` return category ? `${href}/${category}` : href }