import React, { useState, useRef } from 'react'; import type { BlockLibraryProps } from '../types/ui.types'; import type { BlockDefinition } from '../types/cad.types'; const categories = ['Alle', 'Bestuhlung', 'Tische', 'Bühne', 'Architektur', 'Custom']; const BlockLibrary: React.FC = ({ blocks, category, onCategoryChange, onSearch, onDragBlock, onRenameBlock, onDuplicateBlock, onDeleteBlock, onSvgImport, onSaveGroupAsBlock }) => { const [searchQuery, setSearchQuery] = useState(''); const [expandedCats, setExpandedCats] = useState>(new Set(['Bestuhlung'])); const fileInputRef = useRef(null); const handleSearch = (e: React.ChangeEvent) => { const q = e.target.value; setSearchQuery(q); onSearch(q); }; const filteredBlocks = blocks.filter(b => { const catMatch = category === 'Alle' || b.category === category; const q = searchQuery.toLowerCase().trim(); const searchMatch = !q || b.name.toLowerCase().includes(q) || b.description.toLowerCase().includes(q); return catMatch && searchMatch; }); // Group by category const grouped: Record = {}; for (const b of filteredBlocks) { if (!grouped[b.category]) grouped[b.category] = []; grouped[b.category].push(b); } const toggleCat = (cat: string) => { setExpandedCats((prev) => { const next = new Set(prev); if (next.has(cat)) next.delete(cat); else next.add(cat); return next; }); }; const handleDragStart = (e: React.DragEvent, blockId: string) => { e.dataTransfer.setData('text/block-id', blockId); e.dataTransfer.effectAllowed = 'copy'; onDragBlock(blockId); }; const handleSvgImport = (e: React.ChangeEvent) => { const file = e.target.files?.[0]; if (!file) return; const reader = new FileReader(); reader.onload = (ev) => { const svgContent = ev.target?.result as string; if (onSvgImport) { onSvgImport(svgContent, file.name.replace(/\.svg$/i, ''), 'Custom'); } else { window.dispatchEvent(new CustomEvent('block-svg-import', { detail: { svg: svgContent, name: file.name.replace(/\.svg$/i, ''), category: 'Custom' } })); } }; reader.readAsText(file); e.target.value = ''; }; return ( <>
{categories.map((cat) => ( ))}
{onSaveGroupAsBlock && ( )}
{Object.entries(grouped).map(([cat, catBlocks]) => (
toggleCat(cat)}> {expandedCats.has(cat) ? '▾' : '▸'} 📁 {cat} {catBlocks.length}
{expandedCats.has(cat) && (
{catBlocks.map((block) => (
handleDragStart(e, block.id)} title={block.description} > 📦 {block.name} e.stopPropagation()}> {onRenameBlock && ( )} {onDuplicateBlock && ( )} {onDeleteBlock && ( )}
))}
)}
))} {filteredBlocks.length === 0 && (
Keine Blöcke gefunden
)}
); }; export default BlockLibrary;