Files
AIPortPilot/frontend/src/components/shared/FileUploader.tsx
T
selfrelease fad458b2a7 docs(uiux): UIUX 设计方案大改 + 5 份作业指导书对齐 + 开发任务文档
- UIUX 文档:填充 19 个缺口(多主体画像/健康度/AI+看板/增长域/洞察域/创始人端/OODA/助推/商密)
- UIUX 文档:插入 6 个新章节(十四~十九),旧章节重编号为二十~三十一,更新目录和交叉引用
- 作业指导书 x5:导航改为 6 域分组,新增 Context Bar/工作模式/Insight Rail/决策线程/多工作区等 UI 概念
- 新建 docs/2-task-uiux.md:50 个代码落地开发任务,按 P0-P6 分优先级 + 8 Sprint 规划
- 后端/前端:大量新增模型、路由、组件(来自之前 Phase 开发)
2026-07-19 11:53:38 +08:00

89 lines
3.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"use client";
import { useState, useRef, useCallback } from "react";
import { Upload, File as FileIcon, X, Loader2 } from "lucide-react";
/** 文件上传组件 — 支持拖拽 + 点击上传。 */
export function FileUploader({ onParsed }: { onParsed: (result: any) => void }) {
const [dragging, setDragging] = useState(false);
const [uploading, setUploading] = useState(false);
const [file, setFile] = useState<File | null>(null);
const inputRef = useRef<HTMLInputElement>(null);
const allowedTypes = [".xlsx", ".xls", ".pdf", ".txt", ".md", ".csv"];
const handleFile = useCallback(async (f: File) => {
const ext = f.name.match(/\.[^.]+$/)?.[0]?.toLowerCase() || "";
if (!allowedTypes.includes(ext)) {
return;
}
setFile(f);
setUploading(true);
try {
const formData = new FormData();
formData.append("file", f);
const token = localStorage.getItem("token");
const resp = await fetch("/api/v1/reports/upload", {
method: "POST",
headers: { Authorization: `Bearer ${token}` },
body: formData,
});
const data = await resp.json();
onParsed(data.data);
} catch {
// ignore
} finally {
setUploading(false);
}
}, [onParsed]);
return (
<div
className="rounded-lg border-2 border-dashed border-[var(--border)] p-6 text-center transition-colors"
onDragOver={(e) => { e.preventDefault(); setDragging(true); }}
onDragLeave={() => setDragging(false)}
onDrop={(e) => {
e.preventDefault();
setDragging(false);
if (e.dataTransfer.files[0]) handleFile(e.dataTransfer.files[0]);
}}
style={dragging ? { borderColor: "var(--investor-primary)", background: "var(--investor-primary)/5" } : {}}
>
{file ? (
<div className="flex items-center justify-between rounded-md border border-[var(--border)] bg-white px-3 py-2">
<div className="flex items-center gap-2">
<FileIcon size={16} className="text-[var(--investor-primary)]" />
<span className="text-sm">{file.name}</span>
</div>
<div className="flex items-center gap-2">
{uploading && <Loader2 size={14} className="animate-spin text-muted-foreground" />}
<button
onClick={() => { setFile(null); onParsed(null); }}
className="text-muted-foreground hover:text-foreground"
aria-label="移除文件"
>
<X size={14} />
</button>
</div>
</div>
) : (
<button
onClick={() => inputRef.current?.click()}
className="flex flex-col items-center gap-2 text-sm text-muted-foreground hover:text-foreground"
>
<Upload size={24} />
<span></span>
<span className="text-xs"> .xlsx .pdf .txt .csv .md 10MB</span>
</button>
)}
<input
ref={inputRef}
type="file"
accept=".xlsx,.xls,.pdf,.txt,.md,.csv"
className="hidden"
onChange={(e) => { if (e.target.files?.[0]) handleFile(e.target.files[0]); }}
/>
</div>
);
}