import { useState } from 'react'; import { useEditorStore } from '../../store/useEditorStore'; import { useSimulatorStore, BOARD_FQBN } from '../../store/useSimulatorStore'; import { compileCode } from '../../services/compilation'; import { LibraryManagerModal } from '../simulator/LibraryManagerModal'; import './EditorToolbar.css'; export const EditorToolbar = () => { const { code } = useEditorStore(); const { boardType, setCompiledHex, setCompiledBinary, startSimulation, stopSimulation, resetSimulation, running, compiledHex, } = useSimulatorStore(); const [compiling, setCompiling] = useState(false); const [message, setMessage] = useState<{ type: 'success' | 'error'; text: string } | null>(null); const [libManagerOpen, setLibManagerOpen] = useState(false); const handleCompile = async () => { setCompiling(true); setMessage(null); try { const fqbn = BOARD_FQBN[boardType]; const result = await compileCode(code, fqbn); if (result.success) { if (result.hex_content) { setCompiledHex(result.hex_content); setMessage({ type: 'success', text: 'Compiled successfully' }); } else if (result.binary_content) { setCompiledBinary(result.binary_content); setMessage({ type: 'success', text: 'Compiled successfully' }); } else { setMessage({ type: 'error', text: 'No output' }); } } else { setMessage({ type: 'error', text: result.error || result.stderr || 'Compile failed' }); } } catch (err) { setMessage({ type: 'error', text: err instanceof Error ? err.message : 'Compile failed' }); } finally { setCompiling(false); } }; const handleRun = () => { if (compiledHex) { startSimulation(); setMessage(null); } else { setMessage({ type: 'error', text: 'Compile first' }); } }; const handleStop = () => { stopSimulation(); setMessage(null); }; const handleReset = () => { resetSimulation(); setMessage(null); }; return ( <>