Format amounts, guard the loading state, and translate the new errors
Amounts were rendered by bare sats/SATS_PER_PLM division, so binary floating-point artefacts reached the UI — a 0.7 PLM jackpot could display as 0.7000000000000001 (B-22). formatPlm() in app.js and fmtPlm() in admin.js route every display site through Intl.NumberFormat with the already-resolved language. Input fields deliberately keep the raw value: a grouped, localized string would break parseFloat, and amounts sent to the server still go through Math.round(x * SATS_PER_PLM). withLoading kept a snapshot of the button's markup and restored it in finally, but refreshMe() is fired from the SSE handler, the poll chain, placeBet, withdraw and showDashboard, all sharing #refresh-btn. Two overlapping calls made the second snapshot the *loading* label and then restore it permanently, leaving the button stuck on "Aggiornamento…" (B-23). The in-flight promise now lives in a WeakMap keyed by the button, so a nested call awaits the existing one and only the outermost call touches the markup. The registration form mirrors the constraints the server now enforces (minlength/pattern/required) and register() pre-checks the password length, so the failure is immediate and translated instead of a generic 422 (B-12). Five new error codes are translated in all 7 languages — broadcast_failed, amount_below_dust_limit, withdrawal_to_own_address, internal_error, guide_unavailable — keeping the key sets identical, as the i18n contract in CLAUDE.md requires (verified: 123 keys per language). Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
7c4e9983ea
commit
dc4d5761df
4 files changed
+104
-23
No files matched your search
+54
-16
@@ -1,5 +1,19 @@
|
||||
const SATS_PER_PLM = 100000000;
|
||||
|
||||
// Every amount displayed goes through here. A bare sats/SATS_PER_PLM division
|
||||
// leaks binary floating-point artefacts into the UI — a 0.7 PLM jackpot rendering
|
||||
// as 0.7000000000000001 (B-22). Trailing zeros are trimmed so ordinary amounts
|
||||
// stay readable, and grouping follows the selected language.
|
||||
// Amounts sent *to* the server must NOT use this — they keep going through
|
||||
// Math.round(x * SATS_PER_PLM), since this returns a formatted string.
|
||||
function formatPlm(sats) {
|
||||
if (sats === null || sats === undefined || Number.isNaN(sats)) return '—';
|
||||
return new Intl.NumberFormat(currentDateLocale(), {
|
||||
minimumFractionDigits: 0,
|
||||
maximumFractionDigits: 8,
|
||||
}).format(sats / SATS_PER_PLM);
|
||||
}
|
||||
|
||||
let token = localStorage.getItem('plm_token');
|
||||
let username = localStorage.getItem('plm_username');
|
||||
let address = localStorage.getItem('plm_address');
|
||||
@@ -17,17 +31,35 @@ function toast(message, type) {
|
||||
// <span data-i18n=...>, both of which a textContent round-trip would flatten away
|
||||
// — losing the icon for good and, worse, stripping the data-i18n hook so the
|
||||
// button would stop following later language changes.
|
||||
//
|
||||
// Only the outermost call owns the markup. refreshMe() is fired from the SSE
|
||||
// handler, the poll chain, placeBet, withdraw and showDashboard, all sharing
|
||||
// #refresh-btn: two overlapping calls used to make the second one snapshot the
|
||||
// *loading* label and then restore it permanently, leaving the button stuck on
|
||||
// "Aggiornamento…" (B-23). A nested call now just awaits the one already running.
|
||||
const _loadingByButton = new WeakMap();
|
||||
|
||||
async function withLoading(button, label, fn) {
|
||||
const inFlight = _loadingByButton.get(button);
|
||||
if (inFlight) {
|
||||
await inFlight.catch(() => {}); // its own caller reports the failure
|
||||
return fn();
|
||||
}
|
||||
const original = button.innerHTML;
|
||||
button.disabled = true;
|
||||
button.textContent = label;
|
||||
try {
|
||||
await fn();
|
||||
} finally {
|
||||
button.disabled = false;
|
||||
button.innerHTML = original;
|
||||
applyStaticTranslations(button); // the snapshot may predate a language switch made while loading
|
||||
}
|
||||
const run = (async () => {
|
||||
try {
|
||||
await fn();
|
||||
} finally {
|
||||
button.disabled = false;
|
||||
button.innerHTML = original;
|
||||
applyStaticTranslations(button); // the snapshot may predate a language switch made while loading
|
||||
_loadingByButton.delete(button);
|
||||
}
|
||||
})();
|
||||
_loadingByButton.set(button, run);
|
||||
return run;
|
||||
}
|
||||
|
||||
const REQUEST_TIMEOUT_MS = 15000;
|
||||
@@ -83,7 +115,7 @@ function errorParams(params) {
|
||||
const out = { ...(params || {}) };
|
||||
for (const [key, value] of Object.entries(params || {})) {
|
||||
if (key.endsWith('_sats') && typeof value === 'number') {
|
||||
out[key.slice(0, -5) + '_plm'] = value / SATS_PER_PLM;
|
||||
out[key.slice(0, -5) + '_plm'] = formatPlm(value);
|
||||
}
|
||||
}
|
||||
return out;
|
||||
@@ -292,7 +324,7 @@ function renderPersistedResult(result) {
|
||||
setRoundInfoVisible(false);
|
||||
setResultBoxVisible(
|
||||
true,
|
||||
result.won ? t('result.win', { amount: result.amount_sats / SATS_PER_PLM }) : t('result.lose'),
|
||||
result.won ? t('result.win', { amount: formatPlm(result.amount_sats) }) : t('result.lose'),
|
||||
result.won ? 'win' : 'lose'
|
||||
);
|
||||
}
|
||||
@@ -328,7 +360,7 @@ async function checkLastRoundResult() {
|
||||
persistResult(data.round_id, data.won, data.amount_sats);
|
||||
renderPersistedResult({ won: data.won, amount_sats: data.amount_sats });
|
||||
if (data.won) {
|
||||
const won = data.amount_sats / SATS_PER_PLM;
|
||||
const won = formatPlm(data.amount_sats);
|
||||
toast(t('toast.roundWon', { id: data.round_id, amount: won }), 'success');
|
||||
refreshMe();
|
||||
}
|
||||
@@ -395,7 +427,7 @@ function renderBetButton() {
|
||||
if (btn.disabled) return;
|
||||
btn.textContent = betAmountSats === null
|
||||
? t('bet.buttonNoAmount')
|
||||
: t('bet.button', { amount: betAmountSats / SATS_PER_PLM });
|
||||
: t('bet.button', { amount: formatPlm(betAmountSats) });
|
||||
}
|
||||
|
||||
function showNormalState() {
|
||||
@@ -422,7 +454,7 @@ async function refreshRound() {
|
||||
document.getElementById('round-players').textContent = data.participant_count;
|
||||
const jackpotEl = document.getElementById('round-jackpot');
|
||||
const jackpotValue = data.jackpot_sats / SATS_PER_PLM;
|
||||
jackpotEl.textContent = jackpotValue;
|
||||
jackpotEl.textContent = formatPlm(data.jackpot_sats);
|
||||
if (lastJackpotValue !== null && jackpotValue !== lastJackpotValue) {
|
||||
jackpotEl.classList.remove('jackpot-bump');
|
||||
void jackpotEl.offsetWidth; // restart the animation
|
||||
@@ -464,7 +496,7 @@ async function refreshRound() {
|
||||
if (!alreadyKnown) {
|
||||
persistResult(data.round_id, won, data.winner_amount_sats);
|
||||
if (won) {
|
||||
const wonAmount = (data.winner_amount_sats / SATS_PER_PLM);
|
||||
const wonAmount = formatPlm(data.winner_amount_sats);
|
||||
toast(t('toast.roundWon', { id: data.round_id, amount: wonAmount }), 'success');
|
||||
refreshMe(); // the win toast is useless if the balance card still shows the pre-payout amount
|
||||
}
|
||||
@@ -555,6 +587,12 @@ async function register() {
|
||||
toast(t('toast.passwordMismatch'), 'error');
|
||||
return;
|
||||
}
|
||||
// Mirrors what the server now enforces (app/auth/routes.py's RegisterRequest),
|
||||
// so the failure is immediate and translated instead of a generic 422 (B-12).
|
||||
if (p.length < 8) {
|
||||
toast(t('toast.passwordTooShort'), 'error');
|
||||
return;
|
||||
}
|
||||
await withLoading(btn, t('loading.creating'), async () => {
|
||||
try {
|
||||
const data = await call('POST', '/auth/register', { username: u, password: p });
|
||||
@@ -657,7 +695,7 @@ let myBalanceSats = 0; // confirmed, spendable balance — what withdrawals/bets
|
||||
// has_pending is true so it's clear the figure isn't final yet.
|
||||
function setBalanceDisplay(elementId, pendingBalanceSats, hasPending) {
|
||||
const el = document.getElementById(elementId);
|
||||
el.textContent = pendingBalanceSats / SATS_PER_PLM;
|
||||
el.textContent = formatPlm(pendingBalanceSats);
|
||||
el.classList.toggle('balance-pending', hasPending);
|
||||
el.classList.toggle('balance-confirmed', !hasPending);
|
||||
}
|
||||
@@ -670,14 +708,14 @@ async function refreshMe() {
|
||||
myUserId = data.id;
|
||||
myBalanceSats = data.balance_sats;
|
||||
setBalanceDisplay('dash-balance', data.pending_balance_sats, data.has_pending);
|
||||
document.getElementById('navbar-balance').textContent = (data.pending_balance_sats / SATS_PER_PLM) + ' PLM';
|
||||
document.getElementById('navbar-balance').textContent = formatPlm(data.pending_balance_sats) + ' PLM';
|
||||
document.getElementById('navbar-balance').classList.toggle('balance-pending', data.has_pending);
|
||||
document.getElementById('navbar-balance').classList.toggle('balance-confirmed', !data.has_pending);
|
||||
document.getElementById('profile-username').textContent = data.username;
|
||||
document.getElementById('profile-address').textContent = data.address;
|
||||
setBalanceDisplay('profile-balance', data.pending_balance_sats, data.has_pending);
|
||||
document.getElementById('profile-created-at').textContent = new Date(data.created_at).toLocaleDateString(currentDateLocale());
|
||||
document.getElementById('wd-full-amount-value').textContent = data.balance_sats / SATS_PER_PLM;
|
||||
document.getElementById('wd-full-amount-value').textContent = formatPlm(data.balance_sats);
|
||||
if (document.getElementById('wd-full-amount').checked) {
|
||||
document.getElementById('wd-amount').value = data.balance_sats / SATS_PER_PLM;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user