"use client"; import React, { useEffect, useRef } from "react"; import { useRouter, useSearchParams, usePathname } from "next/navigation"; import { useVehicleData } from "@/hooks/useVehicleData"; import { useYmmStore } from "@/store/useYmmStore"; interface YMMBarProps { onSearch?: (fitment: string) => void; className?: string; } export const YMMBar = ({ onSearch, className }: YMMBarProps) => { const params = useSearchParams(); const router = useRouter(); const pathname = usePathname(); const isYmmActive = useYmmStore((state) => state.isYmmActive); const previousPairsRef = useRef(null); const isSearchPage = pathname === "/search"; const { rootTypes, selectedRootType, dropdownLevels, handleRootTypeChange, handleValueChange, getSelectedPairs, initializeFromPairs, resetInitialization, } = useVehicleData(); const initialPairs = params.get("fitment_pairs"); useEffect(() => { if ( rootTypes.length > 0 && selectedRootType === 0 && dropdownLevels.length === 0 && !initialPairs ) { handleRootTypeChange(rootTypes[0].id); } }, [rootTypes]); useEffect(() => { if (!isSearchPage || !initialPairs) { return; } if (previousPairsRef.current === initialPairs) { return; } if (rootTypes.length === 0) { return; } previousPairsRef.current = initialPairs; initializeFromPairs(initialPairs); }, [initialPairs, isSearchPage, rootTypes]); useEffect(() => { return () => { if (isSearchPage) { resetInitialization(); previousPairsRef.current = null; } }; }, [isSearchPage]); const handleSearch = () => { const pairs = getSelectedPairs(); if (onSearch) { onSearch(pairs); } else { router.push(`/search?fitment_pairs=${pairs}`); } }; const handleClear = () => { resetInitialization(); previousPairsRef.current = null; if (isSearchPage) { router.push(`/search`); } if (rootTypes.length > 0) { setTimeout(() => { handleRootTypeChange(rootTypes[0].id); }, 100); } }; const hasSelectedFilters = dropdownLevels.some( (level) => level.selectedValue !== "" ); if (!isYmmActive) { return null; } return (
{/* Title with decorative slashes */}
{"///"} FIND YOUR PERFECT FIT
{/* Dropdowns and buttons container */}
{/* Dynamic Dropdown Levels - show first 3 */} {dropdownLevels.length > 0 ? dropdownLevels.slice(0, 3).map((level, index) => { const selectId = `ymm-bar-${level.typeName.toLowerCase()}-${index}`; return (
{/* Custom dropdown arrow */}
); }) : Array.from({ length: 3 }).map((_, i) => (
{/* Skeleton dropdown arrow */}
))} {/* Search Button */} {/* Clear Button */}
); };