Download web/src/components/FileTree.tsx from SSSAssist/Thoth: direct link, hf CLI and curl.
- Browser
- Download file 5.49 kB
-
https://huggingface.co/spaces/SSSAssist/Thoth/resolve/main/web/src/components/FileTree.tsx
- Command line
-
hf download hf://spaces/SSSAssist/Thoth/web/src/components/FileTree.tsx
-
curl -L -o FileTree.tsx https://huggingface.co/spaces/SSSAssist/Thoth/resolve/main/web/src/components/FileTree.tsx
5.49 kB
| import { ChevronDown, ChevronRight, File, FileImage, FilePlus2, FileText, Folder, Pencil, Star, Trash2, Upload } from 'lucide-react'; | |
| import { type DragEvent, useMemo, useRef, useState } from 'react'; | |
| import type { FileInfo } from '../api'; | |
| interface Props { | |
| files: FileInfo[]; | |
| activePath: string | null; | |
| mainFile: string; | |
| canEdit: boolean; | |
| onOpen: (path: string) => void; | |
| onCreate: () => void; | |
| onUpload: (files: File[], dir: string) => void; | |
| onRename: (path: string) => void; | |
| onDelete: (path: string, isFolder: boolean) => void; | |
| onSetMain: (path: string) => void; | |
| } | |
| interface Node { | |
| name: string; | |
| path: string; | |
| file?: FileInfo; | |
| children: Node[]; | |
| } | |
| function buildTree(files: FileInfo[]): Node[] { | |
| const root: Node = { name: '', path: '', children: [] }; | |
| for (const file of files) { | |
| let node = root; | |
| const parts = file.path.split('/'); | |
| parts.forEach((name, i) => { | |
| const path = parts.slice(0, i + 1).join('/'); | |
| let child = node.children.find((c) => c.name === name); | |
| if (!child) { | |
| child = { name, path, children: [] }; | |
| node.children.push(child); | |
| } | |
| if (i === parts.length - 1) child.file = file; | |
| node = child; | |
| }); | |
| } | |
| const sort = (nodes: Node[]) => { | |
| // Önce klasörler, sonra dosyalar; her biri Türkçe alfabetik sırada. | |
| nodes.sort((a, b) => Number(Boolean(a.file)) - Number(Boolean(b.file)) || a.name.localeCompare(b.name, 'tr')); | |
| nodes.forEach((n) => sort(n.children)); | |
| return nodes; | |
| }; | |
| return sort(root.children); | |
| } | |
| const IMAGE = /\.(png|jpe?g|gif|svg|webp|pdf|eps)$/i; | |
| export function FileTree(props: Props) { | |
| const { files, activePath, mainFile, canEdit } = props; | |
| const tree = useMemo(() => buildTree(files), [files]); | |
| const [collapsed, setCollapsed] = useState<Set<string>>(new Set()); | |
| const [dragOver, setDragOver] = useState(false); | |
| const uploadInput = useRef<HTMLInputElement>(null); | |
| const toggle = (path: string) => | |
| setCollapsed((prev) => { | |
| const next = new Set(prev); | |
| if (!next.delete(path)) next.add(path); | |
| return next; | |
| }); | |
| function onDrop(e: DragEvent) { | |
| e.preventDefault(); | |
| setDragOver(false); | |
| if (canEdit && e.dataTransfer.files.length) props.onUpload([...e.dataTransfer.files], ''); | |
| } | |
| function renderNode(node: Node, depth: number) { | |
| const indent = { paddingLeft: 10 + depth * 14 }; | |
| if (!node.file) { | |
| const open = !collapsed.has(node.path); | |
| return ( | |
| <li key={node.path}> | |
| <div className="tree-row" style={indent} onClick={() => toggle(node.path)}> | |
| {open ? <ChevronDown size={14} /> : <ChevronRight size={14} />} | |
| <Folder size={15} /> | |
| <span className="tree-name">{node.name}</span> | |
| {canEdit && ( | |
| <span className="tree-actions" onClick={(e) => e.stopPropagation()}> | |
| <button title="Yeniden adlandır" onClick={() => props.onRename(node.path)}><Pencil size={13} /></button> | |
| <button title="Klasörü sil" onClick={() => props.onDelete(node.path, true)}><Trash2 size={13} /></button> | |
| </span> | |
| )} | |
| </div> | |
| {open && <ul>{node.children.map((c) => renderNode(c, depth + 1))}</ul>} | |
| </li> | |
| ); | |
| } | |
| const isMain = node.path === mainFile; | |
| const Icon = IMAGE.test(node.name) ? FileImage : /\.(tex|bib|sty|cls)$/i.test(node.name) ? FileText : File; | |
| return ( | |
| <li key={node.path}> | |
| <div | |
| className={`tree-row${node.path === activePath ? ' active' : ''}`} style={{ paddingLeft: indent.paddingLeft + 14 }} | |
| onClick={() => props.onOpen(node.path)} title={node.path} | |
| > | |
| <Icon size={15} /> | |
| <span className="tree-name">{node.name}</span> | |
| {isMain && <span title="Ana dosya (derlenen dosya)"><Star size={12} className="main-star" /></span>} | |
| {canEdit && ( | |
| <span className="tree-actions" onClick={(e) => e.stopPropagation()}> | |
| {!isMain && node.name.endsWith('.tex') && ( | |
| <button title="Ana dosya yap" onClick={() => props.onSetMain(node.path)}><Star size={13} /></button> | |
| )} | |
| <button title="Yeniden adlandır" onClick={() => props.onRename(node.path)}><Pencil size={13} /></button> | |
| {!isMain && <button title="Sil" onClick={() => props.onDelete(node.path, false)}><Trash2 size={13} /></button>} | |
| </span> | |
| )} | |
| </div> | |
| </li> | |
| ); | |
| } | |
| return ( | |
| <div | |
| className={`file-tree${dragOver ? ' drag-over' : ''}`} | |
| onDragOver={(e) => { if (canEdit) { e.preventDefault(); setDragOver(true); } }} | |
| onDragLeave={() => setDragOver(false)} onDrop={onDrop} | |
| > | |
| {canEdit && ( | |
| <div className="tree-toolbar"> | |
| <button className="btn btn-small" onClick={props.onCreate}><FilePlus2 size={14} /> Yeni dosya</button> | |
| <button className="btn btn-small" onClick={() => uploadInput.current?.click()}><Upload size={14} /> Yükle</button> | |
| <input | |
| ref={uploadInput} type="file" multiple hidden | |
| onChange={(e) => { const list = [...(e.target.files ?? [])]; e.target.value = ''; if (list.length) props.onUpload(list, ''); }} | |
| /> | |
| </div> | |
| )} | |
| <ul className="tree-root">{tree.map((n) => renderNode(n, 0))}</ul> | |
| {canEdit && <p className="tree-hint">Görsel ve dosyaları buraya sürükleyip bırakabilirsiniz.</p>} | |
| </div> | |
| ); | |
| } | |