import { useState } from "react"; import Form from "@moduletrace/twui/form/Form"; import Input from "@moduletrace/twui/form/Input"; import Checkbox from "@moduletrace/twui/form/Checkbox"; import Button from "@moduletrace/twui/layout/Button"; import Row from "@moduletrace/twui/layout/Row"; import Stack from "@moduletrace/twui/layout/Stack"; import type { EnvScope, EnvsMutationResult, KnownEnvStatus, PublicEnv } from "@/src/types"; import postEnvApi from "../../(functions)/post-env-api"; import SettingsStatus from "../settings-status"; type Props = { scope: EnvScope; /** * Edits this env; adds one when not given. */ env?: PublicEnv; /** * Sets this app setting: its key, description and flags are fixed. */ known?: KnownEnvStatus; onSaved: (data: EnvsMutationResult, msg: string) => void; onCancel: () => void; }; /** * Adds or edits an env in one scope. A saved secret is never shown; typing * a new value replaces it, leaving it empty keeps it. */ export default function EnvForm({ scope, env, known, onSaved, onCancel }: Props) { const is_known = Boolean(known || env?.known); const [key, setKey] = useState(env?.key || known?.key || ""); const [value, setValue] = useState(env && !env.secret ? env.value || "" : ""); const [description, setDescription] = useState(env?.description || ""); const [secret, setSecret] = useState(env?.secret ?? known?.secret ?? true); const [expose, setExpose] = useState(env?.expose_to_computers ?? true); const [loading, setLoading] = useState(false); const [error, setError] = useState(); const id = `env-${env?.id || known?.key || "new"}`; const keeps_secret = Boolean(env?.secret); const submit = async () => { if (loading) return; if (!window.confirm(env?.id ? `Save ${env.key}?` : `Add ${key || "this env"}?`)) return; setLoading(true); setError(undefined); const res = await postEnvApi({ path: "save-env", body: { env_id: env?.id, key, value: keeps_secret && !value ? undefined : value, description: is_known ? undefined : description, paradigm: scope.paradigm, workspace_id: scope.paradigm == "workspace" ? scope.id : null, computer_id: scope.paradigm == "computer" ? scope.id : null, bot_id: scope.paradigm == "bot" ? scope.id : null, scoped: scope.paradigm == "bot" || scope.paradigm == "workspace", secret, expose_to_computers: expose, }, }); setLoading(false); if (!res.success || !res.singleRes) { setError(res.msg || "Couldn't save the env."); return; } onSaved(res.singleRes, res.msg || "Saved."); }; return (
setKey(e.target.value)} /> setValue(e.target.value)} /> {is_known ? null : ( <> setDescription(e.target.value)} /> )} ); }