'use client' import { useState, useEffect, useRef } from 'react' import { Product, ProductModule } from '@/lib/types' import { Plus, Search, Package, Layers } from 'lucide-react' interface DependencyPopoverProps { currentProductId: string assignedIds: string[] allProducts: Product[] allModules: ProductModule[] onAddDependency: (id: string) => void } export function DependencyPopover({ currentProductId, assignedIds, allProducts, allModules, onAddDependency }: DependencyPopoverProps) { const [isOpen, setIsOpen] = useState(false) const [search, setSearch] = useState('') const popoverRef = useRef(null) const inputRef = useRef(null) // Filter out the current product, and items already assigned const otherProducts = allProducts.filter(p => p.id !== currentProductId && !assignedIds.includes(p.id)) const otherModules = allModules.filter(m => m.product_id !== currentProductId && !assignedIds.includes(m.id)) // Apply search const filteredProducts = otherProducts.filter(p => p.name.toLowerCase().includes(search.toLowerCase())) const filteredModules = otherModules.filter(m => m.name.toLowerCase().includes(search.toLowerCase())) useEffect(() => { if (isOpen && inputRef.current) { inputRef.current.focus() } }, [isOpen]) useEffect(() => { function handleClickOutside(event: MouseEvent) { if (popoverRef.current && !popoverRef.current.contains(event.target as Node)) { setIsOpen(false) } } if (isOpen) { document.addEventListener('mousedown', handleClickOutside) } return () => { document.removeEventListener('mousedown', handleClickOutside) } }, [isOpen]) return (
{isOpen && (
setSearch(e.target.value)} className="w-full bg-white/5 border border-white/10 rounded-lg pl-9 pr-3 py-1.5 text-xs text-white placeholder-slate-400 outline-none focus:border-primary/50 transition-colors" />
{filteredProducts.length > 0 && (
Produkte {filteredProducts.map(p => ( ))}
)} {filteredModules.length > 0 && (
Module {filteredModules.map(m => ( ))}
)} {filteredProducts.length === 0 && filteredModules.length === 0 && (
Keine Ergebnisse gefunden.
)}
)}
) }