import React, { useEffect, useMemo, useRef, useState } from 'react'; import { Check, ChevronDown, CornerLeftUp, Folder, FolderInput, FolderOpen, FolderPlus, Home, Search, X, } from 'lucide-react'; import { ImageFolder } from '../types'; import { ImageFolderRelation, RankedImageFolderOption, getImageFolderPathLabel, getRankedImageMoveFolders, sanitizeImageFolderName, } from '../utils/imageStorage'; import { FloatingPopover } from './FloatingPopover'; interface ImageFolderPickerProps { id?: string; datasetId: string; folders: ImageFolder[]; currentFolderId: string | null; referenceFolderId?: string | null; onSelectFolder: (folderId: string | null) => void; onCreateFolder?: (name: string, parentId: string | null) => ImageFolder | null; placeholderLabel?: string; isMixedCurrent?: boolean; excludeSubtreeOfFolderId?: string | null; excludeFolderIds?: Set | null; align?: 'left' | 'right'; compact?: boolean; } const RELATION_BADGE: Record = { current: 'Текущая', child: 'Подпапка', parent: 'Уровнем выше', sibling: 'На уровне', root: 'Корень', }; export const ImageFolderPicker: React.FC = ({ id, datasetId, folders, currentFolderId, referenceFolderId, onSelectFolder, onCreateFolder, placeholderLabel, isMixedCurrent = false, excludeSubtreeOfFolderId = null, excludeFolderIds = null, align = 'left', compact = false, }) => { const [isOpen, setIsOpen] = useState(false); const [searchQuery, setSearchQuery] = useState(''); const [isCreating, setIsCreating] = useState(false); const [newFolderName, setNewFolderName] = useState(''); const [createError, setCreateError] = useState(null); const triggerRef = useRef(null); const searchInputRef = useRef(null); const newFolderInputRef = useRef(null); const effectiveRefId = referenceFolderId !== undefined ? referenceFolderId : currentFolderId; useEffect(() => { if (isOpen) { setTimeout(() => searchInputRef.current?.focus(), 0); } else { setSearchQuery(''); setIsCreating(false); setNewFolderName(''); setCreateError(null); } }, [isOpen]); useEffect(() => { if (isCreating) { setTimeout(() => newFolderInputRef.current?.focus(), 0); } }, [isCreating]); const { nearby, other } = useMemo( () => getRankedImageMoveFolders( folders, datasetId, effectiveRefId ?? null, searchQuery, excludeSubtreeOfFolderId, excludeFolderIds ), [ folders, datasetId, effectiveRefId, searchQuery, excludeSubtreeOfFolderId, excludeFolderIds, ] ); const triggerLabel = useMemo(() => { if (placeholderLabel) return placeholderLabel; if (isMixedCurrent) return 'Разные папки (выбрать...)'; return getImageFolderPathLabel(folders, currentFolderId, 'images/ (корень)'); }, [folders, currentFolderId, placeholderLabel, isMixedCurrent]); const handleCreateSubmit = () => { if (!onCreateFolder) return; const cleaned = sanitizeImageFolderName(newFolderName); if (!cleaned) { setCreateError('Введите допустимое имя папки'); return; } const created = onCreateFolder(cleaned, currentFolderId || null); if (!created) { setCreateError('Папка с таким именем уже есть на этом уровне'); return; } setCreateError(null); setNewFolderName(''); setIsCreating(false); onSelectFolder(created.id); setIsOpen(false); }; const renderOption = (opt: RankedImageFolderOption) => { const isSelected = !isMixedCurrent && (currentFolderId || null) === opt.id; const isRoot = opt.id === null; const relationLabel = opt.relation ? RELATION_BADGE[opt.relation] : null; return ( ); }; return (
setIsOpen(false)} triggerRef={triggerRef} align={align} minWidth={290} maxWidth={350} > {/* Search Input */}
setSearchQuery(e.target.value)} placeholder="Поиск папки в images/..." className="w-full pl-8 pr-7 py-1.5 text-xs bg-white dark:bg-stone-900 border border-stone-300 dark:border-stone-700 rounded-lg text-stone-900 dark:text-stone-100 placeholder-stone-400 dark:placeholder-stone-500 focus:border-amber-500 focus:outline-hidden" /> {searchQuery && ( )}
{onCreateFolder && (
{!isCreating ? ( ) : (
{ setNewFolderName(e.target.value); setCreateError(null); }} onKeyDown={(e) => { if (e.key === 'Enter') { e.preventDefault(); e.stopPropagation(); handleCreateSubmit(); } else if (e.key === 'Escape') { e.stopPropagation(); setIsCreating(false); } }} placeholder="Имя новой папки..." className="flex-1 min-w-0 px-2 py-1 text-xs font-mono rounded border border-stone-300 dark:border-stone-700 bg-stone-50 dark:bg-stone-950 text-stone-900 dark:text-stone-100 focus:border-amber-500 focus:outline-hidden" />
{createError && (
{createError}
)}
)}
)}
{/* Ranked Folder Sections */}
{nearby.length === 0 && other.length === 0 ? (
Папки по запросу «{searchQuery}» не найдены.
) : ( <> {nearby.length > 0 && (
Доступные рядом
{nearby.map(renderOption)}
)} {other.length > 0 && (
Все остальные папки
{other.map(renderOption)}
)} )}
); };