'use client' import { useState } from 'react' import { useMutation } from '@apollo/client' import { Card, CardContent } from '@/components/ui/card' import { Button } from '@/components/ui/button' import { Badge } from '@/components/ui/badge' import { Package, ChevronRight, FolderOpen, Layers, Image as ImageIcon, Download, Loader2 } from 'lucide-react' import { FETCH_CATEGORY_PRODUCTS } from '@/lib/graphql/queries' import toast from 'react-hot-toast' interface PartsIndexCategory { id: string name: string image?: string groups?: Array<{ id: string name: string image?: string subgroups?: Array<{ id: string name: string image?: string entityNames?: Array<{ id: string name: string }> }> entityNames?: Array<{ id: string name: string }> }> } interface PartsAPICategory { id: string name: string level: number parentId?: string children?: PartsAPICategory[] } interface KrajaCategoriesProps { categories: PartsIndexCategory[] | PartsAPICategory[] onCategorySelect: (category: PartsIndexCategory | PartsAPICategory, group?: any) => void type: 'partsindex' | 'partsapi' } export const KrajaCategories = ({ categories, onCategorySelect, type }: KrajaCategoriesProps) => { const [expandedCategories, setExpandedCategories] = useState>(new Set()) const [fetchingCategories, setFetchingCategories] = useState>(new Set()) const [fetchCategoryProducts] = useMutation(FETCH_CATEGORY_PRODUCTS, { onCompleted: (data) => { if (data.fetchCategoryProducts.success) { toast.success(`✅ ${data.fetchCategoryProducts.message}`) } else { toast.error(`❌ ${data.fetchCategoryProducts.message}`) } }, onError: (error) => { toast.error(`❌ ${error.message}`) } }) const toggleCategory = (categoryId: string) => { setExpandedCategories(prev => { const newSet = new Set(prev) if (newSet.has(categoryId)) { newSet.delete(categoryId) } else { newSet.add(categoryId) } return newSet }) } const handleCategoryClick = (category: PartsIndexCategory | PartsAPICategory, group?: any) => { onCategorySelect(category, group) } const handleFetchProducts = async ( category: PartsIndexCategory | PartsAPICategory, group?: any, fetchAll: boolean = false ) => { const fetchKey = group ? `${category.id}_${group.id}` : category.id setFetchingCategories(prev => new Set(prev).add(fetchKey)) try { await fetchCategoryProducts({ variables: { input: { categoryId: category.id, categoryName: category.name, categoryType: type.toUpperCase(), groupId: group?.id, groupName: group?.name, fetchAll, limit: fetchAll ? 1000 : 100 } } }) } catch (error) { console.error('Fetch error:', error) } finally { setFetchingCategories(prev => { const newSet = new Set(prev) newSet.delete(fetchKey) return newSet }) } } if (!categories || categories.length === 0) { return (

Категории не найдены

) } if (type === 'partsindex') { const partsIndexCategories = categories as PartsIndexCategory[] return (
{partsIndexCategories.map((category) => (
{/* Заголовок категории */}
{category.image ? ( {category.name} ) : ( )}

{category.name}

{category.groups?.length || 0} групп
{/* Группы категории */} {expandedCategories.has(category.id) && category.groups && (
{category.groups.map((group) => (
{/* Подгруппы */} {group.subgroups && group.subgroups.length > 0 && (
{group.subgroups.slice(0, 3).map((subgroup) => ( ))} {group.subgroups.length > 3 && (
и ещё {group.subgroups.length - 3} подгрупп...
)}
)}
))}
)} {/* Кнопки действий */}
))}
) } // PartsAPI categories (tree structure) const partsAPICategories = categories as PartsAPICategory[] const renderPartsAPICategory = (category: PartsAPICategory, level: number = 0) => (
0 ? 'ml-4' : ''}`}>

{category.name}

Уровень {category.level} {category.children && category.children.length > 0 && ( {category.children.length} подкатегорий )}
{category.children && category.children.length > 0 && ( )}
{/* Подкатегории */} {expandedCategories.has(category.id) && category.children && (
{category.children.map((child) => renderPartsAPICategory(child, level + 1))}
)}
) return (
{partsAPICategories.map((category) => renderPartsAPICategory(category))}
) }