import React, { useState, useEffect } from 'react'; import { Parameter } from '../recipe'; import { Button } from './ui/button'; interface ParameterInputModalProps { parameters: Parameter[]; onSubmit: (values: Record) => void; onClose: () => void; } const ParameterInputModal: React.FC = ({ parameters, onSubmit, onClose, }) => { const [inputValues, setInputValues] = useState>({}); const [validationErrors, setValidationErrors] = useState>({}); const [showCancelOptions, setShowCancelOptions] = useState(false); // Pre-fill the form with default values from the recipe useEffect(() => { const initialValues: Record = {}; parameters.forEach((param) => { if (param.requirement === 'optional' && param.default) { const defaultValue = param.input_type === 'boolean' ? param.default.toLowerCase() : param.default; initialValues[param.key] = defaultValue; } }); setInputValues(initialValues); }, [parameters]); 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') { // Create a new chat window without recipe config try { const workingDir = window.appConfig.get('GOOSE_WORKING_DIR'); console.log(`Creating new chat window without recipe, working dir: ${workingDir}`); window.electron.createChatWindow(undefined, workingDir as string); // Close the current window after creating the new one window.electron.hideWindow(); } catch (error) { console.error('Error creating new window:', error); // Fallback: just close the modal 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;