Spaces:
Running
Running
Download src/components/FolderMovePicker.tsx from zelk12/ShareGPT_Dialogue_Studio: direct link, hf CLI and curl.
- Browser
- Download file 9.43 kB
-
https://huggingface.co/spaces/zelk12/ShareGPT_Dialogue_Studio/resolve/main/src/components/FolderMovePicker.tsx
- Command line
-
hf download hf://spaces/zelk12/ShareGPT_Dialogue_Studio/src/components/FolderMovePicker.tsx
-
curl -L -o FolderMovePicker.tsx https://huggingface.co/spaces/zelk12/ShareGPT_Dialogue_Studio/resolve/main/src/components/FolderMovePicker.tsx
9.43 kB
| 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<string> | null; | |
| align?: 'left' | 'right'; | |
| compact?: boolean; | |
| iconOnly?: boolean; | |
| triggerTitle?: string; | |
| } | |
| const RELATION_BADGE: Record<GenericFolderRelation, string> = { | |
| current: 'Текущая', | |
| child: 'Подпапка', | |
| parent: 'Уровнем выше', | |
| sibling: 'На уровне', | |
| root: 'Корень', | |
| }; | |
| export const FolderMovePicker: React.FC<FolderMovePickerProps> = ({ | |
| 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<HTMLButtonElement>(null); | |
| const searchInputRef = useRef<HTMLInputElement>(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 ( | |
| <button | |
| key={opt.id ?? '__root__'} | |
| type="button" | |
| onClick={(e) => { | |
| e.stopPropagation(); | |
| onSelectFolder(opt.id); | |
| setIsOpen(false); | |
| }} | |
| className={`w-full text-left px-2.5 py-2 rounded-lg flex items-center justify-between gap-2 transition-colors ${ | |
| isSelected | |
| ? 'bg-amber-50 dark:bg-amber-950/50 text-amber-950 dark:text-amber-200 border border-amber-300/80 dark:border-amber-700/70' | |
| : 'hover:bg-stone-100 dark:hover:bg-stone-800/80 text-stone-800 dark:text-stone-200 border border-transparent' | |
| }`} | |
| > | |
| <div className="flex items-start space-x-2 min-w-0"> | |
| {isRoot ? ( | |
| <Home className="w-3.5 h-3.5 text-amber-600 dark:text-amber-400 shrink-0 mt-0.5" /> | |
| ) : opt.relation === 'parent' ? ( | |
| <CornerLeftUp className="w-3.5 h-3.5 text-stone-500 dark:text-stone-400 shrink-0 mt-0.5" /> | |
| ) : opt.relation === 'child' ? ( | |
| <FolderOpen className="w-3.5 h-3.5 text-amber-500 shrink-0 mt-0.5" /> | |
| ) : ( | |
| <Folder className="w-3.5 h-3.5 text-stone-400 dark:text-stone-500 shrink-0 mt-0.5" /> | |
| )} | |
| <div className="min-w-0"> | |
| <div className="text-xs font-semibold truncate">{opt.name}</div> | |
| {!isRoot && opt.pathLabel !== `${rootLabel} / ${opt.name}` && ( | |
| <div className="text-[10px] text-stone-500 dark:text-stone-400 truncate"> | |
| {opt.pathLabel} | |
| </div> | |
| )} | |
| </div> | |
| </div> | |
| <div className="flex items-center space-x-1.5 shrink-0"> | |
| {relationLabel && ( | |
| <span className="text-[10px] font-medium px-1.5 py-0.5 rounded bg-stone-100 dark:bg-stone-800 text-stone-600 dark:text-stone-300 border border-stone-200/80 dark:border-stone-700 whitespace-nowrap"> | |
| {relationLabel} | |
| </span> | |
| )} | |
| {isSelected && ( | |
| <Check className="w-3.5 h-3.5 text-amber-600 dark:text-amber-400 shrink-0" /> | |
| )} | |
| </div> | |
| </button> | |
| ); | |
| }; | |
| return ( | |
| <div className="relative"> | |
| {iconOnly ? ( | |
| <button | |
| ref={triggerRef} | |
| id={id} | |
| type="button" | |
| onClick={(e) => { | |
| e.stopPropagation(); | |
| setIsOpen((v) => !v); | |
| }} | |
| className={`p-1 rounded transition-colors ${ | |
| isOpen | |
| ? 'bg-amber-100 dark:bg-amber-950/60 text-amber-700 dark:text-amber-300' | |
| : 'text-stone-400 hover:text-stone-700 dark:hover:text-stone-200 hover:bg-stone-100 dark:hover:bg-stone-800' | |
| }`} | |
| title={triggerTitle || triggerLabel} | |
| > | |
| <FolderInput className="w-3.5 h-3.5" /> | |
| </button> | |
| ) : ( | |
| <button | |
| ref={triggerRef} | |
| id={id} | |
| type="button" | |
| onClick={(e) => { | |
| e.stopPropagation(); | |
| setIsOpen((v) => !v); | |
| }} | |
| className={`w-full ${ | |
| compact ? 'px-2 py-1 text-[11px]' : 'px-2.5 py-1.5 text-xs' | |
| } bg-stone-50 dark:bg-stone-950 border border-stone-200 dark:border-stone-700 rounded-lg text-stone-900 dark:text-stone-100 hover:border-amber-500 focus:border-amber-500 focus:outline-hidden flex items-center justify-between gap-2 transition-colors`} | |
| title={triggerTitle || triggerLabel} | |
| > | |
| <span className="flex items-center space-x-1.5 min-w-0"> | |
| <FolderInput className="w-3.5 h-3.5 text-amber-600 dark:text-amber-400 shrink-0" /> | |
| <span className="truncate font-medium">{triggerLabel}</span> | |
| </span> | |
| <ChevronDown | |
| className={`w-3.5 h-3.5 text-stone-400 shrink-0 transition-transform ${ | |
| isOpen ? 'rotate-180' : '' | |
| }`} | |
| /> | |
| </button> | |
| )} | |
| <FloatingPopover | |
| isOpen={isOpen} | |
| onClose={() => setIsOpen(false)} | |
| triggerRef={triggerRef} | |
| align={align} | |
| minWidth={280} | |
| maxWidth={360} | |
| matchTriggerWidth={!iconOnly} | |
| > | |
| {/* Search Input */} | |
| <div className="p-2 border-b border-stone-200 dark:border-stone-800 bg-stone-50/80 dark:bg-stone-950/70 shrink-0"> | |
| <div className="relative flex items-center"> | |
| <Search className="w-3.5 h-3.5 text-stone-400 absolute left-2.5 pointer-events-none" /> | |
| <input | |
| ref={searchInputRef} | |
| type="text" | |
| value={searchQuery} | |
| onChange={(e) => 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 && ( | |
| <button | |
| type="button" | |
| onClick={() => setSearchQuery('')} | |
| className="absolute right-2 p-0.5 text-stone-400 hover:text-stone-700 dark:hover:text-stone-200" | |
| title="Очистить поиск" | |
| > | |
| <X className="w-3 h-3" /> | |
| </button> | |
| )} | |
| </div> | |
| </div> | |
| {/* Ranked Folder Sections */} | |
| <div className="overflow-y-auto flex-1 min-h-0 p-1.5 space-y-2"> | |
| {nearby.length === 0 && other.length === 0 ? ( | |
| <div className="py-5 px-3 text-center text-xs text-stone-500 dark:text-stone-400"> | |
| Папки по запросу «{searchQuery}» не найдены. | |
| </div> | |
| ) : ( | |
| <> | |
| {nearby.length > 0 && ( | |
| <div className="space-y-0.5"> | |
| <div className="px-2 py-1 text-[10px] font-bold uppercase tracking-wider text-amber-700 dark:text-amber-400"> | |
| Доступные рядом | |
| </div> | |
| {nearby.map(renderOption)} | |
| </div> | |
| )} | |
| {other.length > 0 && ( | |
| <div className="space-y-0.5 pt-1 border-t border-stone-200/80 dark:border-stone-800"> | |
| <div className="px-2 py-1 text-[10px] font-bold uppercase tracking-wider text-stone-500 dark:text-stone-400"> | |
| Все остальные папки | |
| </div> | |
| {other.map(renderOption)} | |
| </div> | |
| )} | |
| </> | |
| )} | |
| </div> | |
| </FloatingPopover> | |
| </div> | |
| ); | |
| }; | |