import { useCallback, useEffect, useMemo, useState } from 'react' import { useProductSlotIdentifier } from '@/hooks/useProductSlotIdentifier' import { useAllProducts } from '@/src/hooks/prominent-api-hooks' import { useCentralSlotStore } from '@/src/store/centralSlotStore' /** Rows per page, same as the "Show More" step on the home catalogue. */ export const HOME_CATALOG_PAGE_SIZE = 21 /** * Home catalogue derivation: the products payload, the per-tag rows and the * sorted "All Products" feed. * * This used to live inside `Dashboard`, which made that component the largest * file in the folder and left the sort effect reading `getQuickestSlot` and * `isSlotsLoaded` without listing them. Keeping it here leaves the screen with * just its layout concerns, and the effect lists every value it reads. */ export function useHomeCatalog() { const { data: productsData, isLoading, error } = useAllProducts() const { getQuickestSlot } = useProductSlotIdentifier() const productSlotsMap = useCentralSlotStore((state) => state.productSlotsMap) const isSlotsLoaded = useCentralSlotStore((state) => state.isSlotsLoaded) // Single source of truth for the catalogue: `products` and `dashboardTags` // come out of this memo together with the per-tag rows, so the screen never // holds two views of the same payload that can drift apart. const { dashboardTags, products, productsByTagId } = useMemo(() => { const tags = productsData?.tags ?? [] const allProducts = productsData?.products ?? [] // Hoisted: the old version rebuilt this Map once per tag. const productById = new Map() for (const product of allProducts) { productById.set(product.id, product) } const map: Record = {} for (const tag of tags) { // Respect the admin-curated order — tag.productIds is already ordered by // the tag's sortOrder (backend reorders it when building the cache). // Unknown ids are skipped and a repeated id only renders once. const ordered: any[] = [] const seen = new Set() for (const id of tag.productIds || []) { if (seen.has(id)) continue seen.add(id) const product = productById.get(id) if (product) ordered.push(product) } // Out-of-stock items are pushed to the end, preserving their relative order. const inStock: any[] = [] const outOfStock: any[] = [] for (const product of ordered) { const isOut = Boolean(productSlotsMap[product.id]?.isOutOfStock) || (isSlotsLoaded && !getQuickestSlot(product.id)) if (isOut) outOfStock.push(product) else inStock.push(product) } map[tag.id] = [...inStock, ...outOfStock] } return { dashboardTags: tags, products: allProducts, productsByTagId: map } }, [productsData, productSlotsMap, isSlotsLoaded, getQuickestSlot]) // Tag selection/expansion live in homeTagStore (subscribed by TagTabStrip and // TagScene), so a tap does not re-render Dashboard or rebuild the list below. const [displayedProducts, setDisplayedProducts] = useState([]) const [visibleCount, setVisibleCount] = useState(HOME_CATALOG_PAGE_SIZE) useEffect(() => { if (products.length === 0) { setDisplayedProducts([]) return } const allSorted = products .filter((product) => typeof product.id === 'number') .sort((a, b) => { const slotA = getQuickestSlot(a.id) const slotB = getQuickestSlot(b.id) const aOutOfStock = Boolean(productSlotsMap[a.id]?.isOutOfStock) || (isSlotsLoaded && !slotA) const bOutOfStock = Boolean(productSlotsMap[b.id]?.isOutOfStock) || (isSlotsLoaded && !slotB) if (aOutOfStock && !bOutOfStock) return 1 if (!aOutOfStock && bOutOfStock) return -1 return 0 }) setDisplayedProducts(allSorted) setVisibleCount(HOME_CATALOG_PAGE_SIZE) }, [products, productSlotsMap, getQuickestSlot, isSlotsLoaded]) // Derived, not state: the old version set this from inside the setVisibleCount // updater, which React may replay. const hasMore = visibleCount < displayedProducts.length // Pure updater; the footer reads `hasMore` above. const showMore = useCallback(() => { setVisibleCount((prev) => prev + HOME_CATALOG_PAGE_SIZE) }, []) return { isLoading, error, products, dashboardTags, productsByTagId, displayedProducts, visibleCount, hasMore, showMore, } }