import React, { useMemo, useState } from "react"; import { FaSearch, FaSquare } from 'react-icons/fa'; import { ComponentDefinition, HookInfo, PropertyInfo } from "../../proxy/developerKit/componentInfo"; import { getAllComponentDefinitions } from "./data/componentDefinitions"; import navigationIcon from "@/proxy/menus/navigation-icon.config"; interface ComponentLibraryProps { onDragStart: (componentDef: ComponentDefinition, e: React.DragEvent) => void; } export const ComponentLibrary: React.FC = ({ onDragStart, }) => { const [searchTerm, setSearchTerm] = useState(""); const handleDragStart = ( componentDef: ComponentDefinition, e: React.DragEvent ) => { e.stopPropagation(); try { const data = JSON.stringify(componentDef); e.dataTransfer.setData("text/plain", data); e.dataTransfer.setData("application/json", data); } catch (error) { console.error("Error setting drag data:", error); } e.dataTransfer.effectAllowed = "copy"; if (e.dataTransfer.setDragImage) { try { const dragPreview = document.createElement("div"); dragPreview.className = "component-drag-preview"; dragPreview.innerHTML = `
${componentDef.icon || "📦"} ${componentDef.name}
`; Object.assign(dragPreview.style, { position: "absolute", top: "-1000px", left: "0", padding: "8px 12px", backgroundColor: "white", border: "2px solid #4f46e5", borderRadius: "4px", boxShadow: "0 2px 5px rgba(0,0,0,0.2)", zIndex: "9999", pointerEvents: "none", }); document.body.appendChild(dragPreview); e.dataTransfer.setDragImage(dragPreview, 20, 20); setTimeout(() => document.body.removeChild(dragPreview), 0); } catch (error) { console.error("Error setting drag image:", error); } } onDragStart(componentDef, e); }; const filteredComponents = useMemo( () => getAllComponentDefinitions().filter( (comp) => comp.name.toLowerCase().includes(searchTerm.toLowerCase()) || comp.description.toLowerCase().includes(searchTerm.toLowerCase()) ), [searchTerm] ); const categories = [ "basic", "form", "layout", "feedback", "media", "interactive", "navigation", "data", ] .map((cat: any) => ({ id: cat, name: cat, components: filteredComponents?.filter((c) => c.category === cat), })) .filter((cat) => cat.components.length > 0); const getIcon = (iconName: string): React.ComponentType => { return navigationIcon[iconName] || FaSquare; }; return (
{/* Arama kutusu */}
setSearchTerm(e.target.value)} className="w-full pl-10 pr-4 py-2 bg-gray-800 border border-gray-600 rounded-lg text-sm text-white placeholder-gray-400 focus:outline-none focus:ring-2 focus:ring-blue-500" />
{/* Bileşen kategorileri */}
{categories.map((category) => (

{category.name}

{category.components.map((componentDef) => { const IconComponent = getIcon(componentDef.icon); return (
handleDragStart(componentDef, e)} onDragEnd={(e) => { e.preventDefault(); e.stopPropagation(); }} >
{componentDef.name}

{componentDef.description}

{/* Özellikler */} {componentDef.properties?.length > 0 && (
{componentDef.properties.slice(0, 2).map((prop: PropertyInfo) => ( {prop.name} ))} {componentDef.properties.length > 2 && ( ... )}
)} {/* Hooklar */} {componentDef.hooks?.length > 0 && (
{componentDef.hooks.slice(0, 2).map((hook: HookInfo) => ( {hook.name} ))} {componentDef.hooks.length > 2 && ( ... )}
)}
); })}
))}
); };