import { validEnvKey } from "./format.js";
import { t } from "./i18n.js";
export function envRowState(row, draft) {
return {
key: draft?.key ?? row.key,
value: draft?.value ?? (row.isSecret ? "" : row.value),
useSecret: draft?.useSecret ?? !!row.isSecret,
secret: draft?.secret ?? (row.secretName || ""),
};
}
export function envRowChanges(row, state) {
const key = (state.key || "").trim();
const renameTo = key !== row.key ? key : null;
let newValue = null;
if (state.useSecret) {
if (!row.isSecret || state.secret !== row.secretName) newValue = { Secret: state.secret || "" };
} else if (row.isSecret || state.value !== row.value) {
newValue = { Plain: state.value ?? "" };
}
return { renameTo, newValue };
}
export function envRowEditor({ scope, secretOptions, afterSave, footer }) {
return {
envDrafts: {},
envDraftId(row) {
const where = scope.call(this);
return `${where.project_id}|${where.service_id || ""}|${where.job_id || ""}|${row.key}`;
},
envRow(row) {
return envRowState(row, this.envDrafts[this.envDraftId(row)]);
},
setEnvDraft(row, field, value) {
const id = this.envDraftId(row);
const next = { ...envRowState(row, this.envDrafts[id]), [field]: value };
this.envDrafts = { ...this.envDrafts, [id]: next };
},
envRowDirty(row) {
const draft = this.envDrafts[this.envDraftId(row)];
if (!draft) return false;
const changes = envRowChanges(row, draft);
return changes.renameTo !== null || changes.newValue !== null;
},
resetEnvRow(row) {
const { [this.envDraftId(row)]: _dropped, ...rest } = this.envDrafts;
this.envDrafts = rest;
},
envSecretOptions(row) {
const options = secretOptions.call(this);
const current = this.envRow(row).secret;
if (current && !options.some((option) => option.name === current)) {
return [...options, { name: current, label: t`${current} (não encontrado)` }];
}
return options;
},
async saveEnvRow(row) {
const state = this.envRow(row);
const { renameTo, newValue } = envRowChanges(row, state);
if (renameTo === null && newValue === null) return;
if (renameTo !== null && !validEnvKey(renameTo)) {
this.store.toastError(t`Nome inválido: "${renameTo}" — use letras, dígitos e _, sem começar por dígito`);
return;
}
if (newValue?.Secret !== undefined && !newValue.Secret) {
this.store.toastWarn(t("escolha um secret"));
return;
}
const lines = [];
if (newValue?.Secret !== undefined) lines.push(t`passa a usar o secret "${newValue.Secret}"`);
else if (newValue && row.isSecret) lines.push(t`deixa de usar o secret "${row.secretName}" e passa a ter o valor digitado`);
else if (newValue) lines.push(t("o valor muda para o que foi digitado"));
if (renameTo !== null) lines.push(t`a chave muda de "${row.key}" para "${renameTo}"`);
if (!confirm(t`Salvar a variável "${row.key}"?` + "\n\n• " + lines.join("\n• ") + "\n\n" + (footer ? footer() : t("Vale no próximo deploy.")))) return;
const where = scope.call(this);
if (newValue) {
const valueResponse = await this.store.api.rpcChecked({ EnvValueSet: { ...where, key: row.key, value: newValue } });
if (!valueResponse.ok) {
this.store.toastResult(valueResponse);
return;
}
}
if (renameTo !== null) {
const renameResponse = await this.store.api.rpcChecked({ EnvKeyRename: { ...where, from: row.key, to: renameTo } });
if (!renameResponse.ok) {
this.store.toastResult(renameResponse);
await afterSave.call(this);
return;
}
}
this.resetEnvRow(row);
this.store.toastOk(t`variável ${renameTo ?? row.key} salva`);
await afterSave.call(this);
},
};
}