import { useState, useRef } from 'react'; interface ImportModalProps { isOpen: boolean; onClose: () => void; onImport: (words: any[]) => void; } export function ImportModal({ isOpen, onClose, onImport }: ImportModalProps) { const [content, setContent] = useState(''); const [error, setError] = useState(''); const fileInputRef = useRef(null); if (!isOpen) return null; const handleFileUpload = (e: React.ChangeEvent) => { const file = e.target.files?.[0]; if (!file) return; const reader = new FileReader(); reader.onload = (event) => { const text = event.target?.result as string; setContent(text); parseContent(text); }; reader.readAsText(file); }; const parseContent = (text: string) => { try { // Try JSON first const json = JSON.parse(text); if (Array.isArray(json)) { onImport(json); onClose(); return; } if (json.words && Array.isArray(json.words)) { onImport(json.words); onClose(); return; } } catch { // Not JSON, try CSV const lines = text.trim().split('\n'); const words = lines.map((line, index) => { const parts = line.split(',').map(p => p.trim()); return { id: `imported-${Date.now()}-${index}`, french: parts[0] || '', english: parts[1] || '', pronunciation: parts[2] || '', category: parts[3] || 'General', difficulty: 'beginner', }; }).filter(w => w.french && w.english); if (words.length > 0) { onImport(words); onClose(); } else { setError('无法解析文件格式'); } } }; const handleSubmit = () => { if (!content.trim()) { setError('请输入内容'); return; } parseContent(content); }; return (

导入词库

或粘贴内容