"use client"; import { useEffect, useState } from "react"; import { RiArrowLeftLine, RiArrowRightLine } from "react-icons/ri"; import { Carousel, CarouselContent, CarouselItem, } from "@/components/ui/carousel"; import PopularCategoriesSkeleton from "./PopularCategoriesSkeleton.jsx"; import PopularCategoryCard from "@/components/PagesComponent/Home/PopularCategoryCard"; import { useSelector } from "react-redux"; import { t } from "@/utils"; import { getIsRtl } from "@/redux/reducer/languageSlice.js"; import { Loader2 } from "lucide-react"; import useGetCategories from "@/components/Layout/useGetCategories.jsx"; const PopularCategories = () => { const { cateData, getCategories, isCatLoading, isCatLoadMore, catLastPage, catCurrentPage, } = useGetCategories(); const isRTL = useSelector(getIsRtl); const [api, setApi] = useState(); const [current, setCurrent] = useState(0); const isNextDisabled = isCatLoadMore || ((!api || !api.canScrollNext()) && catCurrentPage >= catLastPage); useEffect(() => { if (!api) { return; } setCurrent(api.selectedScrollSnap()); api.on("select", () => { setCurrent(api.selectedScrollSnap()); }); }, [api, cateData.length]); const handleNext = async () => { if (api && api.canScrollNext()) { api.scrollTo(current + 1); } else if (catCurrentPage < catLastPage) { await getCategories(catCurrentPage + 1); setTimeout(() => { api.scrollTo(current + 1); }, 200); } }; return isCatLoading && !cateData.length ? ( ) : ( cateData && cateData.length > 0 && ( {t("popularCategories")} api && api.scrollTo(current - 1)} className={`bg-primary p-1 sm:p-2 rounded-full ${ !api?.canScrollPrev() ? "opacity-65 cursor-default" : "" }`} disabled={!api?.canScrollPrev()} > {isCatLoadMore ? ( ) : ( )} {cateData.map((item) => ( ))} ) ); }; export default PopularCategories;