Upstream/public/js/screens/alerts.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

87 lines
3.9 KiB
JavaScript

'use strict';
/* Alerts: the durable notification feed (GET /notifications). Newest first; what arrived since you last looked is marked. */
(function () {
const { h, clear, icon } = UI;
const PAGE = 100, MAX_PAGES = 5;
let box, listEl, statusEl, items = [], seenAtOpen = 0, loading = false, offTick = null;
function townName(id) {
const t = App.state.towns.find((x) => x.id === id);
return t ? t.name : '#' + id;
}
// One entry per notification type: icon + title + detail. Unknown types still show something readable.
function describe(n) {
const p = n.payload || {};
if (n.type === 'build_done') {
return { icon: 'b-' + p.type, title: App.t('alerts.build_done', { name: App.buildingName(p.type), level: p.level }), detail: App.t('alerts.build_done.in', { town: townName(p.townId) }), go: 'pop' };
}
// Other screens describe their own notification types: App.alertKinds[type] = (n, p) => ({ icon, title, detail, go, params })
const ext = App.alertKinds && App.alertKinds[n.type];
if (ext) return ext(n, p);
return { icon: 'bell', title: App.t('alerts.unknown', { type: n.type }), detail: '', go: null };
}
function draw() {
clear(listEl);
if (!items.length) { listEl.appendChild(h('p', { class: 'empty pad', text: App.t('alerts.empty') })); return; }
const now = App.now();
for (const n of items.slice().reverse()) {
const d = describe(n);
const isNew = n.id > seenAtOpen;
listEl.appendChild(h('li', null,
h('button', { class: 'alert' + (isNew ? ' new' : ''), type: 'button', onclick: () => { if (d.go) App.go(d.go, d.params); } },
h('span', { class: 'row-ic' }, icon(d.icon)),
h('span', { class: 'row-main' }, h('span', { class: 'row-title' }, h('b', { text: d.title }), isNew ? h('span', { class: 'pill accent', text: App.t('alerts.new') }) : null),
d.detail ? h('span', { class: 'row-sub', text: d.detail }) : null),
h('span', { class: 'when mono', dataset: { at: n.createdAt }, text: App.fmt.ago(now - n.createdAt) }))));
}
}
async function load() {
if (loading) return;
loading = true;
statusEl.hidden = true;
try {
let after = items.length ? items[items.length - 1].id : 0;
for (let page = 0; page < MAX_PAGES; page++) {
const r = await App.api.get('/notifications', { after, limit: PAGE });
items = items.concat(r.items);
if (r.next === null || !r.items.length) break;
after = r.next;
}
draw();
if (items.length) App.markSeen(items[items.length - 1].id); else App.markSeen(0);
} catch (e) {
statusEl.textContent = App.t('alerts.error') + ' ' + App.errMsg(e);
statusEl.hidden = false;
} finally { loading = false; }
}
App.registerScreen({
id: 'alerts', titleKey: 'screen.alerts', navKey: 'nav.alerts', icon: 'bell', hud: false, navGroup: 'more',
render(container) {
box = container;
listEl = h('ul', { class: 'alert-list' });
statusEl = h('p', { class: 'form-error', role: 'alert', hidden: true });
box.appendChild(h('div', { class: 'page' },
h('div', { class: 'card-head page-head' }, h('h1', { text: App.t('screen.alerts') }),
h('button', { class: 'icon-btn', type: 'button', 'aria-label': App.t('common.retry'), onclick: () => load() }, icon('refresh'))),
statusEl, listEl));
App.on('session', () => { items = []; });
},
onShow() {
seenAtOpen = App.getSeen();
box.querySelector('h1').textContent = App.t('screen.alerts');
draw();
load();
if (offTick) offTick();
offTick = App.onTick(() => {
const now = App.now();
for (const el of listEl.querySelectorAll('.when')) { const s = App.fmt.ago(now - Number(el.dataset.at)); if (el.textContent !== s) el.textContent = s; }
});
},
onHide() { if (offTick) offTick(); offTick = null; },
});
}());