import React, { useState, useEffect } from 'react'; import { Parameter } from '../recipe'; import { Button } from './ui/button'; import { getInitialWorkingDir } from '../utils/workingDir'; interface ParameterInputModalProps { parameters: Parameter[]; onSubmit: (values: Record) => void; onClose: () => void; initialValues?: Record; } const ParameterInputModal: React.FC = ({ parameters, onSubmit, onClose, initialValues, }) => { const [inputValues, setInputValues] = useState>({}); const [validationErrors, setValidationErrors] = useState>({}); const [showCancelOptions, setShowCancelOptions] = useState(false); // Pre-fill the form with default values from the recipe and initialValues from deeplink useEffect(() => { const defaultValues: Record = {}; parameters.forEach((param) => { if (param.requirement === 'optional' && param.default) { defaultValues[param.key] = param.input_type === 'boolean' ? param.default.toLowerCase() : param.default; } }); setInputValues({ ...defaultValues, ...initialValues }); }, [parameters, initialValues]); const handleChange = (name: string, value: string): void => { setInputValues((prevValues: Record) => ({ ...prevValues, [name]: value })); }; const handleSubmit = (): void => { // Clear previous validation errors setValidationErrors({}); // Check if all *required* parameters are filled const requiredParams: Parameter[] = parameters.filter((p) => p.requirement === 'required'); const errors: Record = {}; requiredParams.forEach((param) => { const value = inputValues[param.key]?.trim(); if (!value) { errors[param.key] = `${param.description || param.key} is required`; } }); if (Object.keys(errors).length > 0) { setValidationErrors(errors); return; } onSubmit(inputValues); }; const handleCancel = (): void => { // Always show cancel options if recipe has any parameters (required or optional) const hasAnyParams = parameters.length > 0; if (hasAnyParams) { setShowCancelOptions(true); } else { onClose(); } }; const handleCancelOption = (option: 'new-chat' | 'back-to-form'): void => { if (option === 'new-chat') { try { const workingDir = getInitialWorkingDir(); window.electron.createChatWindow(undefined, workingDir); window.electron.hideWindow(); } catch (error) { console.error('Error creating new window:', error); onClose(); } } else { setShowCancelOptions(false); // Go back to the parameter form } }; return (
{showCancelOptions ? ( // Cancel options modal

Cancel Recipe Setup

What would you like to do?

) : ( // Main parameter form

Recipe Parameters

{parameters.map((param) => (
{/* Render different input types */} {param.input_type === 'select' && param.options ? ( ) : param.input_type === 'boolean' ? ( ) : ( handleChange(param.key, e.target.value)} className={`w-full p-3 border rounded-lg bg-bgSubtle text-textStandard focus:outline-none focus:ring-2 ${ validationErrors[param.key] ? 'border-red-500 focus:ring-red-500' : 'border-borderSubtle focus:ring-borderProminent' }`} placeholder={param.default || `Enter value for ${param.key}...`} /> )} {validationErrors[param.key] && (

{validationErrors[param.key]}

)}
))}
)}
); }; export default ParameterInputModal;