import { Component, createSignal, For, Show } from "solid-js" import { Plus, Trash2, Key, Globe } from "lucide-solid" import { useConfig } from "../stores/preferences" import { useI18n } from "../lib/i18n" interface EnvironmentVariablesEditorProps { disabled?: boolean } const EnvironmentVariablesEditor: Component = (props) => { const { t } = useI18n() const { serverSettings, addEnvironmentVariable, removeEnvironmentVariable, updateEnvironmentVariables, } = useConfig() const [envVars, setEnvVars] = createSignal>(serverSettings().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 (
{t("envEditor.title")} {entries().length === 1 ? t("envEditor.count.one", { count: entries().length }) : t("envEditor.count.other", { count: entries().length })}
{/* 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={t("envEditor.fields.value.placeholder")} />
)}
{/* 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={t("envEditor.fields.name.placeholder")} /> 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={t("envEditor.fields.value.placeholder")} />
{t("envEditor.empty")}
{t("envEditor.help")}
) } export default EnvironmentVariablesEditor