ifixkart-storefront/components/catalog/StandardCatalog.tsx

1052 lines
40 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use client';
import { useState, useEffect, useMemo, useRef } from 'react';
import Link from 'next/link';
import Image from 'next/image';
import { useRouter } from 'next/navigation';
import {
catalogService,
ProductResponse,
CategoryResponse,
BrandResponse,
} from '@/services/api/catalogService';
import { formatCurrency, getCatalogPriceInfo, getImageUrl, resolveProductImage } from '@/lib/utils';
import {
applyCatalogFilters,
CATALOG_API_LIMIT,
CATALOG_MAX_API_PAGES,
CATALOG_PAGE_SIZE,
CATALOG_PRICE_RANGES,
getDisplayStarCount,
productMatchesRating,
type CatalogHighlight,
} from '@/lib/catalogFilters';
import {
LayoutGrid,
List,
Minus,
Plus,
Heart,
Eye,
GitCompare,
ShoppingCart,
Star,
ChevronDown,
ChevronRight,
SlidersHorizontal,
X,
} from 'lucide-react';
import { useCartStore } from '@/store/cartStore';
import { AnnouncementBar } from '@/layout/AnnouncementBar';
import { Header } from '@/layout/Header';
import { Navbar } from '@/layout/Navbar';
import { Footer } from '@/layout/Footer';
import { StickyHeaderSpacer } from '@/components/StickyHeaderSpacer';
import { FloatingButtons } from '@/components/FloatingButtons';
import { ProductCardSkeleton } from '@/components/ui/Skeleton';
import { mapProductCardToProductResponse } from '@/utils/productMapper';
import { addToCompareAndNavigate } from '@/lib/compare';
import BlurHashImage from '@/components/ui/BlurHashImage';
interface StandardCatalogProps {
initialCategory?: CategoryResponse | null;
categoryParam?: string | null;
brandParam?: string | null;
searchQuery?: string | null;
}
const HIGHLIGHTS: CatalogHighlight[] = [
'All Products',
'Best Seller',
'New Arrivals',
'Sale',
'Hot Items',
];
type CollapseKey = 'categories' | 'brands' | 'highlight' | 'price';
function mapApiProduct(p: any): ProductResponse {
return mapProductCardToProductResponse(p);
}
function CatalogProductCard({
product,
viewMode,
}: {
product: ProductResponse;
viewMode: 'grid' | 'list';
}) {
const addToCart = useCartStore((s) => s.addToCart);
const toggleWishlist = useCartStore((s) => s.toggleWishlist);
const setQuickViewProduct = useCartStore((s) => s.setQuickViewProduct);
const router = useRouter();
const wishlist = useCartStore((s) => s.wishlist) || [];
const compareList = useCartStore((s) => s.compareList) || [];
const variant = product.variants?.[0];
const productUrl = product.variant_url || `/products/${product.slug}`;
const { price, oldPrice, discount: mappedDiscount } = getCatalogPriceInfo(product);
const compare = oldPrice || 0;
const discount =
mappedDiscount ||
(compare > price ? Math.round(((compare - price) / compare) * 100) : 0);
const image = resolveProductImage(product);
const rating = getDisplayStarCount(product);
const isWishlisted = wishlist.some((i) => i.product_id === product.product_id);
const isCompared = compareList.some((i) => i.product_id === product.product_id);
const hasOptions = (product.variants?.length || 0) > 1;
const ctaLabel = hasOptions ? 'Select Options' : 'Add To Cart';
const handleCart = (e: React.MouseEvent) => {
e.preventDefault();
e.stopPropagation();
if (!variant) return;
if (hasOptions) {
window.location.href = productUrl;
return;
}
void addToCart(product, variant, 1);
};
if (viewMode === 'list') {
return (
<div className="flex gap-5 p-5 border-b border-[#e8e8e8] bg-white hover:bg-[#fafafa] transition-colors group">
<Link href={productUrl} className="relative w-36 h-36 shrink-0 bg-white">
{discount > 0 && (
<span className="absolute top-0 right-0 z-10 bg-primary text-white text-[10px] font-bold px-1.5 py-0.5">
-{discount}%
</span>
)}
{image ? (
<BlurHashImage src={image} alt={product.name} fill className="object-contain p-2" />
) : (
<div className="w-full h-full bg-gray-50" />
)}
</Link>
<div className="flex-1 min-w-0 flex flex-col justify-center">
<Link
href={productUrl}
className="text-[14px] font-semibold text-[#222] hover:text-primary line-clamp-2 mb-1.5"
>
{product.name}
</Link>
<div className="flex items-center gap-0.5 text-amber-400 mb-2">
{Array.from({ length: 5 }).map((_, i) => (
<Star key={i} size={12} className={i < rating ? 'fill-amber-400' : 'text-gray-200'} />
))}
</div>
<div className="flex items-center gap-2 mb-3">
<span className="text-[15px] font-bold text-[#111]">{formatCurrency(price)}</span>
{compare > price && (
<span className="text-[12px] text-gray-400 line-through">{formatCurrency(compare)}</span>
)}
</div>
<div className="flex items-center gap-2">
<button
type="button"
onClick={handleCart}
className="w-fit px-5 py-2 bg-primary text-white text-[12px] font-semibold rounded-md hover:brightness-95 cursor-pointer"
>
{ctaLabel}
</button>
<button
type="button"
aria-label="Compare"
onClick={(e) => {
e.preventDefault();
e.stopPropagation();
void addToCompareAndNavigate(product, (href) => router.push(href));
}}
className={`w-9 h-9 rounded-md border flex items-center justify-center cursor-pointer ${
isCompared ? 'border-primary text-primary bg-primary/5' : 'border-gray-200 text-gray-500 hover:border-primary hover:text-primary'
}`}
>
<GitCompare size={14} />
</button>
</div>
</div>
</div>
);
}
return (
<div className="group relative flex flex-col bg-white p-4 md:p-5 min-h-[340px] border-r border-b border-[#e8e8e8]">
{discount > 0 && (
<span className="absolute top-3 right-3 z-10 bg-primary text-white text-[10px] font-bold px-1.5 py-0.5">
-{discount}%
</span>
)}
<div className="absolute left-3 top-3 z-20 flex flex-col gap-1.5 opacity-100 md:opacity-0 md:group-hover:opacity-100 transition-opacity duration-200">
<button
type="button"
aria-label="Wishlist"
onClick={(e) => {
e.preventDefault();
toggleWishlist(product);
}}
className={`w-8 h-8 rounded-full border bg-white flex items-center justify-center shadow-sm cursor-pointer ${
isWishlisted ? 'border-primary text-primary' : 'border-gray-200 text-gray-500 hover:border-primary hover:text-primary'
}`}
>
<Heart size={13} className={isWishlisted ? 'fill-current' : ''} />
</button>
<button
type="button"
aria-label="Compare"
onClick={(e) => {
e.preventDefault();
e.stopPropagation();
void addToCompareAndNavigate(product, (href) => router.push(href));
}}
className={`w-8 h-8 rounded-full border bg-white flex items-center justify-center shadow-sm cursor-pointer ${
isCompared ? 'border-primary text-primary' : 'border-gray-200 text-gray-500 hover:border-primary hover:text-primary'
}`}
>
<GitCompare size={13} />
</button>
<button
type="button"
aria-label="Quick view"
onClick={(e) => {
e.preventDefault();
setQuickViewProduct(product);
}}
className="w-8 h-8 rounded-full border border-gray-200 bg-white text-gray-500 hover:border-primary hover:text-primary flex items-center justify-center shadow-sm cursor-pointer"
>
<Eye size={13} />
</button>
</div>
<Link
href={productUrl}
className="relative flex-1 flex items-center justify-center min-h-[160px] mb-3"
>
{image ? (
<BlurHashImage
src={image}
alt={product.name}
width={180}
height={160}
className="max-h-[150px] w-auto object-contain"
/>
) : (
<div className="w-24 h-24 bg-gray-50 rounded" />
)}
</Link>
<div className="text-left mt-auto">
<Link
href={productUrl}
className="block text-[13px] font-semibold text-[#222] hover:text-primary line-clamp-2 leading-snug min-h-[36px] mb-1.5"
>
{product.name}
</Link>
<div className="flex items-center gap-0.5 text-amber-400 mb-1.5">
{Array.from({ length: 5 }).map((_, i) => (
<Star key={i} size={12} className={i < rating ? 'fill-amber-400' : 'text-gray-200'} />
))}
</div>
<div className="flex items-center gap-2">
<span className="text-[14px] font-bold text-[#111]">{formatCurrency(price)}</span>
{compare > price && (
<span className="text-[12px] text-gray-400 line-through">{formatCurrency(compare)}</span>
)}
</div>
</div>
<div className="overflow-hidden transition-all duration-300 max-h-0 opacity-0 group-hover:max-h-12 group-hover:opacity-100 group-hover:mt-3">
<button
type="button"
onClick={handleCart}
className="w-full py-2.5 bg-primary text-white text-[12px] font-semibold rounded-md hover:brightness-95 cursor-pointer inline-flex items-center justify-center gap-1.5"
>
<ShoppingCart size={13} />
{ctaLabel}
</button>
</div>
</div>
);
}
function FilterCard({
title,
open,
onToggle,
children,
}: {
title: string;
open: boolean;
onToggle: () => void;
children: React.ReactNode;
}) {
return (
<div className="bg-white border border-[#e5e5e5] rounded-lg overflow-hidden">
<button
type="button"
onClick={onToggle}
className="w-full flex items-center justify-between px-4 py-3.5 text-left cursor-pointer"
>
<span className="text-[14px] font-bold text-[#222]">{title}</span>
{open ? <Minus size={15} className="text-gray-500" /> : <Plus size={15} className="text-gray-500" />}
</button>
{open && <div className="px-4 pb-4 pt-0 border-t border-[#f0f0f0]">{children}</div>}
</div>
);
}
export function StandardCatalog({
initialCategory,
categoryParam,
brandParam,
searchQuery,
}: StandardCatalogProps) {
const router = useRouter();
const [productPool, setProductPool] = useState<ProductResponse[]>([]);
const [categories, setCategories] = useState<CategoryResponse[]>([]);
const [brands, setBrands] = useState<BrandResponse[]>([]);
const [selectedCategory, setSelectedCategory] = useState<string>(
categoryParam || initialCategory?.category_id || 'all'
);
const [selectedBrand, setSelectedBrand] = useState<string>(brandParam || 'all');
const [highlight, setHighlight] = useState<CatalogHighlight>('All Products');
const [priceRangeId, setPriceRangeId] = useState('all');
const [sortBy, setSortBy] = useState('default');
const [viewMode, setViewMode] = useState<'grid' | 'list'>('grid');
const [page, setPage] = useState(1);
const [isLoading, setIsLoading] = useState(true);
const [loadingMore, setLoadingMore] = useState(false);
const [isMobileFilterOpen, setIsMobileFilterOpen] = useState(false);
const [expandedCategoryIds, setExpandedCategoryIds] = useState<Record<string, boolean>>({});
const fetchIdRef = useRef(0);
const [collapsed, setCollapsed] = useState<Record<CollapseKey, boolean>>({
categories: false,
brands: false,
highlight: false,
price: false,
});
const [allProductsTotal, setAllProductsTotal] = useState<number>(0);
const [categoryCounts, setCategoryCounts] = useState<Record<string, number>>({});
useEffect(() => {
catalogService.getProductsPaginated({ limit: 1 }).then((res) => {
if (res && typeof res.total === 'number') {
setAllProductsTotal(res.total);
}
}).catch(() => {});
}, []);
const toggleCollapse = (key: CollapseKey) =>
setCollapsed((prev) => ({ ...prev, [key]: !prev[key] }));
// Synchronize URL search params seamlessly
const syncUrlParams = (catId: string, brandId: string) => {
if (typeof window === 'undefined') return;
const currentParams = new URLSearchParams(window.location.search);
if (catId && catId !== 'all') {
const cat = categories.find(
(c) => c.category_id === catId || c.slug.toLowerCase() === catId.toLowerCase()
);
currentParams.set('category', cat?.slug || catId);
} else {
currentParams.delete('category');
}
if (brandId && brandId !== 'all') {
const b = brands.find(
(br) => br.brand_id === brandId || br.slug.toLowerCase() === brandId.toLowerCase()
);
currentParams.set('brand', b?.slug || brandId);
} else {
currentParams.delete('brand');
}
const qs = currentParams.toString();
router.replace(`/shop${qs ? `?${qs}` : ''}`, { scroll: false });
};
const handleCategorySelect = (catId: string) => {
const nextCat = selectedCategory === catId ? 'all' : catId;
setSelectedCategory(nextCat);
syncUrlParams(nextCat, selectedBrand);
};
const handleBrandSelect = (brandId: string) => {
const nextBrand = selectedBrand === brandId ? 'all' : brandId;
setSelectedBrand(nextBrand);
syncUrlParams(selectedCategory, nextBrand);
};
useEffect(() => {
catalogService.getCategories().then((catData) => {
setCategories(catData);
const counts: Record<string, number> = {};
for (const c of catData) {
if (c.product_count != null) {
counts[c.category_id] = c.product_count;
counts[c.slug] = c.product_count;
}
}
setCategoryCounts(counts);
if (categoryParam) {
const found = catData.find(
(c) =>
c.slug.toLowerCase() === categoryParam.toLowerCase() ||
c.category_id === categoryParam
);
setSelectedCategory(found?.category_id || categoryParam);
} else if (initialCategory) {
setSelectedCategory(initialCategory.category_id);
}
});
}, [categoryParam, initialCategory]);
// Group root categories and their child subcategories
const rootCategories = useMemo(() => {
return categories.filter((c) => !c.parent_category_id);
}, [categories]);
const subCategoriesMap = useMemo(() => {
const map: Record<string, CategoryResponse[]> = {};
for (const cat of categories) {
if (cat.parent_category_id) {
if (!map[cat.parent_category_id]) map[cat.parent_category_id] = [];
map[cat.parent_category_id].push(cat);
}
}
return map;
}, [categories]);
// Auto-expand active category parent in accordion
useEffect(() => {
if (selectedCategory && selectedCategory !== 'all') {
const found = categories.find(
(c) =>
c.category_id === selectedCategory ||
c.slug.toLowerCase() === selectedCategory.toLowerCase()
);
if (found?.parent_category_id) {
setExpandedCategoryIds((prev) => ({ ...prev, [found.parent_category_id!]: true }));
} else if (found?.category_id) {
setExpandedCategoryIds((prev) => ({ ...prev, [found.category_id]: true }));
}
}
}, [selectedCategory, categories]);
useEffect(() => {
const activeCatId = selectedCategory === 'all' ? undefined : selectedCategory;
catalogService.getBrands(activeCatId).then((brandData) => {
setBrands(brandData);
if (brandParam) {
const found = brandData.find(
(b) =>
b.slug.toLowerCase() === brandParam.toLowerCase() ||
b.brand_id === brandParam
);
setSelectedBrand(found?.brand_id || brandParam);
} else {
setSelectedBrand('all');
}
});
}, [selectedCategory, brandParam]);
useEffect(() => {
setPage(1);
}, [
selectedCategory,
selectedBrand,
searchQuery,
highlight,
priceRangeId,
sortBy,
]);
useEffect(() => {
const fetchId = ++fetchIdRef.current;
setIsLoading(true);
setProductPool([]);
const loadAll = async () => {
const paramsBase: Record<string, string | number> = {
limit: CATALOG_API_LIMIT,
};
if (selectedCategory !== 'all') paramsBase.category = selectedCategory;
if (selectedBrand !== 'all') paramsBase.brand = selectedBrand;
if (searchQuery) {
paramsBase.search = searchQuery;
// Sunsky-style: keyword searches expand each active variant into its own card
paramsBase.expand_variants = 'true';
}
const accumulated: ProductResponse[] = [];
const seen = new Set<string>();
let apiPage = 1;
let total = Number.POSITIVE_INFINITY;
while (apiPage <= CATALOG_MAX_API_PAGES && accumulated.length < total) {
try {
const res = await catalogService.getProductsPaginated({
...paramsBase,
page: apiPage,
});
if (fetchId !== fetchIdRef.current) return;
total = typeof res.total === 'number' ? res.total : accumulated.length;
const mapped = (res.products || []).map(mapApiProduct);
if (mapped.length === 0) break;
let added = 0;
for (const product of mapped) {
// Variant-expanded cards share the same product_id — dedupe per variant
const dedupeKey = product.variant_url || product.product_id;
if (seen.has(dedupeKey)) continue;
seen.add(dedupeKey);
accumulated.push(product);
added += 1;
}
if (added > 0) {
setProductPool([...accumulated]);
if (apiPage === 1) setIsLoading(false);
}
if (accumulated.length >= total || mapped.length < CATALOG_API_LIMIT) break;
apiPage += 1;
} catch {
break;
}
}
if (fetchId !== fetchIdRef.current) return;
setProductPool([...accumulated]);
setIsLoading(false);
};
void loadAll();
}, [selectedCategory, selectedBrand, searchQuery]);
const filteredProducts = useMemo(() => {
return applyCatalogFilters(productPool, {
priceRangeId,
selectedRating: null,
highlight,
sortBy,
});
}, [productPool, priceRangeId, highlight, sortBy]);
const displayed = useMemo(
() => filteredProducts.slice(0, page * CATALOG_PAGE_SIZE),
[filteredProducts, page]
);
const totalProducts = filteredProducts.length;
const showingFrom = displayed.length === 0 ? 0 : 1;
const showingTo = displayed.length;
const hasMore = displayed.length < totalProducts;
const resetFilters = () => {
setSelectedCategory('all');
setSelectedBrand('all');
setHighlight('All Products');
setPriceRangeId('all');
setSortBy('default');
setPage(1);
if (typeof window !== 'undefined') {
const currentParams = new URLSearchParams(window.location.search);
currentParams.delete('category');
currentParams.delete('brand');
currentParams.delete('search');
const qs = currentParams.toString();
router.replace(`/shop${qs ? `?${qs}` : ''}`, { scroll: false });
}
};
const handleLoadMore = () => {
setLoadingMore(true);
setPage((p) => p + 1);
requestAnimationFrame(() => setLoadingMore(false));
};
const activeFiltersCount = useMemo(() => {
let count = 0;
if (selectedCategory !== 'all') count++;
if (selectedBrand !== 'all') count++;
if (highlight !== 'All Products') count++;
if (priceRangeId !== 'all') count++;
return count;
}, [selectedCategory, selectedBrand, highlight, priceRangeId]);
// Reusable Filter Cards Component (used in both desktop sidebar & mobile drawer)
const renderFiltersContent = () => (
<div className="space-y-4">
{/* 1. Categories Accordion */}
<FilterCard
title="Shop By Categories"
open={!collapsed.categories}
onToggle={() => toggleCollapse('categories')}
>
<div className="pt-3 space-y-2 max-h-80 overflow-y-auto pr-1">
<label className="flex items-center justify-between text-[13px] cursor-pointer group py-1">
<span className="flex items-center gap-2.5">
<input
type="checkbox"
checked={selectedCategory === 'all'}
onChange={() => handleCategorySelect('all')}
className="w-3.5 h-3.5 rounded border-gray-300 text-primary accent-primary cursor-pointer shrink-0"
/>
<span className={selectedCategory === 'all' ? 'text-primary font-semibold' : 'text-gray-700'}>
All Categories
</span>
</span>
<span className="text-gray-400 text-xs shrink-0">({allProductsTotal || 0})</span>
</label>
{rootCategories.map((parent) => {
const isParentActive =
selectedCategory === parent.category_id ||
selectedCategory.toLowerCase() === parent.slug.toLowerCase();
const parentCount =
categoryCounts[parent.category_id] ??
categoryCounts[parent.slug] ??
parent.product_count ??
0;
const subs = subCategoriesMap[parent.category_id] || parent.sub_categories || [];
const hasSubs = subs.length > 0;
const isExpanded = !!expandedCategoryIds[parent.category_id];
return (
<div key={parent.category_id} className="space-y-1">
<div className="flex items-center justify-between text-[13px] group">
<label className="flex items-center gap-2.5 min-w-0 cursor-pointer flex-1 py-1">
<input
type="checkbox"
checked={isParentActive}
onChange={() => handleCategorySelect(parent.category_id)}
className="w-3.5 h-3.5 rounded border-gray-300 text-primary accent-primary cursor-pointer shrink-0"
/>
<span className={`truncate ${isParentActive ? 'text-primary font-semibold' : 'text-gray-700 group-hover:text-primary'}`}>
{parent.name}
</span>
</label>
<div className="flex items-center gap-1.5 shrink-0 ml-1">
<span className="text-gray-400 text-xs">({parentCount})</span>
{hasSubs && (
<button
type="button"
onClick={(e) => {
e.preventDefault();
setExpandedCategoryIds((prev) => ({
...prev,
[parent.category_id]: !prev[parent.category_id],
}));
}}
className="p-1 text-gray-400 hover:text-gray-700 rounded hover:bg-gray-100 transition-colors"
aria-label={`Toggle ${parent.name} subcategories`}
>
<ChevronDown
size={14}
className={`transition-transform duration-200 ${isExpanded ? 'rotate-180' : ''}`}
/>
</button>
)}
</div>
</div>
{hasSubs && isExpanded && (
<div className="pl-6 pr-1 py-1 space-y-1.5 border-l-2 border-gray-100 ml-1.5">
{subs.map((sub) => {
const isSubActive =
selectedCategory === sub.category_id ||
selectedCategory.toLowerCase() === sub.slug.toLowerCase();
const subCount =
categoryCounts[sub.category_id] ??
categoryCounts[sub.slug] ??
sub.product_count ??
0;
return (
<label
key={sub.category_id}
className="flex items-center justify-between text-[12.5px] cursor-pointer group py-0.5"
>
<span className="flex items-center gap-2 min-w-0">
<input
type="checkbox"
checked={isSubActive}
onChange={() => handleCategorySelect(sub.category_id)}
className="w-3 h-3 rounded border-gray-300 text-primary accent-primary cursor-pointer shrink-0"
/>
<span className={`truncate ${isSubActive ? 'text-primary font-semibold' : 'text-gray-600 group-hover:text-primary'}`}>
{sub.name}
</span>
</span>
<span className="text-gray-400 text-[11px] shrink-0">({subCount})</span>
</label>
);
})}
</div>
)}
</div>
);
})}
</div>
</FilterCard>
{/* 2. Brands */}
<FilterCard title="Brands" open={!collapsed.brands} onToggle={() => toggleCollapse('brands')}>
<div className="pt-3 space-y-2.5 max-h-56 overflow-y-auto pr-1">
<label className="flex items-center gap-2.5 text-[13px] cursor-pointer">
<input
type="checkbox"
checked={selectedBrand === 'all'}
onChange={() => handleBrandSelect('all')}
className="w-3.5 h-3.5 rounded border-gray-300 accent-primary cursor-pointer"
/>
<span className={selectedBrand === 'all' ? 'text-primary font-semibold' : 'text-gray-600'}>
All Brands
</span>
</label>
{brands.map((brand) => {
const checked = selectedBrand === brand.brand_id;
return (
<label key={brand.brand_id} className="flex items-center gap-2.5 text-[13px] cursor-pointer">
<input
type="checkbox"
checked={checked}
onChange={() => handleBrandSelect(brand.brand_id)}
className="w-3.5 h-3.5 rounded border-gray-300 accent-primary cursor-pointer"
/>
<span className={checked ? 'text-primary font-semibold' : 'text-gray-600'}>
{brand.name}
</span>
</label>
);
})}
</div>
</FilterCard>
{/* 3. Highlight */}
<FilterCard
title="Highlight"
open={!collapsed.highlight}
onToggle={() => toggleCollapse('highlight')}
>
<div className="pt-3 space-y-2">
{HIGHLIGHTS.map((item) => (
<button
key={item}
type="button"
onClick={() => setHighlight(item)}
className={`block w-full text-left text-[13px] py-0.5 cursor-pointer ${
highlight === item ? 'text-primary font-semibold' : 'text-gray-600 hover:text-primary'
}`}
>
{item}
</button>
))}
</div>
</FilterCard>
{/* 4. Price Filter */}
<FilterCard title="Price Filter" open={!collapsed.price} onToggle={() => toggleCollapse('price')}>
<div className="pt-3 space-y-2">
{CATALOG_PRICE_RANGES.map((range) => (
<button
key={range.id}
type="button"
onClick={() => setPriceRangeId(range.id)}
className={`block w-full text-left text-[13px] py-0.5 cursor-pointer ${
priceRangeId === range.id
? 'text-primary font-semibold'
: 'text-gray-600 hover:text-primary'
}`}
>
{range.label}
</button>
))}
</div>
</FilterCard>
</div>
);
return (
<div className="min-h-screen bg-white text-[#1E293B] flex flex-col font-sans">
<AnnouncementBar />
<Header />
<StickyHeaderSpacer />
<Navbar />
<main className="flex-grow pb-16">
<div className="container mx-auto px-4 sm:px-6 lg:px-8 py-6 max-w-[1400px]">
{/* Mobile Filter & Sort Bar (Visible on mobile/tablet < lg) */}
<div className="lg:hidden mb-4 flex items-center justify-between gap-3 bg-gray-50 p-3 rounded-lg border border-gray-200">
<button
type="button"
onClick={() => setIsMobileFilterOpen(true)}
className="flex items-center gap-2 px-3.5 py-2 bg-white border border-gray-300 rounded-md shadow-xs text-[13px] font-semibold text-gray-800 hover:border-primary active:bg-gray-50 transition-colors cursor-pointer"
>
<SlidersHorizontal size={14} className="text-primary" />
<span>Filters</span>
{activeFiltersCount > 0 && (
<span className="w-5 h-5 rounded-full bg-primary text-white text-[11px] font-bold flex items-center justify-center">
{activeFiltersCount}
</span>
)}
</button>
<div className="flex items-center gap-2">
<span className="text-xs text-gray-500 font-medium hidden sm:inline">Sort:</span>
<div className="relative">
<select
value={sortBy}
onChange={(e) => setSortBy(e.target.value)}
className="appearance-none bg-white border border-gray-300 rounded-md pl-2.5 pr-7 py-2 text-[12px] font-medium text-gray-700 cursor-pointer focus:outline-none focus:border-primary"
>
<option value="default">Default</option>
<option value="newest">Newest</option>
<option value="price-low">Price: Low to High</option>
<option value="price-high">Price: High to Low</option>
<option value="a-z">A–Z</option>
</select>
<ChevronDown
size={12}
className="absolute right-2 top-1/2 -translate-y-1/2 text-gray-400 pointer-events-none"
/>
</div>
</div>
</div>
{/* Active Filter Chips */}
{activeFiltersCount > 0 && (
<div className="mb-4 flex flex-wrap items-center gap-2 text-xs">
<span className="text-gray-500 font-medium">Active:</span>
{selectedCategory !== 'all' && (
<button
type="button"
onClick={() => handleCategorySelect('all')}
className="inline-flex items-center gap-1.5 px-2.5 py-1 bg-primary/10 text-primary font-medium rounded-full hover:bg-primary/20 transition-colors cursor-pointer"
>
<span>
{categories.find(
(c) => c.category_id === selectedCategory || c.slug.toLowerCase() === selectedCategory.toLowerCase()
)?.name || 'Category'}
</span>
<X size={12} />
</button>
)}
{selectedBrand !== 'all' && (
<button
type="button"
onClick={() => handleBrandSelect('all')}
className="inline-flex items-center gap-1.5 px-2.5 py-1 bg-primary/10 text-primary font-medium rounded-full hover:bg-primary/20 transition-colors cursor-pointer"
>
<span>
{brands.find(
(b) => b.brand_id === selectedBrand || b.slug.toLowerCase() === selectedBrand.toLowerCase()
)?.name || 'Brand'}
</span>
<X size={12} />
</button>
)}
{priceRangeId !== 'all' && (
<button
type="button"
onClick={() => setPriceRangeId('all')}
className="inline-flex items-center gap-1.5 px-2.5 py-1 bg-primary/10 text-primary font-medium rounded-full hover:bg-primary/20 transition-colors cursor-pointer"
>
<span>{CATALOG_PRICE_RANGES.find((r) => r.id === priceRangeId)?.label}</span>
<X size={12} />
</button>
)}
{highlight !== 'All Products' && (
<button
type="button"
onClick={() => setHighlight('All Products')}
className="inline-flex items-center gap-1.5 px-2.5 py-1 bg-primary/10 text-primary font-medium rounded-full hover:bg-primary/20 transition-colors cursor-pointer"
>
<span>{highlight}</span>
<X size={12} />
</button>
)}
<button
type="button"
onClick={resetFilters}
className="text-xs text-gray-500 hover:text-red-600 underline ml-1 cursor-pointer"
>
Reset All
</button>
</div>
)}
<div className="flex flex-col lg:flex-row gap-7 items-start">
{/* Desktop Sidebar: hidden on mobile (< lg), shown on lg+ */}
<aside className="hidden lg:block w-[260px] shrink-0 space-y-4">
{renderFiltersContent()}
</aside>
<div className="flex-1 min-w-0 w-full">
<div className="flex flex-col sm:flex-row sm:items-center justify-between gap-3 mb-5">
<p className="text-[13px] text-gray-600">
Showing{' '}
<span className="font-semibold text-gray-900">
{showingFrom}–{showingTo}
</span>{' '}
of <span className="font-semibold text-gray-900">{totalProducts}</span> results
</p>
<div className="flex items-center gap-3">
<div className="relative">
<select
value={sortBy}
onChange={(e) => setSortBy(e.target.value)}
className="appearance-none bg-white border border-[#e5e5e5] rounded-md pl-3 pr-8 py-2 text-[13px] text-gray-700 cursor-pointer focus:outline-none focus:border-primary"
>
<option value="default">Default sorting</option>
<option value="newest">Newest</option>
<option value="price-low">Price: low to high</option>
<option value="price-high">Price: high to low</option>
<option value="a-z">Name: A–Z</option>
</select>
<ChevronDown
size={13}
className="absolute right-2.5 top-1/2 -translate-y-1/2 text-gray-400 pointer-events-none"
/>
</div>
<div className="flex items-center gap-1">
<button
type="button"
onClick={() => setViewMode('grid')}
className={`p-2 rounded-sm cursor-pointer ${
viewMode === 'grid' ? 'bg-primary text-white' : 'bg-gray-100 text-gray-500'
}`}
aria-label="Grid view"
>
<LayoutGrid size={15} />
</button>
<button
type="button"
onClick={() => setViewMode('list')}
className={`p-2 rounded-sm cursor-pointer ${
viewMode === 'list' ? 'bg-primary text-white' : 'bg-gray-100 text-gray-500'
}`}
aria-label="List view"
>
<List size={15} />
</button>
</div>
</div>
</div>
{isLoading ? (
<div className="border border-[#e8e8e8] grid grid-cols-2 md:grid-cols-3 xl:grid-cols-4">
{Array.from({ length: 8 }).map((_, i) => (
<div
key={i}
className="border-r border-b border-[#e8e8e8]"
>
<ProductCardSkeleton />
</div>
))}
</div>
) : displayed.length === 0 ? (
<div className="border border-[#e8e8e8] rounded-lg p-12 text-center">
<p className="text-sm text-gray-500 mb-4">No products found matching your filters.</p>
<button
type="button"
onClick={resetFilters}
className="text-[13px] font-semibold text-primary hover:underline cursor-pointer"
>
Clear all filters
</button>
</div>
) : viewMode === 'grid' ? (
<div className="border border-[#e8e8e8] border-b-0 border-r-0 grid grid-cols-2 md:grid-cols-3 xl:grid-cols-4">
{displayed.map((p) => (
<CatalogProductCard key={(p as any).variant_sku || p.variant_url || p.product_id} product={p} viewMode="grid" />
))}
</div>
) : (
<div className="border border-[#e8e8e8] rounded-sm overflow-hidden">
{displayed.map((p) => (
<CatalogProductCard key={(p as any).variant_sku || p.variant_url || p.product_id} product={p} viewMode="list" />
))}
</div>
)}
{hasMore && !isLoading && (
<div className="flex justify-center mt-8">
<button
type="button"
disabled={loadingMore}
onClick={handleLoadMore}
className="px-10 py-3 bg-primary text-white text-[13px] font-semibold rounded-md hover:brightness-95 disabled:opacity-60 cursor-pointer"
>
{loadingMore ? 'Loading…' : 'Load More'}
</button>
</div>
)}
</div>
</div>
</div>
</main>
{/* Slide-over Mobile Filter Drawer */}
{isMobileFilterOpen && (
<div className="fixed inset-0 z-[100] lg:hidden flex justify-end">
{/* Backdrop */}
<div
className="fixed inset-0 bg-black/50 backdrop-blur-xs transition-opacity"
onClick={() => setIsMobileFilterOpen(false)}
/>
{/* Drawer Sheet */}
<div className="relative z-10 w-full max-w-[320px] bg-white h-full flex flex-col shadow-2xl animate-in slide-in-from-right duration-300">
{/* Header */}
<div className="flex items-center justify-between px-5 py-4 border-b border-gray-100">
<div className="flex items-center gap-2">
<SlidersHorizontal size={16} className="text-primary" />
<h3 className="text-base font-bold text-gray-900">Filters</h3>
{activeFiltersCount > 0 && (
<span className="w-5 h-5 rounded-full bg-primary text-white text-[11px] font-bold flex items-center justify-center">
{activeFiltersCount}
</span>
)}
</div>
<button
type="button"
onClick={() => setIsMobileFilterOpen(false)}
className="p-1.5 text-gray-400 hover:text-gray-700 rounded-full hover:bg-gray-100 cursor-pointer"
aria-label="Close filters"
>
<X size={18} />
</button>
</div>
{/* Scrollable Body */}
<div className="flex-1 overflow-y-auto p-4 space-y-4">
{renderFiltersContent()}
</div>
{/* Sticky Footer */}
<div className="p-4 border-t border-gray-100 bg-white flex items-center gap-3">
<button
type="button"
onClick={resetFilters}
className="flex-1 py-2.5 px-3 border border-gray-300 text-gray-700 text-xs font-semibold rounded-lg hover:bg-gray-50 cursor-pointer"
>
Reset All
</button>
<button
type="button"
onClick={() => setIsMobileFilterOpen(false)}
className="flex-1 py-2.5 px-3 bg-primary text-white text-xs font-bold rounded-lg hover:brightness-95 shadow-sm cursor-pointer"
>
Apply ({totalProducts})
</button>
</div>
</div>
</div>
)}
<Footer />
<FloatingButtons />
</div>
);
}