UploadModal.tsx
8.99 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
"use client";
import { Upload, X, Loader2, CheckCircle2, AlertCircle, Database } from "lucide-react";
import { KbTreePicker } from "@/components/system/OrgTree";
import { getFileIcon } from "@/components/wiki/fileIcons";
import type { UploadQueueItem } from "@/types/wiki";
import type { KbInfo } from "@/lib/auth";
/**
* 上传弹窗(纯展示)。
*
* 队列状态与上传动作都由父级持有:上传涉及库权限校验、失败重试与列表刷新,
* 这里只负责渲染队列与转发点击,不自持任何状态。
*/
export interface UploadModalProps {
open: boolean;
onClose: () => void;
/** 可选的目标知识库(需写权限,由父级过滤) */
readableKbs: KbInfo[];
/** 空串=未选择(与 KbTreePicker 的「全部」语义一致) */
uploadKbId: string;
onUploadKbChange: (token: string) => void;
uploadQueue: UploadQueueItem[];
onQueueChange: (next: UploadQueueItem[]) => void;
/** 选择文件(初次选择与「继续添加」共用) */
onSelectFiles: (e: React.ChangeEvent<HTMLInputElement>) => void;
/** 确认上传 */
onConfirm: () => void;
/** 全部完成后点「确认」:关闭并刷新文件列表 */
onFinish: () => void;
}
export const UploadModal = ({
open,
onClose,
readableKbs,
uploadKbId,
onUploadKbChange,
uploadQueue,
onQueueChange,
onSelectFiles,
onConfirm,
onFinish,
}: UploadModalProps) => {
if (!open) return null;
const isUploading = uploadQueue.some((q) => q.status === "uploading");
const isAllSettled = uploadQueue.every((q) => q.status === "success" || q.status === "error");
return (
<div className="fixed inset-0 z-[100] flex items-center justify-center p-8 bg-slate-900/60 backdrop-blur-sm animate-in fade-in duration-300">
<div className="bg-white w-full max-w-2xl rounded-3xl shadow-2xl flex flex-col overflow-hidden animate-in zoom-in-95 duration-300">
<header className="px-8 py-6 border-b border-slate-100 flex items-center justify-between bg-slate-50/50">
<div className="flex items-center gap-3">
<div className="p-2.5 bg-blue-100 text-blue-600 rounded-xl">
<Upload className="w-5 h-5" />
</div>
<div>
<h2 className="text-xl font-bold text-slate-800">上传新文件</h2>
<p className="text-xs text-slate-500 mt-0.5">支持 PDF, Word, Excel, PPT, Markdown</p>
</div>
</div>
<button onClick={onClose} className="p-2 hover:bg-slate-200 rounded-xl transition-colors text-slate-400 hover:text-slate-800">
<X className="w-5 h-5" />
</button>
</header>
{/* 上传目标知识库选择:文件按库隔离落盘(需对目标库有写权限:Owner/Editor;否则接口 403) */}
<div className="px-8 pt-5 flex items-center gap-3">
<span className="flex items-center gap-1.5 text-sm font-medium text-slate-600 shrink-0">
<Database className="w-4 h-4 text-blue-500" /> 目标知识库
</span>
<KbTreePicker
mode="single"
kbs={readableKbs}
value={uploadKbId}
onChange={(token) => onUploadKbChange(token)}
placeholder="选择目标知识库"
showAccessBadge
panelWidthClass="w-[24rem]"
buttonClassName="flex flex-1 items-center gap-1.5 h-10 rounded-xl border border-slate-200 bg-white px-3 text-sm text-slate-700 outline-none transition hover:border-slate-300 focus:border-blue-500"
/>
</div>
<div className="px-8 pt-5 pb-8 flex flex-col gap-6 bg-white min-h-[260px] max-h-[52vh] overflow-auto">
{uploadQueue.length === 0 ? (
<div className="flex-1 flex flex-col items-center justify-center border-2 border-dashed border-slate-200 rounded-2xl bg-slate-50 hover:bg-blue-50/50 hover:border-blue-300 transition-colors p-10 relative group">
<input type="file" multiple className="absolute inset-0 w-full h-full opacity-0 cursor-pointer z-10" onChange={onSelectFiles} />
<div className="w-16 h-16 bg-white shadow-sm rounded-full flex items-center justify-center mb-4 group-hover:scale-110 transition-transform">
<Upload className="w-8 h-8 text-blue-500" />
</div>
<h3 className="text-lg font-bold text-slate-700">点击或拖拽文件到此处</h3>
<p className="text-sm text-slate-400 mt-2 text-center max-w-xs">
上传的文件将进入“原始文件大厅”,稍后您可以发起批量摄入。
</p>
</div>
) : (
<div className="flex flex-col gap-3">
<div className="flex items-center justify-between mb-2">
<h3 className="text-sm font-bold text-slate-700">待上传列表 ({uploadQueue.length})</h3>
{!isUploading && (
<label className="text-xs font-bold text-blue-600 bg-blue-50 px-3 py-1.5 rounded-lg cursor-pointer hover:bg-blue-100 transition-colors">
+ 继续添加
<input type="file" multiple className="hidden" onChange={onSelectFiles} />
</label>
)}
</div>
{uploadQueue.map((item, idx) => (
<div key={idx} className="flex items-center gap-4 p-4 bg-slate-50 border border-slate-100 rounded-xl">
<div className="p-2 bg-white rounded-lg shadow-sm border border-slate-100">
{getFileIcon(item.name.split('.').pop() || "")}
</div>
<div className="flex-1 min-w-0">
<div className="flex items-center justify-between mb-1">
<span className="text-sm font-semibold text-slate-700 truncate">{item.name}</span>
<span className="text-xs font-medium text-slate-500">{(item.file.size / 1024 / 1024).toFixed(2)} MB</span>
</div>
{item.status === 'uploading' ? (
<div className="flex items-center gap-1.5 mt-1 text-xs font-bold text-blue-600">
<Loader2 className="w-3.5 h-3.5 animate-spin" /> 保存中...
</div>
) : item.status === 'success' ? (
<div className="mt-1">
<div className="flex items-center gap-1.5 text-xs font-bold text-emerald-600">
<CheckCircle2 className="w-3.5 h-3.5" /> 已保存到文件库
</div>
</div>
) : item.status === 'error' ? (
<div className="flex items-center gap-1.5 mt-1 text-xs font-bold text-red-600" title={item.errorMsg}>
<AlertCircle className="w-3.5 h-3.5" /> {item.errorMsg || "上传失败"}
</div>
) : (
<div className="flex items-center gap-1.5 mt-1 text-xs font-bold text-slate-400">
等待上传...
</div>
)}
</div>
{item.status !== 'uploading' && (
<button onClick={() => onQueueChange(uploadQueue.filter((_, i) => i !== idx))} className="p-2 hover:bg-slate-200 rounded-lg text-slate-400 hover:text-slate-700 transition-colors" title="移除该项">
<X className="w-4 h-4" />
</button>
)}
</div>
))}
</div>
)}
</div>
{uploadQueue.length > 0 && (
<footer className="px-8 py-4 border-t border-slate-100 bg-slate-50 flex items-center justify-end gap-3">
<button
onClick={() => { onQueueChange([]); onClose(); }}
disabled={isUploading}
className="px-6 py-2.5 rounded-xl font-bold text-sm text-slate-600 hover:bg-slate-200 transition-colors disabled:opacity-50"
>
取消
</button>
{isAllSettled ? (
<button
onClick={onFinish}
className="flex items-center gap-2 px-6 py-2.5 bg-emerald-600 hover:bg-emerald-700 text-white rounded-xl font-bold text-sm transition-colors shadow-lg shadow-emerald-500/30"
>
<CheckCircle2 className="w-4 h-4" /> 确认
</button>
) : (
<button
onClick={onConfirm}
disabled={isUploading}
className="flex items-center gap-2 px-6 py-2.5 bg-blue-600 hover:bg-blue-700 text-white rounded-xl font-bold text-sm transition-colors shadow-lg shadow-blue-500/30 disabled:opacity-50"
>
{isUploading ? (
<><Loader2 className="w-4 h-4 animate-spin" /> 上传中...</>
) : (
<><Upload className="w-4 h-4" /> 确认上传</>
)}
</button>
)}
</footer>
)}
</div>
</div>
);
};
export default UploadModal;