import { Component, createSignal, For, Show } from "solid-js" import { Plus, Trash2, Key, Globe } from "lucide-solid" import { useConfig } from "../stores/preferences" interface EnvironmentVariablesEditorProps { disabled?: boolean } const EnvironmentVariablesEditor: Component = (props) => { const { preferences, addEnvironmentVariable, removeEnvironmentVariable, updateEnvironmentVariables, } = useConfig() const [envVars, setEnvVars] = createSignal>(preferences().environmentVariables || {}) const [newKey, setNewKey] = createSignal("") const [newValue, setNewValue] = createSignal("") const entries = () => Object.entries(envVars()) function handleAddVariable() { const key = newKey().trim() const value = newValue().trim() if (!key) return addEnvironmentVariable(key, value) setEnvVars({ ...envVars(), [key]: value }) setNewKey("") setNewValue("") } function handleRemoveVariable(key: string) { removeEnvironmentVariable(key) const { [key]: removed, ...rest } = envVars() setEnvVars(rest) } function handleUpdateVariable(key: string, value: string) { const updated = { ...envVars(), [key]: value } setEnvVars(updated) updateEnvironmentVariables(updated) } function handleKeyPress(e: KeyboardEvent) { if (e.key === "Enter" && !e.shiftKey) { e.preventDefault() handleAddVariable() } } return (
Environment Variables ({entries().length} variable{entries().length !== 1 ? "s" : ""})
{/* Existing variables */} 0}>
{([key, value]) => (
handleUpdateVariable(key, e.currentTarget.value)} class="flex-1 px-2.5 py-1.5 text-sm bg-surface-base border border-base rounded text-primary focus-ring-accent disabled:opacity-50 disabled:cursor-not-allowed" placeholder="Variable value" />
)}
{/* Add new variable */}
setNewKey(e.currentTarget.value)} onKeyPress={handleKeyPress} disabled={props.disabled} class="flex-1 px-2.5 py-1.5 text-sm bg-surface-base border border-base rounded text-primary focus-ring-accent disabled:opacity-50 disabled:cursor-not-allowed" placeholder="Variable name" /> setNewValue(e.currentTarget.value)} onKeyPress={handleKeyPress} disabled={props.disabled} class="flex-1 px-2.5 py-1.5 text-sm bg-surface-base border border-base rounded text-primary focus-ring-accent disabled:opacity-50 disabled:cursor-not-allowed" placeholder="Variable value" />
No environment variables configured. Add variables above to customize the OpenCode environment.
These variables will be available in the OpenCode environment when starting instances.
) } export default EnvironmentVariablesEditor