31 lines
3.8 KiB
TypeScript
31 lines
3.8 KiB
TypeScript
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<Listing | null>(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<Listing>('/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 <section className="directory-picker" aria-label="本机文件夹选择">
|
||
<div className="directory-location"><button type="button" className="icon-button" aria-label="上一级文件夹" disabled={busy || !listing?.parent} onClick={() => void browse(listing!.parent!)}><ArrowUp size={16} /></button><input aria-label="浏览文件夹路径" disabled={busy} value={location} onChange={event => setLocation(event.target.value)} onKeyDown={event => { if (event.key === 'Enter') { event.preventDefault(); void browse(location); } }} /><button type="button" className="button secondary" disabled={busy} onClick={() => void browse(location)}>前往</button><button type="button" className="icon-button" aria-label="刷新文件夹" disabled={busy} onClick={() => void browse(listing?.current || location)}><RotateCw size={15} /></button></div>
|
||
{listing && <nav className="directory-bookmarks" aria-label="常用位置">{listing.bookmarks.map(item => <button type="button" key={item.path} disabled={busy} onClick={() => void browse(item.path)}>{item.name}</button>)}</nav>}
|
||
{error && <p className="local-files-error" role="alert">{error}</p>}
|
||
<input className="directory-filter" aria-label="筛选子文件夹" placeholder="筛选当前目录下的文件夹" value={filter} onChange={event => setFilter(event.target.value)} />
|
||
<div className="directory-list" aria-busy={busy}>{busy ? <p role="status"><LoaderCircle className="spin" size={16} />正在读取文件夹…</p> : listing?.directories.filter(item => item.name.toLocaleLowerCase().includes(filter.toLocaleLowerCase())).map(item => <button type="button" key={item.path} onClick={() => void browse(item.path)}><Folder size={17} /><span>{item.name}</span></button>)}{!busy && listing && !listing.directories.filter(item => item.name.toLocaleLowerCase().includes(filter.toLocaleLowerCase())).length && <p>{filter ? '没有匹配的子文件夹。' : '此文件夹没有子文件夹,可以直接选择。'}</p>}</div>
|
||
{listing?.truncated && <p className="subtle">此处文件较多,已显示部分文件夹;可在上方输入完整路径。</p>}
|
||
<div className="directory-selection"><span title={listing?.current}>已定位:{listing?.current || '尚未选择'}</span><div><button type="button" className="button secondary" onClick={onCancel}>取消选择</button><button type="button" className="button primary" disabled={busy || !!error || !listing} onClick={() => onChoose(listing!.current)}>使用此文件夹</button></div></div>
|
||
</section>;
|
||
}
|