import { useEffect, useRef, useState } from 'react'; import { ArrowUp, Folder, LoaderCircle, RotateCw } from 'lucide-react'; import { api } from './api'; import './LocalFiles.css'; type Directory = { name: string; path: string }; type Listing = { current: string; parent: string | null; directories: Directory[]; bookmarks: Directory[]; truncated: boolean }; export function ProjectDirectoryPicker({ initialPath, onChoose, onCancel }: { initialPath: string; onChoose(path: string): void; onCancel(): void }) { const [listing, setListing] = useState(null), [busy, setBusy] = useState(true), [error, setError] = useState(''); const [location, setLocation] = useState(initialPath), [filter, setFilter] = useState(''); const request = useRef(0); async function browse(path: string) { const sequence = ++request.current; setBusy(true); setError(''); try { const value = await api('/local-directories?path=' + encodeURIComponent(path)); if (sequence === request.current) { setListing(value); setLocation(value.current); setFilter(''); } } catch (cause) { if (sequence === request.current) setError(cause instanceof Error ? cause.message : '读取文件夹失败。'); } finally { if (sequence === request.current) setBusy(false); } } useEffect(() => { void browse(initialPath); return () => { request.current++; }; }, []); return
setLocation(event.target.value)} onKeyDown={event => { if (event.key === 'Enter') { event.preventDefault(); void browse(location); } }} />
{listing && } {error &&

{error}

} setFilter(event.target.value)} />
{busy ?

正在读取文件夹…

: listing?.directories.filter(item => item.name.toLocaleLowerCase().includes(filter.toLocaleLowerCase())).map(item => )}{!busy && listing && !listing.directories.filter(item => item.name.toLocaleLowerCase().includes(filter.toLocaleLowerCase())).length &&

{filter ? '没有匹配的子文件夹。' : '此文件夹没有子文件夹,可以直接选择。'}

}
{listing?.truncated &&

此处文件较多,已显示部分文件夹;可在上方输入完整路径。

}
已定位:{listing?.current || '尚未选择'}
; }