/** * Design Philosophy: Japanese Minimalism * - Generous whitespace (Ma concept) * - Subtle borders and gentle interactions * - Elegant hover states with slow transitions */ import { Upload } from 'lucide-react'; import { useRef, useState } from 'react'; import { toast } from 'sonner'; import { parseExcelFile } from '@/lib/projectUtils'; interface FileUploadProps { onFileSelect: (file: File) => void; onDataParsed?: (data: any[]) => void; // Optional callback after parsing } export default function FileUpload({ onFileSelect, onDataParsed }: FileUploadProps) { const fileInputRef = useRef(null); const [isProcessing, setIsProcessing] = useState(false); const handleFileChange = async (e: React.ChangeEvent) => { const file = e.target.files?.[0]; if (file) { if (file.name.endsWith('.xlsx') || file.name.endsWith('.xls')) { onFileSelect(file); // Parse Excel file if onDataParsed callback is provided if (onDataParsed) { setIsProcessing(true); try { const projects = await parseExcelFile(file); onDataParsed(projects); } catch (error) { console.error('Failed to parse Excel:', error); toast.error('文件解析失败,请检查文件格式'); } finally { setIsProcessing(false); } } } else { toast.error('请上传 Excel 文件 (.xlsx 或 .xls)'); } } }; const handleDrop = async (e: React.DragEvent) => { e.preventDefault(); const file = e.dataTransfer.files[0]; if (file) { if (file.name.endsWith('.xlsx') || file.name.endsWith('.xls')) { onFileSelect(file); // Parse Excel file if onDataParsed callback is provided if (onDataParsed) { setIsProcessing(true); try { const projects = await parseExcelFile(file); onDataParsed(projects); } catch (error) { console.error('Failed to parse Excel:', error); toast.error('文件解析失败,请检查文件格式'); } finally { setIsProcessing(false); } } } else { toast.error('请上传 Excel 文件 (.xlsx 或 .xls)'); } } }; const handleDragOver = (e: React.DragEvent) => { e.preventDefault(); }; return (
fileInputRef.current?.click()} onDrop={handleDrop} onDragOver={handleDragOver} className="relative cursor-pointer group" > {/* Subtle gradient background */}
{/* Main content */}
{/* Icon with ripple effect on hover */}
{/* Text content */}

上传项目进度表

拖拽 Excel 文件到此处,或点击选择文件

支持 .xlsx 和 .xls 格式

); }