Files
magpie/internal/gui/assets/balfound.js
T
yetone b95a78d43c gui: the editor says where magpie found a relay's balance, with Use
A relay added by hand with no Balance URL has its balance found by
magpie, but nothing in the window said so. A user who saw a wrong amount
couldn't tell it was a guess or where to put it right. Under the Balance
URL, in Advanced, the editor now shows "Found by magpie: <url> <field>"
with Use, which copies the query into the Balance URL and Balance field
to be changed. Check balance draws the line again from what it found,
and when it found nothing it says "No balance at /v1/usage or
/api/usage/token" instead of "No Balance URL to ask".

/api/providers carries balanceFound and balanceFoundField. The check's
reply carries looked, found and foundField. The line is in balfound.js,
loaded when the editor draws a relay's balance with no Balance URL. Its
one call and the check's text in app.js are paid for by three provider
editor comments cut down, so there is no raise: the core is 1,533,452,
350 under its budget. zh, zh-TW, ja and de are translated.

Run on mote:
- go test -count=1 -timeout 45m ./internal/gui: ok, except
  TestMainWindowCorners/macOS_26_SDK, mote's macOS 27 (#3949).
- GUI, both engines: balance-found (new, 900px and 420px, every
  language) 22/22. balance-url-first, balance-fix, balance-method,
  balance-field-default, key-balance-card and old-webkit: ok.
  i18n-keys: "a plugin's words are its own" is red (#5327), as on main.

Broken by hand: with the import taken out of app.js, balance-found is
0/11 in chromium.

Not tried: the whole GUI suite at this head (run after the push), and
the real app with a real relay.
2026-11-05 14:46:23 +08:00

39 lines
1.8 KiB
JavaScript

// A relay added by hand with no Balance URL has its balance found by
// magpie where relays tell a key it (provider/balance_probe.go). Under the
// Balance URL the editor says where, with Use to copy it into the fields,
// so an amount read at the wrong query can be put right. app.js imports
// this when the editor draws a relay's balance with no Balance URL.
let balFoundAt = null;
window.balFound = function (box, p, draft, bal, pathOf) {
const line = el("div", "bal-found");
line.hidden = true;
balFoundAt = { line, draft, bal, pathOf };
box.append(line);
bal.addEventListener("input", () => { line.hidden = !line.dataset.url || !!bal.value.trim(); });
window.balFoundShow({ found: p.balanceFound, foundField: p.balanceFoundField });
};
// what the editor's Check balance found (app.js), drawn again
window.balFoundShow = function (r) {
const s = balFoundAt;
if (!s || !s.line.isConnected) return;
const { line, draft, bal, pathOf } = s;
line.replaceChildren();
line.dataset.url = r.found || "";
line.hidden = !r.found || !!(draft.balanceURL || "").trim();
if (!r.found) return;
const txt = el("span", "", t("Found by magpie: {url}", { url: r.found + (r.foundField ? " " + r.foundField : "") }));
txt.title = t("magpie asked this relay where relays tell a key its balance, and reads it here. If the amount is wrong, put in your own Balance URL and field; magpie looks again each day");
const use = el("button", "text action", t("Use"));
use.type = "button";
use.title = t("Put this query in the Balance URL and Balance field, to change it");
use.onclick = () => {
draft.balanceURL = bal.value = r.found;
draft.balancePath = r.foundField || "";
const f = pathOf();
if (f) { f.value = draft.balancePath; f.placeholder = balanceFieldOf(r.found); }
line.hidden = true;
bal.focus();
};
line.append(txt, use);
};