import React, { useEffect, useMemo, useState } from 'react'; import { useLocation, useParams, useSearchParams } from 'react-router-dom'; import { MobileShell } from '../../components/layout/MobileShell'; import { StandardPageHeader } from '../../components/layout/TopHeader'; import { SearchBar } from '../../components/common/SearchBar'; import { GridProductCard } from '../../components/common/GridProductCard'; import { EmptyStateBox, ProductGridShimmer, RetryBanner, } from '../../components/common/ShimmerSkeletons'; import { TWO_COLUMN_GRID_PRODUCTS } from '../../data/catalogData'; import { useShopApp } from '../../context/ShopAppContext'; import { tr } from '../../lib/cookies'; import { mapProductToGridItem } from '../../lib/mappers'; import { isBareMode, ROUTES, USE_MOCK_DATA } from '../../lib/routes'; import { useCategoryDetailQuery, useProductsQuery, } from '../../lib/shopQueries'; interface ShopCatItemsPageProps { mode?: 'category' | 'search' | 'bookmarks'; } function humanizeSlug(slug?: string): string { if (!slug) return 'Category'; return slug .replace(/-/g, ' ') .replace(/\b\w/g, (ch) => ch.toUpperCase()); } /** * Node 4181:12817 — `shop-catitems` (Category Items, Search Results, Bookmarks, 375×814) */ export const ShopCatItemsPage: React.FC = ({ mode = 'category', }) => { const location = useLocation(); const isBare = isBareMode(location.search); const useMockFallback = isBare || USE_MOCK_DATA; const { categorySlug } = useParams<{ categorySlug?: string }>(); const [searchParams, setSearchParams] = useSearchParams(); const initialQ = searchParams.get('q') || ''; const { hydrated, bookmarkedProducts, isBookmarked, toggleBookmark } = useShopApp(); const [query, setQuery] = useState(initialQ); const isSearchOrAll = mode === 'search' || !categorySlug; const shouldFetchLive = !isBare && mode !== 'bookmarks'; const productsQuery = useProductsQuery( { q: initialQ || undefined }, shouldFetchLive && isSearchOrAll ); const categoryQuery = useCategoryDetailQuery( categorySlug || '', shouldFetchLive && !isSearchOrAll ); const products = !shouldFetchLive ? null : isSearchOrAll ? productsQuery.data?.results ?? null : categoryQuery.data?.products ?? null; const activeQuery = isSearchOrAll ? productsQuery : categoryQuery; const loading = shouldFetchLive && (activeQuery.isPending || (!products && activeQuery.isFetching)); const loadError = shouldFetchLive && activeQuery.isError; const headerTitle = useMemo(() => { if (mode === 'bookmarks') return 'Saved Items'; if (isSearchOrAll) { return initialQ ? `Results for "${initialQ}"` : 'Explore Products'; } const cat = categoryQuery.data?.category; return ( cat?.localized_title || cat?.title || (categorySlug ? humanizeSlug(categorySlug) : 'Home & Living') ); }, [categoryQuery.data?.category, categorySlug, initialQ, isSearchOrAll, mode]); useEffect(() => { setQuery(initialQ); }, [initialQ]); const displayedProducts = useMemo(() => { const source = mode === 'bookmarks' ? bookmarkedProducts : products; if (!source) return null; const trimmed = query.trim().toLowerCase(); if (!trimmed) return source; return source.filter( (p) => p.title.toLowerCase().includes(trimmed) || (p.store_name || '').toLowerCase().includes(trimmed) || (p.category_title || '').toLowerCase().includes(trimmed) ); }, [bookmarkedProducts, mode, products, query]); const backFallback = mode === 'bookmarks' ? ROUTES.account : mode === 'search' ? ROUTES.home : ROUTES.categories; const isBookmarksLoading = !isBare && mode === 'bookmarks' && !hydrated; return (
{ if (mode === 'search') { setSearchParams(val ? { q: val } : {}); } else { setQuery(val); } } } /> {!isBare && (loading || isBookmarksLoading) ? ( ) : !isBare && displayedProducts ? ( displayedProducts.length > 0 ? (
{displayedProducts.map((product) => ( { void toggleBookmark(product.slug, product.id, product.slug); }} /> ))}
) : (
{tr( mode === 'bookmarks' ? 'No saved items yet. Tap the bookmark icon on any product to save it here.' : 'No matching products found.' )}
) ) : ( <> {!isBare && loadError && ( void activeQuery.refetch()} /> )} {useMockFallback ? (
{TWO_COLUMN_GRID_PRODUCTS.map((product) => ( ))}
) : ( )} )}
); };