import { useState } from "react"; import { errorMessage } from "../api/client.js"; import { useUpsertVariable } from "../api/hooks.js"; import { FormInput, FormSelect, FormTextarea } from "./FormControls.jsx"; import { DEFAULT_OWNER } from "../lib/tenant.js"; const TYPES = ["string", "number", "boolean"]; function defaultValue(type) { if (type === "boolean") return false; if (type === "number") return ""; return ""; } /** * Add / edit a plaintext workflow variable. * * @param {"add" | "edit"} mode * @param {{ owner: string, name?: string, type?: string, value?: string | number | boolean }} initial * @param {() => void} onClose * @param {(saved: unknown) => void} [onSaved] */ export function VariableEditorModal({ mode, initial, onClose, onSaved }) { const upsert = useUpsertVariable(); const [form, setForm] = useState(() => ({ owner: initial.owner || DEFAULT_OWNER, name: initial.name || "", type: initial.type || "string", value: initial.type === "number" ? String(initial.value ?? "") : initial.value !== undefined ? initial.value : defaultValue(initial.type || "string"), })); const [formError, setFormError] = useState(null); function onTypeChange(type) { setForm({ ...form, type, value: defaultValue(type) }); } function onSubmit(e) { e.preventDefault(); let value = form.value; if (form.type === "number") { value = Number(form.value); if (!Number.isFinite(value)) { setFormError("value must be a finite number"); return; } } if (form.type === "boolean") { value = form.value === true; } setFormError(null); upsert.mutate( { owner: form.owner, name: form.name, type: form.type, value }, { onSuccess: (data) => { onSaved?.(data?.variable ?? data); onClose(); }, }, ); } const title = mode === "add" ? "New variable" : `Edit ${form.name}`; return (

{title}

{mode === "add" ? ( ) : null}

Stored in plaintext. Use Secrets for credentials. In workflows use{" "} {"{{ vars.name }}"} (quote strings that start with {"{"}). Nested:{" "} {"{{ context.user.id }}"}.

{formError ?

{formError}

: null} {upsert.isError ? (

{errorMessage(upsert.error)}

) : null}
); }