import { useState, useRef, useEffect } from 'react' import UploadForm from './UploadForm'; import LogsDisplay from './LogsDisplay'; import FiguresDisplay from './FiguresDisplay'; import ImageModal from './ImageModal'; import 'bootstrap/dist/css/bootstrap.min.css'; import './App.css'; function App() { const [selectedFile, setSelectedFile] = useState(null); const [formData, setFormData] = useState({ tilt: '', num_threshold: 10, entropy_threshold: 0.01, n_plots: 10 }); const [statusText, setStatusText] = useState('Start Analysis'); const [figures, setFigures] = useState([]); const [logs, setLogs] = useState([]); const [modalState, setModalState] = useState({ show: false, currentIndex: 0 }); const leftFormRef = useRef(null); const rightPanelRef = useRef(null); const logPanelRef = useRef(null); const validateForm = () => { if (!selectedFile || !selectedFile.name.endsWith('.cs')) { alert('Please select a .cs file to upload'); return false; } if (!formData.tilt || isNaN(parseFloat(formData.tilt))) { alert('Micrograph Tilting must be a floating number'); return false; } if (!formData.num_threshold || isNaN(parseInt(formData.num_threshold))) { alert('Fibril Minimum Particle Count Threshold must be an integer'); return false; } else if (parseInt(formData.num_threshold) < 0) { alert('Fibril Minimum Particle Count Threshold must be a non-negative integer'); return false; } if (!formData.entropy_threshold || isNaN(parseFloat(formData.entropy_threshold))) { alert('Fibril Maximum Particle Entropy Threshold must be a floating number'); return false; } else if (parseFloat(formData.entropy_threshold) < 0 || parseFloat(formData.entropy_threshold) > 1) { alert('Fibril Maximum Particle Entropy Threshold must be a floating number between 0 and 1'); return false; } if (!formData.n_plots || isNaN(parseInt(formData.n_plots))) { alert('Number of Fibrils to Analyze must be an integer'); return false; } else if (parseInt(formData.n_plots) < 0) { alert('Number of Fibrils to Analyze must be a non-negative integer'); return false; } return true; } const handleSubmit = async (e) => { e.preventDefault(); if (!validateForm()) return; try { setStatusText('Uploading...'); setLogs([]); setFigures([]); // Create FormData object const submitData = new FormData(); submitData.append('file', selectedFile); submitData.append('tilt', formData.tilt); submitData.append('num_threshold', formData.num_threshold); submitData.append('entropy_threshold', formData.entropy_threshold); submitData.append('n_plots', formData.n_plots); const response = await fetch('https://zerinf-ahaha-backend.hf.space/api', { method: 'POST', body: submitData }); if (!response.ok) { throw new Error('Ahaha backend broke down, contact the administrator!'); } setStatusText('Processing...'); // 处理流式响应,注意read得到的chunk未必是完整的 const reader = response.body.getReader(); const decoder = new TextDecoder(); let buffer = ''; while (true) { const { done, value } = await reader.read(); if (done) break; const chunk = decoder.decode(value); buffer += chunk; const lines = buffer.split('\n'); buffer = lines.pop() || ''; for (const line of lines) { if (line.startsWith('data: ')) { const data = JSON.parse(line.slice(6)); if (data.type == 'info' || data.type == 'final') { setLogs(prev => [...prev, data]); // console.log(`[${data.type}] ${data.message}`); } else if (data.type == 'error') { throw new Error(data.message); } else if (data.type == 'data') { setFigures(prev => [...prev, data.data]); } } } } } catch (error) { alert(`Failed to analysis data: ${error}`); } finally { setStatusText('Start Analysis'); } }; useEffect(() => { if (leftFormRef.current && rightPanelRef.current) { rightPanelRef.current.style.minHeight = `${leftFormRef.current.offsetHeight}px`; } }, [logs.length, figures.length, selectedFile, formData, statusText, modalState]); useEffect(() => { if (logPanelRef.current) { logPanelRef.current.scrollTop = logPanelRef.current.scrollHeight; } }, [logs]); return (