DirectoryPickerDialog.tsx
7.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import { useCallback, useEffect, useState } from 'react';
import {
X,
ChevronRight,
CornerUpLeft,
FolderOpen,
Check,
Loader2,
} from 'lucide-react';
import { api } from '@/lib/api';
import type { FsListResult } from '@/lib/types';
import { joinPath } from './profile-presets';
interface Props {
open: boolean;
initialDir?: string;
onClose: () => void;
onPick: (absPath: string) => void;
}
/**
* 服务端文件系统导航弹窗。通过 GET /api/fs/list 逐层浏览,最终选中一个
* 真实绝对路径。无法用浏览器原生 <input webkitdirectory>(拿不到真实路径),
* 因此由服务端枚举目录。
*/
export function DirectoryPickerDialog({ open, initialDir, onClose, onPick }: Props) {
const [current, setCurrent] = useState<string | undefined>(initialDir);
const [data, setData] = useState<FsListResult | null>(null);
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
const [manual, setManual] = useState('');
const load = useCallback(async (dir?: string) => {
setLoading(true);
setError(null);
try {
const r = await api.fsList(dir);
setData(r);
setCurrent(r.path ?? undefined);
setManual(r.path ?? '');
} catch (e) {
setError(e instanceof Error ? e.message : '读取目录失败');
} finally {
setLoading(false);
}
}, []);
useEffect(() => {
if (open) void load(initialDir);
}, [open, initialDir, load]);
if (!open) return null;
const enter = (name: string) => {
const next = current ? joinPath(current, name) : name;
void load(next);
};
const goUp = () => {
if (data?.parent) void load(data.parent);
};
const goManual = () => {
const v = manual.trim();
if (v) void load(v);
};
// 面包屑分段(Windows 盘符 + 各级目录)
const segments: { label: string; path: string }[] = [];
if (current) {
const norm = current.replace(/\//g, '\\').replace(/\\+$/, '');
const driveMatch = norm.match(/^[a-zA-Z]:/);
let acc = '';
if (driveMatch) {
acc = driveMatch[0] + '\\';
segments.push({ label: acc, path: acc });
}
const rest = norm.slice(acc.length).split('\\').filter(Boolean);
for (const part of rest) {
acc = acc ? `${acc}\\${part}` : part;
segments.push({ label: part, path: acc });
}
}
return (
<div
className="fixed inset-0 z-50 flex items-center justify-center bg-black/60 p-4"
onClick={onClose}
>
<div
className="flex max-h-[80vh] w-full max-w-2xl flex-col overflow-hidden rounded-2xl border border-border bg-card shadow-2xl"
onClick={(e) => e.stopPropagation()}
>
<div className="flex items-center justify-between border-b border-border px-4 py-3">
<div className="flex items-center gap-2 text-sm font-semibold">
<FolderOpen className="h-4 w-4 text-primary" /> 选择目录
</div>
<button
onClick={onClose}
className="flex h-8 w-8 items-center justify-center rounded-lg text-muted-foreground hover:bg-accent hover:text-foreground"
>
<X className="h-4 w-4" />
</button>
</div>
{/* 面包屑 + 上级 */}
<div className="flex items-center gap-1 border-b border-border px-4 py-2 text-[11px]">
<button
onClick={goUp}
disabled={!data?.parent}
className="flex items-center gap-1 rounded-md px-1.5 py-1 text-muted-foreground hover:bg-accent hover:text-foreground disabled:opacity-40"
>
<CornerUpLeft className="h-3.5 w-3.5" /> 上级
</button>
<div className="flex flex-1 flex-wrap items-center gap-0.5 overflow-auto">
{segments.length === 0 ? (
<span className="text-muted-foreground">此电脑</span>
) : (
segments.map((s, i) => (
<span key={s.path} className="flex items-center">
{i > 0 && <ChevronRight className="h-3 w-3 text-muted-foreground/50" />}
<button
onClick={() => void load(s.path)}
className="rounded px-1 py-0.5 font-mono text-muted-foreground hover:bg-accent hover:text-foreground"
>
{s.label}
</button>
</span>
))
)}
</div>
</div>
{/* 手动输入 */}
<div className="flex items-center gap-2 border-b border-border px-4 py-2">
<input
value={manual}
onChange={(e) => setManual(e.target.value)}
onKeyDown={(e) => {
if (e.key === 'Enter') goManual();
}}
placeholder="或直接输入路径,如 D:\\work\\company\\fe"
className="h-9 flex-1 rounded-lg border border-border bg-background px-3 font-mono text-[12px] outline-none focus:border-primary/50"
/>
<button
onClick={goManual}
className="h-9 rounded-lg border border-border px-3 text-xs hover:bg-accent"
>
前往
</button>
</div>
{/* 目录列表 */}
<div className="flex-1 overflow-auto p-2">
{loading ? (
<div className="flex items-center justify-center gap-2 py-10 text-sm text-muted-foreground">
<Loader2 className="h-4 w-4 animate-spin" /> 读取中…
</div>
) : error ? (
<div className="py-10 text-center text-sm text-rose-500">{error}</div>
) : data && data.dirs.length === 0 ? (
<div className="py-10 text-center text-sm text-muted-foreground">该目录下没有子文件夹</div>
) : (
<div className="grid grid-cols-2 gap-1 sm:grid-cols-3">
{data?.dirs.map((d) => (
<button
key={d}
onClick={() => enter(d)}
className="flex items-center gap-2 rounded-lg border border-border bg-background/40 px-3 py-2 text-left text-[12px] hover:border-primary/40 hover:bg-primary/5"
>
<FolderOpen className="h-4 w-4 shrink-0 text-primary/70" />
<span className="truncate font-mono">{d}</span>
</button>
))}
</div>
)}
</div>
{/* 底部:当前路径 + 选择 */}
<div className="flex items-center justify-between gap-3 border-t border-border px-4 py-3">
<span className="min-w-0 flex-1 truncate font-mono text-[11px] text-muted-foreground">
{current || '此电脑'}
</span>
<div className="flex gap-2">
<button
onClick={onClose}
className="h-9 rounded-lg border border-border px-3 text-xs hover:bg-accent"
>
取消
</button>
<button
onClick={() => current && onPick(current)}
disabled={!current}
className="flex h-9 items-center gap-1.5 rounded-lg bg-primary px-3 text-xs font-medium text-primary-foreground hover:bg-primary/90 disabled:opacity-40"
>
<Check className="h-3.5 w-3.5" /> 选择此目录
</button>
</div>
</div>
</div>
</div>
);
}