window.gentSettings = (() => {
const {byId,el,button,request,notice,present} = window.gentUI;
const form = byId("preferences"), editor = byId("editor"), text = byId("config-text");
const groups = [
["General", [
["login","Start Gent Desktop at graphical login"], ["updates","Automatically update Gent"],
["discovery","Discover and publish native chats"]]],
["Babysitter", [
["babysitter","Resume goals after provider failures"],
["ordinary_babysitter","Continue ordinary chats after provider errors","Sends “please continue” in eligible Codex chats on this node."]]],
["Network", [
["direct","Prefer direct connections"], ["udp","Allow UDP for direct connections"],
["helper","Offer this node as a transport helper"]]],
];
const switches = new Map(), edit = button("Edit full configuration…", () => perform("edit"));
const channel = el("select"), retry = button("Retry request", () => channelRequest("retry_release_channel"));
const channelNotice = el("p",null,"muted");
let channelView = {}, channelBusy = false, values = {}, busy = false, draft = false;
function sync() {
for (const [key, box] of switches) {
if (!busy) box.checked = values[key] === true;
box.disabled = busy || typeof values[key] !== "boolean";
}
edit.disabled = busy;
for (const id of ["discard","save","publish","config-text"]) byId(id).disabled = busy;
editor.hidden = !draft; form.hidden = draft;
}
function perform(op, fields) {
if (busy) return;
busy = true; sync(); notice("settings-notice","");
request(op,fields).catch(() => receive({error:"Request could not be sent."}));
}
function syncChannel() {
channel.value = channelView.current || "";
channel.disabled = channelBusy || !["stable","beta"].includes(channelView.current) || !channelView.can_set;
retry.hidden = !channelView.retry; retry.disabled = channelBusy;
}
function channelRequest(op, fields) {
if (channelBusy) return;
channelBusy = true; syncChannel();
if (op !== "release_channel") channelNotice.textContent = op === "retry_release_channel" ? "Retrying the same request…" : "Requesting channel change…";
request(op,fields).catch(() => receiveChannel({notice:"Channel request could not be sent."}));
}
function receiveChannel(reply) {
channelBusy = false;
if (Object.hasOwn(reply,"current")) channelView = reply;
channelNotice.textContent = reply.notice || reply.error || "";
syncChannel();
}
for (const [title, options] of groups) {
const card = el("section",null,"card"); card.append(el("h2",title));
for (const [key,name,hint] of options) {
const row=el("div",null,"preference"), label=el("label",name), box=el("input");
box.type="checkbox"; box.id="pref-"+key; label.htmlFor=box.id;
if (hint) label.append(el("span",hint,"muted"));
box.addEventListener("change",() => perform("set",{key,enabled:box.checked}));
switches.set(key,box); row.append(label,box); card.append(row);
if (key === "updates") {
const section=el("div",null,"preference"), label=el("label","Cluster release channel");
channel.id="release-channel"; label.htmlFor=channel.id;
label.append(el("span","Switching requests one update per node, even when automatic updates are paused.","muted"));
channel.append(new Option("Unavailable",""),new Option("Stable","stable"),new Option("Beta","beta"));
channel.addEventListener("change",() => {
const choice = channel.value;
if (["stable","beta"].includes(choice) && choice !== channelView.current) channelRequest("set_release_channel",{channel:choice});
});
channelNotice.setAttribute("role","status");
section.append(label,channel,retry); card.append(section,channelNotice);
}
}
if (title==="Network") card.append(el("p","Network changes take effect when the daemon restarts.","muted"));
form.append(card);
}
form.append(edit);
byId("discard").onclick = () => perform("discard");
byId("save").onclick = () => perform("save",{text:text.value,publish:false});
byId("publish").onclick = () => present("Publish shared settings?",[],"Save and publish", () => {
perform("save",{text:text.value,publish:true});
},["Publish these shared settings to the cluster?"],true);
function receive(reply) {
busy = false;
if (reply.values) values = reply.values;
if (typeof reply.document === "string") { draft = true; text.value = reply.document; }
else if (!reply.error && (reply.saved || !reply.values)) { draft = false; text.value = ""; }
sync();
notice("settings-notice",reply.error || reply.notice,!!reply.error);
if (typeof reply.document === "string" && !byId("settings").hidden) text.focus();
}
function load() {
if (!draft) { perform("settings"); channelRequest("release_channel"); }
}
sync();syncChannel();
return {receive,receiveChannel,load,poll() { if (!draft && channelView.pending) channelRequest("release_channel"); }};
})();