import React, { useState, useRef } from "react"; import { PartsSearchHistoryItem } from '@/lib/graphql/search-history'; interface ProfileHistoryTabsProps { tabs: string[]; activeTab: string; onTabChange: (tab: string) => void; historyItems: PartsSearchHistoryItem[]; selectedManufacturer: string; onManufacturerChange: (manufacturer: string) => void; } const ProfileHistoryTabs: React.FC = ({ tabs, activeTab, onTabChange, historyItems, selectedManufacturer, onManufacturerChange, }) => { const [isDropdownOpen, setIsDropdownOpen] = useState(false); const dropdownRef = useRef(null); // Получаем уникальных производителей из истории поиска const getUniqueManufacturers = () => { const manufacturersSet = new Set(); historyItems.forEach(item => { // Добавляем бренд из поля brand if (item.brand) { manufacturersSet.add(item.brand); } // Добавляем бренд из информации об автомобиле if (item.vehicleInfo?.brand) { manufacturersSet.add(item.vehicleInfo.brand); } }); const uniqueManufacturers = Array.from(manufacturersSet).sort(); return ["Все", ...uniqueManufacturers]; }; const manufacturers = getUniqueManufacturers(); // Закрытие дропдауна при клике вне React.useEffect(() => { function handleClickOutside(event: MouseEvent) { if (dropdownRef.current && !dropdownRef.current.contains(event.target as Node)) { setIsDropdownOpen(false); } } if (isDropdownOpen) { document.addEventListener("mousedown", handleClickOutside); } else { document.removeEventListener("mousedown", handleClickOutside); } return () => { document.removeEventListener("mousedown", handleClickOutside); }; }, [isDropdownOpen]); const handleManufacturerSelect = (manufacturer: string) => { onManufacturerChange(manufacturer); setIsDropdownOpen(false); }; return (
{tabs.map((tab) => (
onTabChange(tab)} >
{tab}
))}
setIsDropdownOpen((prev) => !prev)} > {selectedManufacturer}
{isDropdownOpen && (
    {manufacturers.length === 0 ? (
  • Нет данных
  • ) : ( manufacturers.map((manufacturer) => (
  • handleManufacturerSelect(manufacturer)} > {manufacturer} {manufacturer !== "Все" && ( ({historyItems.filter(item => item.brand === manufacturer || item.vehicleInfo?.brand === manufacturer ).length}) )}
  • )) )}
)}
); }; export default ProfileHistoryTabs;