import { NavigationMenu, NavigationMenuContent, NavigationMenuItem, NavigationMenuLink, NavigationMenuList, NavigationMenuTrigger, navigationMenuTriggerStyle, } from "@/components/ui/navigation-menu"; import { t } from "@/utils"; import CustomLink from "@/components/Common/CustomLink"; import { useEffect, useRef, useState } from "react"; import { IoIosMore } from "react-icons/io"; import CustomImage from "@/components/Common/CustomImage"; import { useNavigate } from "@/components/Common/useNavigate"; import { usePathname, useSearchParams } from "next/navigation"; const HeaderCategories = ({ cateData }) => { const containerRef = useRef(null); const measureRef = useRef(null); const pathname = usePathname(); const searchParams = useSearchParams(); const { navigate } = useNavigate(); const [fitCategoriesCount, setFitCategoriesCount] = useState(3); useEffect(() => { const calculateFit = () => { if (!containerRef.current || !measureRef.current) return; const containerWidth = containerRef.current.offsetWidth; const otherWidth = 80; //approx width of other option const availableWidth = containerWidth - otherWidth; const items = Array.from(measureRef.current.children); let totalWidth = 0; let visible = 0; for (const item of items) { const width = item.getBoundingClientRect().width + 48; // padding/gap buffer if (totalWidth + width > availableWidth) break; totalWidth += width; visible++; } setFitCategoriesCount(visible); }; const resizeObserver = new ResizeObserver(calculateFit); if (containerRef.current) { resizeObserver.observe(containerRef.current); } return () => resizeObserver.disconnect(); }, [cateData]); // Helper function to build URL with category while preserving existing search params const buildCategoryUrl = (categorySlug) => { if (pathname.startsWith("/ads")) { // Preserve existing search params and update category const newSearchParams = new URLSearchParams(searchParams.toString()); newSearchParams.delete("lang"); newSearchParams.set("category", categorySlug); return `/ads?${newSearchParams.toString()}`; } else { // Not on ads page, just add category return `/ads?category=${categorySlug}`; } }; const handleCategoryClick = (slug) => { if (pathname.startsWith("/ads")) { const newSearchParams = new URLSearchParams(searchParams.toString()); newSearchParams.set("category", slug); const newUrl = `/ads?${newSearchParams.toString()}`; window.history.pushState(null, "", newUrl); } else { navigate(`/ads?category=${slug}`); } }; const handleOtherCategoryClick = () => { if (pathname.startsWith("/ads")) { const newSearchParams = new URLSearchParams(searchParams.toString()); newSearchParams.delete("category"); const newUrl = `/ads?${newSearchParams.toString()}`; window.history.pushState(null, "", newUrl); } else { navigate(`/ads`); } }; return (
{category?.translated_name}
{t("other")}