import React, { useState } from "react"; import { GripVertical, X, Plus, ArrowDown, ArrowUp, Settings2, } from "lucide-react"; import { SearchableSelect } from "../SearchableSelect"; import { FeatureConfigPopover } from "./FeatureConfigPopover"; import { Feature } from "../../types/dataset"; interface FeaturePickerProps { options: Feature[]; initialFeatures?: Feature[]; onFeaturesChange: (features: Feature[]) => void; } export function FeaturePicker({ options, initialFeatures = [], onFeaturesChange, }: FeaturePickerProps) { const [selectedFeatures, setSelectedFeatures] = useState(initialFeatures); const [draggedIndex, setDraggedIndex] = useState(null); console.log(selectedFeatures); const availableFeatures = options.filter( (feature) => !selectedFeatures.find((t) => t.name === feature.name) ); const updateFeatures = (newFeatures: Feature[]) => { const featuresWithPosition = newFeatures.map((feature, index) => ({ ...feature, feature_position: index, })); setSelectedFeatures(featuresWithPosition); onFeaturesChange(featuresWithPosition); }; const handleAddFeature = (transformName: string) => { const feature = options.find((t) => t.name === transformName); if (feature) { const newFeature = { ...feature, feature_position: selectedFeatures.length, }; updateFeatures([...selectedFeatures, newFeature]); } }; const handleRemove = (index: number) => { const newFeatures = [...selectedFeatures]; newFeatures.splice(index, 1); updateFeatures(newFeatures); }; const handleMoveUp = (index: number) => { if (index === 0) return; const newFeatures = [...selectedFeatures]; [newFeatures[index - 1], newFeatures[index]] = [ newFeatures[index], newFeatures[index - 1], ]; updateFeatures(newFeatures); }; const handleMoveDown = (index: number) => { if (index === selectedFeatures.length - 1) return; const newFeatures = [...selectedFeatures]; [newFeatures[index], newFeatures[index + 1]] = [ newFeatures[index + 1], newFeatures[index], ]; updateFeatures(newFeatures); }; const handleDragStart = (e: React.DragEvent, index: number) => { setDraggedIndex(index); }; const handleDragOver = (e: React.DragEvent, index: number) => { e.preventDefault(); if (draggedIndex === null || draggedIndex === index) return; const newFeatures = [...selectedFeatures]; const [draggedFeature] = newFeatures.splice(draggedIndex, 1); newFeatures.splice(index, 0, draggedFeature); updateFeatures(newFeatures); setDraggedIndex(index); }; const handleDragEnd = () => { setDraggedIndex(null); }; return (
{/* Add Feature */}
({ value: feature.name, label: feature.name, description: feature.description, }))} value="" onChange={(value) => handleAddFeature(value as string)} placeholder="Add a transform..." />
{/* Selected Features */}
{selectedFeatures.map((feature, index) => (
handleDragStart(e, index)} onDragOver={(e) => handleDragOver(e, index)} onDragEnd={handleDragEnd} className={`flex items-center gap-3 p-3 bg-white border rounded-lg ${ draggedIndex === index ? "border-blue-500 shadow-lg" : "border-gray-200" } ${draggedIndex !== null ? "cursor-grabbing" : ""}`} >
{feature.name} {"feature"}

{feature.description}

))} {selectedFeatures.length === 0 && (

Add features to enrich your dataset

)}
); }