Files

37 lines
1.3 KiB
JavaScript

const Topbar = {
_statusEl: null,
init() {
this._statusEl = document.getElementById('connection-status');
this._statusEl.style.cursor = 'pointer';
this._statusEl.addEventListener('click', () => {
if (!WS.connected) {
WS._reconnectDelay = 500;
WS._connect();
Toast.show(Lang.t('loading'), 'info', 1500);
}
});
WS.on('status', (status) => {
this._statusEl.className = `status-indicator ${status}`;
this._statusEl.querySelector('.status-text').textContent =
status === 'connected' ? Lang.t('connected') :
status === 'error' ? Lang.t('connectionError') : Lang.t('disconnected');
});
setInterval(() => this._refreshStatus(), 5000);
},
async _refreshStatus() {
try {
const status = await API.getStatus();
const wsStatus = status.ws_clients > 0 ? 'connected' : 'disconnected';
this._statusEl.className = `status-indicator ${wsStatus}`;
this._statusEl.querySelector('.status-text').textContent =
wsStatus === 'connected' ? `${Lang.t('connected')} (${status.ws_clients})` : Lang.t('disconnected');
} catch (e) { console.error(e); }
}
};
window.Topbar = Topbar;