import React, { useEffect, useMemo, useRef, useState } from 'react'; import { Check, ChevronDown, CornerLeftUp, Folder, FolderInput, FolderOpen, Home, Search, X, } from 'lucide-react'; import { GenericFolderNode, GenericFolderRelation, RankedGenericFolderOption, getGenericFolderPathLabel, getRankedGenericMoveFolders, } from '../utils/folders'; import { FloatingPopover } from './FloatingPopover'; interface FolderMovePickerProps { id?: string; folders: GenericFolderNode[]; currentFolderId: string | null; referenceFolderId: string | null; onSelectFolder: (folderId: string | null) => void; placeholderLabel?: string; isMixedCurrent?: boolean; rootLabel?: string; excludeFolderIds?: Set | null; align?: 'left' | 'right'; compact?: boolean; iconOnly?: boolean; triggerTitle?: string; } const RELATION_BADGE: Record = { current: 'Текущая', child: 'Подпапка', parent: 'Уровнем выше', sibling: 'На уровне', root: 'Корень', }; export const FolderMovePicker: React.FC = ({ id, folders, currentFolderId, referenceFolderId, onSelectFolder, placeholderLabel, isMixedCurrent = false, rootLabel = 'Мои теги', excludeFolderIds = null, align = 'left', compact = false, iconOnly = false, triggerTitle, }) => { const [isOpen, setIsOpen] = useState(false); const [searchQuery, setSearchQuery] = useState(''); const triggerRef = useRef(null); const searchInputRef = useRef(null); useEffect(() => { if (isOpen) { setTimeout(() => searchInputRef.current?.focus(), 0); } else { setSearchQuery(''); } }, [isOpen]); const { nearby, other } = useMemo( () => getRankedGenericMoveFolders(folders, referenceFolderId, { rootLabel, query: searchQuery, excludeFolderIds, }), [folders, referenceFolderId, rootLabel, searchQuery, excludeFolderIds] ); const triggerLabel = useMemo(() => { if (placeholderLabel) return placeholderLabel; if (isMixedCurrent) return 'Разные папки (выбрать...)'; return getGenericFolderPathLabel( folders, currentFolderId, `${rootLabel} (корень)` ); }, [folders, currentFolderId, placeholderLabel, isMixedCurrent, rootLabel]); const renderOption = (opt: RankedGenericFolderOption) => { const isSelected = !isMixedCurrent && !excludeFolderIds?.size && (currentFolderId || null) === opt.id; const isRoot = opt.id === null; const relationLabel = opt.relation ? RELATION_BADGE[opt.relation] : null; return ( ); }; return (
{iconOnly ? ( ) : ( )} setIsOpen(false)} triggerRef={triggerRef} align={align} minWidth={280} maxWidth={360} matchTriggerWidth={!iconOnly} > {/* Search Input */}
setSearchQuery(e.target.value)} placeholder="Поиск папки по названию или пути..." 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 && ( )}
{/* Ranked Folder Sections */}
{nearby.length === 0 && other.length === 0 ? (
Папки по запросу «{searchQuery}» не найдены.
) : ( <> {nearby.length > 0 && (
Доступные рядом
{nearby.map(renderOption)}
)} {other.length > 0 && (
Все остальные папки
{other.map(renderOption)}
)} )}
); };