Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | "use client";
import { Swiper, SwiperSlide } from "swiper/react";
import { useRef, useEffect, useState } from "react";
import type { Swiper as SwiperClass } from "swiper";
import { Icon } from "@/components/ui/icons";
import { clientLogger } from "@/lib/logging/clientLogger";
// Import Swiper styles
import "swiper/css/navigation";
import "swiper/css";
import SingleItem from "./SingleItem";
interface CategoryItem {
id: number;
name: string;
imageUrl?: string | null;
products?: number;
}
const Categories = () => {
const sliderRef = useRef<SwiperClass | null>(null);
const [categories, setCategories] = useState<CategoryItem[]>([]);
const [loading, setLoading] = useState(true);
// Fetch parent categories from API
useEffect(() => {
const fetchCategories = async () => {
try {
const response = await fetch("/api/categories?parentOnly=true");
if (!response.ok) throw new Error("Failed to fetch categories");
const data = await response.json();
setCategories(data.data || []);
} catch (error) {
clientLogger.error("Error fetching categories", error instanceof Error ? error : new Error(String(error)));
setCategories([]);
} finally {
setLoading(false);
}
};
fetchCategories();
}, []);
const handlePrev = () => {
if (!sliderRef.current) return;
sliderRef.current.slidePrev();
};
const handleNext = () => {
if (!sliderRef.current) return;
sliderRef.current.slideNext();
};
// Don't render until categories are loaded - skeleton matches Swiper single-row layout to prevent CLS
if (loading) {
return (
<section className="overflow-hidden pt-17.5">
<div className="max-w-[1170px] w-full mx-auto px-4 sm:px-8 xl:px-0 pb-15 border-b border-gray-3 dark:border-gray-700">
<div className="mb-10 flex items-center justify-between">
<div>
<span className="flex items-center gap-2.5 font-medium text-dark dark:text-gray-200 mb-1.5">
<Icon name="tag" className="text-blue" size={20} />
Categories
</span>
<h2 className="font-semibold text-xl xl:text-heading-5 text-dark dark:text-gray-100">
Browse by Category
</h2>
</div>
<div className="flex items-center gap-3">
<div className="w-10 h-10 bg-gray-200 dark:bg-gray-700 rounded animate-pulse" />
<div className="w-10 h-10 bg-gray-200 dark:bg-gray-700 rounded animate-pulse" />
</div>
</div>
{/* Skeleton matches Swiper: shows 2 items on mobile, 4 on md, 6 on xl - single row */}
<div className="flex gap-4 overflow-hidden">
{[...Array(6)].map((_, i) => (
<div
key={i}
className={`flex-shrink-0 flex flex-col items-center animate-pulse ${
i >= 4 ? 'hidden xl:flex' : i >= 2 ? 'hidden md:flex' : ''
}`}
>
<div className="w-[130px] h-[130px] bg-gray-200 dark:bg-gray-700 rounded-full mb-4" />
<div className="w-20 h-4 bg-gray-200 dark:bg-gray-700 rounded" />
</div>
))}
</div>
</div>
</section>
);
}
// Don't render if no categories
if (categories.length === 0) {
return null;
}
return (
<section className="overflow-hidden pt-17.5">
<div className="max-w-[1170px] w-full mx-auto px-4 sm:px-8 xl:px-0 pb-15 border-b border-gray-3 dark:border-gray-700">
<div className="swiper categories-carousel common-carousel">
{/* <!-- section title --> */}
<div className="mb-10 flex items-center justify-between">
<div>
<span className="flex items-center gap-2.5 font-medium text-dark dark:text-gray-200 mb-1.5">
<Icon name="tag" className="text-blue" size={20} />
Categories
</span>
<h2 className="font-semibold text-xl xl:text-heading-5 text-dark dark:text-gray-100">
Browse by Category
</h2>
</div>
<div className="flex items-center gap-3">
<div onClick={handlePrev} className="swiper-button-prev cursor-pointer">
<Icon name="chevron-left" className="fill-current" size={24} />
</div>
<div onClick={handleNext} className="swiper-button-next cursor-pointer">
<Icon name="chevron-right" className="fill-current" size={24} />
</div>
</div>
</div>
<Swiper
slidesPerView={6}
onSwiper={(swiper) => {
sliderRef.current = swiper;
}}
breakpoints={{
// when window width is >= 640px
0: {
slidesPerView: 2 },
1000: {
slidesPerView: 4
// spaceBetween: 4
},
// when window width is >= 768px
1200: {
slidesPerView: 6 } }}
>
{categories.map((item) => (
<SwiperSlide key={item.id}>
<SingleItem item={item} />
</SwiperSlide>
))}
</Swiper>
</div>
</div>
</section>
);
};
export default Categories;
|