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 apply = button("Switch channel", () => {
if (!apply.disabled) channelRequest("set_release_channel",{channel:channel.value});
});
const channelNotice = el("p",null,"muted");
const feedback = byId("settings-notice");
let channelView = {}, channelBusy = false, values = null, busy = null, draft = false, enrolled;
let requestFocus;
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 || !values || enrolled === false;
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;
requestFocus = document.activeElement;
busy = op; sync(); notice("settings-notice","");
if (op === "set") switches.get(fields.key).closest(".preference").after(feedback);
else if (op === "settings") form.before(feedback);
else if (op === "edit") edit.after(feedback);
else editor.after(feedback);
request(op,fields).catch(() => receive({error:"Request could not be sent."}));
}
function syncChannel() {
channel.disabled = channelBusy || !["stable","beta"].includes(channelView.current) || !channelView.can_set;
apply.hidden = !channelView.can_set;
apply.disabled = channel.disabled || !["stable","beta"].includes(channel.value) || channel.value === channelView.current;
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;
channelView = Object.hasOwn(reply,"current") ? reply : {pending:channelView.pending};
channel.value = channelView.current || "";
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"));
const unavailable = new Option("Unavailable",""); unavailable.disabled = true;
channel.append(unavailable,new Option("Stable","stable"),new Option("Beta","beta"));
channel.addEventListener("change",() => {
syncChannel();
channelNotice.textContent = apply.disabled ? channelView.notice || "" : "Current channel: "+channelView.current+".";
});
channelNotice.setAttribute("role","status");
const controls=el("div",null,"actions"); controls.append(channel,apply,retry);
section.append(label,controls); 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) {
const operation = busy, wasDraft = draft;
const keepFocus = document.activeElement === requestFocus || document.activeElement === document.body;
busy = null;
if (operation === "settings" && reply.error) values = null;
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 (keepFocus && !byId("settings").hidden) {
if (!wasDraft && draft) text.focus();
else if (wasDraft && !draft) edit.focus();
else if (operation !== "settings" && requestFocus && byId("settings").contains(requestFocus)) requestFocus.focus({preventScroll:true});
if (operation === "set" && reply.error) feedback.scrollIntoView({block:"nearest"});
}
requestFocus = null;
}
function load() {
if (!draft) { perform("settings"); channelRequest("release_channel"); }
}
sync();syncChannel();
return {receive,receiveChannel,load,enrollment(value) {
enrolled = value;
byId("settings-setup").hidden = value;
sync();
},poll() { if (!draft && channelView.pending) channelRequest("release_channel"); }};
})();