import React, { useState } from 'react' import { Input } from '@/components/ui/input' import { Label } from '@/components/ui/label' import { Button } from '@/components/ui/button' import { X, Plus } from 'lucide-react' interface EnvironmentVariablesInputProps { id: string label: string value: Record | undefined onChange: (value: Record | undefined) => void description?: string disabled?: boolean className?: string } interface EnvVar { key: string value: string } const EnvironmentVariablesInput: React.FC = ({ id, label, value, onChange, description, disabled = false, className }) => { // Convert the value object to an array of key-value pairs for editing const envVarsFromValue = value ? Object.entries(value).map(([key, val]) => ({ key, value: val })) : [] const [envVars, setEnvVars] = useState( envVarsFromValue.length > 0 ? envVarsFromValue : [{ key: '', value: '' }] ) // Update parent component when env vars change const updateParent = (newEnvVars: EnvVar[]) => { // Filter out empty entries const validVars = newEnvVars.filter(env => env.key.trim() !== '' && env.value.trim() !== '') if (validVars.length === 0) { onChange(undefined) } else { const envObject = validVars.reduce((acc, env) => { acc[env.key.trim()] = env.value.trim() return acc }, {} as Record) onChange(envObject) } } const handleKeyChange = (index: number, newKey: string) => { const newEnvVars = [...envVars] newEnvVars[index].key = newKey setEnvVars(newEnvVars) updateParent(newEnvVars) } const handleValueChange = (index: number, newValue: string) => { const newEnvVars = [...envVars] newEnvVars[index].value = newValue setEnvVars(newEnvVars) updateParent(newEnvVars) } const addEnvVar = () => { const newEnvVars = [...envVars, { key: '', value: '' }] setEnvVars(newEnvVars) } const removeEnvVar = (index: number) => { if (envVars.length === 1) { // Reset to empty if it's the last one const newEnvVars = [{ key: '', value: '' }] setEnvVars(newEnvVars) updateParent(newEnvVars) } else { const newEnvVars = envVars.filter((_, i) => i !== index) setEnvVars(newEnvVars) updateParent(newEnvVars) } } return (
{envVars.map((envVar, index) => (
handleKeyChange(index, e.target.value)} disabled={disabled} className="flex-1" /> handleValueChange(index, e.target.value)} disabled={disabled} className="flex-1" />
))}
{description && (

{description}

)}

Environment variables that will be passed to the backend process

) } export default EnvironmentVariablesInput