Upstream/public/js/screens/settings.js
jvanvik b18645dca1 Phase 2: client military/social screens, WebSocket push, trade + endgame, review findings pending
Built: units/movements/reports/send/expansion screens, alliance/messages/rankings screens, ws client + server /ws push
(docs/PROTOCOL.md), Internet exchange trade, backbone endgame + world end, shared reports, buildDone relative levels.
Tested: npm test 175/175. Two adversarial reviews found issues (trade-leg recall exploit, __proto__ via validate.js,
rankings DoS, static headers, ...): fixes follow in the next commit.
NOT done: bots/balance, e2e suite, real-device checks.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Q2MJaQ5LXAHzGnTDgceT14
2026-10-11 19:05:03 +02:00

84 lines
4.6 KiB
JavaScript

'use strict';
/* Settings: language, account info, device tokens (list + revoke) and logout. */
(function () {
const { h, clear, icon, toast, openSheet } = UI;
const I = UpstreamI18n;
let box, devicesEl, tokens = null, tokenErr = null;
function kv(label, value) { return h('div', { class: 'kv' }, h('span', { class: 'k', text: label }), h('span', { class: 'v', text: value })); }
async function loadTokens() {
try { tokens = (await App.api.get('/auth/tokens')).tokens; tokenErr = null; } catch (e) { tokenErr = App.errMsg(e); }
drawDevices();
}
function drawDevices() {
if (!devicesEl) return;
clear(devicesEl);
if (tokenErr) { devicesEl.appendChild(h('p', { class: 'form-error', role: 'alert', text: tokenErr })); return; }
if (!tokens) { devicesEl.appendChild(h('p', { class: 'empty', text: App.t('common.loading') })); return; }
const now = App.now();
for (const t of tokens) {
devicesEl.appendChild(h('li', { class: 'device' },
h('span', { class: 'row-ic' }, icon('device')),
h('span', { class: 'row-main' },
h('span', { class: 'row-title' }, h('b', { text: t.device || App.t('settings.device.unnamed') }), t.current ? h('span', { class: 'pill accent', text: App.t('settings.device.current') }) : null),
h('span', { class: 'row-sub', text: App.t('settings.device.created', { t: App.fmt.ago(now - t.createdAt) }) + ' · ' + App.t('settings.device.lastUsed', { t: App.fmt.ago(now - t.lastUsedAt) }) })),
t.current ? null : h('button', { class: 'btn small danger', type: 'button', onclick: (ev) => revoke(t, ev.currentTarget) }, App.t('settings.device.revoke'))));
}
}
async function revoke(t, btn) {
btn.disabled = true;
try {
await App.api.del('/auth/tokens/' + t.id);
toast(App.t('settings.device.revoked'), 'ok');
} catch (e) { if (e.code !== 'not_found') toast(App.errMsg(e), 'error'); }
loadTokens();
}
function confirmLogout() {
const sheet = openSheet({
title: App.t('settings.logout'), icon: 'logout', closeLabel: App.t('common.close'),
body: h('div', { class: 'detail' }, h('p', { class: 'detail-desc', text: App.t('settings.logout.confirm') }),
h('div', { class: 'btn-row' },
h('button', { class: 'btn', type: 'button', onclick: () => sheet.close() }, App.t('common.cancel')),
h('button', { class: 'btn danger', type: 'button', onclick: async () => {
try { await App.api.post('/auth/logout', {}, { retries: 1 }); } catch (e) { /* the token may already be gone */ }
sheet.close();
App.clearSession();
App.authNotice = null;
App.go('auth');
} }, icon('logout'), App.t('settings.logout')))),
});
}
function draw() {
clear(box);
const st = App.state;
const op = st.operator, w = st.world;
devicesEl = h('ul', { class: 'device-list' });
box.appendChild(h('div', { class: 'page' },
h('section', { class: 'card' }, h('div', { class: 'card-head' }, h('h2', { text: App.t('settings.language') })),
h('div', { class: 'seg', role: 'group', 'aria-label': App.t('settings.language') },
I.LANGS.map((l) => h('button', { class: 'seg-btn', type: 'button', 'aria-pressed': String(st.lang === l), onclick: () => App.setLang(l) }, App.t('settings.lang.' + l))))),
h('section', { class: 'card' }, h('div', { class: 'card-head' }, h('h2', { text: App.t('settings.account') })),
st.user ? kv(App.t('settings.username'), st.user.username) : null,
op ? kv(App.t('settings.operator'), op.name) : null,
op ? kv(App.t('settings.as'), I.fmtAs(op.asNumber)) : null,
w ? kv(App.t('settings.world'), App.t('settings.worldInfo', { name: w.name, speed: w.speed, r: w.mapRadius })) : null,
h('button', { class: 'btn danger wide', type: 'button', onclick: confirmLogout }, icon('logout'), App.t('settings.logout'))),
h('section', { class: 'card' }, h('div', { class: 'card-head' }, h('h2', { text: App.t('settings.devices') })),
h('p', { class: 'dimmer small', text: App.t('settings.devices.hint') }), devicesEl),
h('section', { class: 'card' }, h('div', { class: 'card-head' }, h('h2', { text: App.t('settings.about') })),
h('p', { class: 'dimmer small', text: App.t('settings.about.text') }))));
drawDevices();
}
App.registerScreen({
id: 'settings', titleKey: 'screen.settings', navKey: 'nav.settings', icon: 'gear', hud: false, navGroup: 'more',
render(container) { box = container; App.on('session', () => { tokens = null; }); },
onShow() { draw(); loadTokens(); },
});
}());