/** * ConfigImporter - Import optimization configs into canvas * * Supports: * - File upload (optimization_config.json) * - Paste JSON directly * - Load from study directory */ import { useState, useRef } from 'react'; import { useCanvasStore, OptimizationConfig } from '../../../hooks/useCanvasStore'; interface ConfigImporterProps { isOpen: boolean; onClose: () => void; onImport: (source: string) => void; } export function ConfigImporter({ isOpen, onClose, onImport }: ConfigImporterProps) { const [tab, setTab] = useState<'file' | 'paste' | 'study'>('file'); const [jsonText, setJsonText] = useState(''); const [studyPath, setStudyPath] = useState(''); const [error, setError] = useState(null); const [isLoading, setIsLoading] = useState(false); const fileInputRef = useRef(null); const { loadFromConfig } = useCanvasStore(); if (!isOpen) return null; const handleFileSelect = async (e: React.ChangeEvent) => { const file = e.target.files?.[0]; if (!file) return; setError(null); setIsLoading(true); try { const text = await file.text(); const config = JSON.parse(text) as OptimizationConfig; validateConfig(config); loadFromConfig(config); onImport(`File: ${file.name}`); handleClose(); } catch (err) { setError(err instanceof Error ? err.message : 'Invalid JSON file'); } finally { setIsLoading(false); } }; const handlePasteImport = () => { setError(null); try { const config = JSON.parse(jsonText) as OptimizationConfig; validateConfig(config); loadFromConfig(config); onImport('Pasted JSON'); handleClose(); } catch (err) { setError(err instanceof Error ? err.message : 'Invalid JSON'); } }; const handleStudyLoad = async () => { setError(null); setIsLoading(true); try { // Call backend API to load study config const response = await fetch(`/api/studies/${encodeURIComponent(studyPath)}/config`); if (!response.ok) { throw new Error(`Study not found: ${studyPath}`); } const config = await response.json() as OptimizationConfig; validateConfig(config); loadFromConfig(config); onImport(`Study: ${studyPath}`); handleClose(); } catch (err) { setError(err instanceof Error ? err.message : 'Failed to load study'); } finally { setIsLoading(false); } }; const validateConfig = (config: OptimizationConfig) => { if (!config) { throw new Error('Empty configuration'); } // Must have at least one design variable or objective const hasDesignVars = config.design_variables && config.design_variables.length > 0; const hasObjectives = config.objectives && config.objectives.length > 0; if (!hasDesignVars && !hasObjectives) { throw new Error('Configuration must have design variables or objectives'); } }; const handleClose = () => { setJsonText(''); setStudyPath(''); setError(null); setTab('file'); onClose(); }; return (
{/* Header */}

Import Configuration

{/* Tabs */}
{[ { id: 'file', label: 'Upload File' }, { id: 'paste', label: 'Paste JSON' }, { id: 'study', label: 'Load Study' }, ].map((t) => ( ))}
{/* Content */}
{/* File Upload Tab */} {tab === 'file' && (

Upload an optimization_config.json file from an existing study.

)} {/* Paste JSON Tab */} {tab === 'paste' && (

Paste your optimization configuration JSON below.