import React, { useState, useMemo } from 'react'; import { Button } from './Button'; import { FolderPlus, Folder, X, Search, MoveUp } from 'lucide-react'; import { DataItem } from '../types'; interface MoveToFolderModalProps { isOpen: boolean; onClose: () => void; selectionCount: number; folders: DataItem[]; currentFolderId: string | null; onMoveToFolder: (folderId: string | null) => Promise; onCreateFolder: (name: string) => Promise; } export const MoveToFolderModal: React.FC = ({ isOpen, onClose, selectionCount, folders, currentFolderId, onMoveToFolder, onCreateFolder, }) => { const [searchQuery, setSearchQuery] = useState(''); const [newFolderName, setNewFolderName] = useState(''); const [isProcessing, setIsProcessing] = useState(false); // Filter folders to not include the current one if we want to prevent moving to same folder // Actually, we can move to any folder. // Construir mapa jerárquico recursivo de carpetas o filtrar de forma plana si hay búsqueda const filteredFolders = useMemo(() => { if (searchQuery.trim() !== '') { return folders .filter(f => f.title.toLowerCase().includes(searchQuery.toLowerCase())) .map(f => ({ folder: f, depth: 0 })); } const buildTree = (parentId: string | null = null, depth: number = 0): { folder: DataItem, depth: number }[] => { const children = folders.filter(f => (f.parentId || null) === parentId); children.sort((a, b) => a.title.localeCompare(b.title)); let result: { folder: DataItem, depth: number }[] = []; for (const child of children) { result.push({ folder: child, depth }); result = result.concat(buildTree(child.id, depth + 1)); } return result; }; return buildTree(null, 0); }, [folders, searchQuery]); if (!isOpen) return null; const handleCreateAndMove = async () => { if (!newFolderName.trim() || isProcessing) return; setIsProcessing(true); try { const newFolderId = await onCreateFolder(newFolderName.trim()); await onMoveToFolder(newFolderId); onClose(); } catch(e) { console.error(e); alert("Error al crear carpeta"); } finally { setIsProcessing(false); } }; const handleMove = async (folderId: string | null) => { if (isProcessing) return; setIsProcessing(true); try { await onMoveToFolder(folderId); onClose(); } catch(e) { console.error(e); alert("Error al mover"); } finally { setIsProcessing(false); } }; return ( <>
{/* Header */}

Mover a...

{selectionCount} elementos seleccionados

{/* Content */}
{/* Create new folder inline */}

Nueva Carpeta

setNewFolderName(e.target.value)} onKeyDown={(e) => { if (e.key === 'Enter') { e.preventDefault(); handleCreateAndMove(); } }} className="flex-1 bg-[#121212] border border-white/10 focus:border-primary rounded-lg px-3 py-2 text-sm outline-none transition-colors" disabled={isProcessing} />
{/* Search folders */}
setSearchQuery(e.target.value)} className="w-full bg-[#121212] border border-white/10 focus:border-primary rounded-lg py-2.5 pl-10 pr-4 text-sm outline-none transition-colors" />
{/* Folder List */}
{currentFolderId !== null && !searchQuery && ( )} {filteredFolders.length === 0 ? (

No se encontraron carpetas.

) : ( filteredFolders.map(({ folder, depth }) => ( )) )}
); };