'use client'; import { useEffect, useState } from 'react'; import type { Product } from 'brainerce'; import { getClient } from '@/core/lib/brainerce'; import { useRegion } from '@/core/providers/store-provider'; export interface UseHomeDataResult { /** Newest products for the featured grid (max 8). */ products: Product[]; /** True while the initial fetch is in flight. */ loading: boolean; } /** * Homepage data: the featured product grid. * * Discount banners are deliberately NOT fetched here. They render below the * header on every page, so the root layout fetches them server-side and hands * them to ``. Fetching them again on the home page would * be a duplicate request and a second, later-arriving copy of the same strip. * * Pure data/behavior — rendering lives in `ui/home/`. */ export function useHomeData(): UseHomeDataResult { const [products, setProducts] = useState([]); const [loading, setLoading] = useState(true); // ⛔ Every product read carries the region, or the featured grid quotes the // default region's currency to a shopper the product page then prices // differently. `undefined` on a store with no regions, and the SDK omits // the param, so a single-region store behaves exactly as before. const { regionId } = useRegion(); useEffect(() => { async function load() { try { const client = getClient(); const [productsRes] = await Promise.allSettled([ client.getProducts({ limit: 8, sortBy: 'createdAt', sortOrder: 'desc', ...(regionId ? { regionId } : {}), }), ]); if (productsRes.status === 'fulfilled') { setProducts(productsRes.value.data); } } catch (err) { console.error('Failed to load home page data:', err); } finally { setLoading(false); } } load(); // Re-runs when the shopper switches region, so the grid re-prices instead // of keeping the currency it was first rendered with. }, [regionId]); return { products, loading }; }