Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Mzbb6zeFTjT6Rp8b92AVkA
103 lines
5.6 KiB
JavaScript
103 lines
5.6 KiB
JavaScript
// Impostazioni, Notifiche (spec §13.3): which events notify, Windows notifications (desktop only), Telegram with the
|
|
// bot token (masked, never shown again once saved, never logged) and the chat identifier, and «Invia una prova».
|
|
// Telegram: one HTTPS call from the server (TelegramAlertChannel); where no sender is registered (demonstration) the
|
|
// page says that no message leaves.
|
|
|
|
import { api } from '../core/api.js';
|
|
import { h, icon, fill } from '../core/dom.js';
|
|
import { t } from '../core/text.js';
|
|
import { label, showError, toast, toggle } from '../core/ui.js';
|
|
import { guardChanges, heading } from './settings.js';
|
|
|
|
const EVENTS = ['verification', 'disconnection', 'throttle', 'backfillDone', 'diskLow', 'unknownFormat'];
|
|
|
|
export default async function notifications(page, box) {
|
|
let data = await api('/api/settings/notifications', { signal: page.signal });
|
|
let form = fromData(data);
|
|
let original = JSON.stringify(form);
|
|
const dirty = () => JSON.stringify(form) !== original;
|
|
guardChanges(page, dirty);
|
|
|
|
function fromData(d) {
|
|
return { events: { ...d.events }, windows: d.windows, chat: d.telegramChatId || '', token: '', removeToken: false };
|
|
}
|
|
|
|
const tokenInput = h('input.dati', { type: 'password', id: 'gettone', autocomplete: 'off', spellcheck: 'false' });
|
|
const chatInput = h('input.dati', { type: 'text', id: 'chat', autocomplete: 'off', spellcheck: 'false', inputmode: 'text' });
|
|
tokenInput.addEventListener('input', () => { form.token = tokenInput.value; form.removeToken = false; drawSaveBar(); drawTokenNote(); });
|
|
chatInput.addEventListener('input', () => { form.chat = chatInput.value; drawSaveBar(); });
|
|
const tokenNote = h('p.aiuto.piccolo');
|
|
const removeButton = h('button.btn.nudo.piccolo', { type: 'button', onclick: () => { form.removeToken = true; form.token = ''; tokenInput.value = ''; drawSaveBar(); drawTokenNote(); } }, icon('x', 's'), t('impostazioni.notifiche.togliGettone'));
|
|
|
|
function drawTokenNote() {
|
|
tokenInput.placeholder = data.telegramTokenSet && !form.removeToken ? t('impostazioni.notifiche.gettoneSalvato') : t('impostazioni.notifiche.gettoneVuoto');
|
|
removeButton.hidden = !data.telegramTokenSet || form.removeToken;
|
|
tokenNote.textContent = form.removeToken ? t('impostazioni.notifiche.gettoneTolto') : t('impostazioni.notifiche.gettoneNota');
|
|
}
|
|
|
|
async function test(channel) {
|
|
try {
|
|
const result = await api('/api/settings/notifications/test', { method: 'POST', body: { channel } });
|
|
toast(result.message, result.sent ? '' : 'errore');
|
|
} catch (error) {
|
|
showError(error);
|
|
}
|
|
}
|
|
|
|
function drawSaveBar() {
|
|
if (!dirty()) {
|
|
page.saveBar(null);
|
|
return;
|
|
}
|
|
page.saveBar(h('div', { style: { display: 'contents' } },
|
|
h('span.nota', t('impostazioni.modifiche')),
|
|
h('div.destra',
|
|
h('button.btn', { type: 'button', onclick: () => { form = fromData(data); draw(); } }, t('impostazioni.annulla')),
|
|
h('button.btn.primo', { type: 'button', onclick: save }, icon('check'), t('impostazioni.salva')))));
|
|
}
|
|
|
|
async function save() {
|
|
try {
|
|
data = await api('/api/settings/notifications', {
|
|
method: 'PUT',
|
|
body: { events: form.events, windows: form.windows, telegramChatId: form.chat.trim() || null, telegramToken: form.token.trim() || null, removeTelegramToken: form.removeToken },
|
|
});
|
|
form = fromData(data);
|
|
original = JSON.stringify(form);
|
|
draw();
|
|
toast(t('impostazioni.salvate'));
|
|
} catch (error) {
|
|
showError(error);
|
|
}
|
|
}
|
|
|
|
function draw() {
|
|
tokenInput.value = form.token;
|
|
chatInput.value = form.chat;
|
|
drawTokenNote();
|
|
fill(box,
|
|
h('div.sezione',
|
|
heading(t('impostazioni.notifiche.quando'), t('impostazioni.notifiche.quandoAiuto')),
|
|
h('div.elenco-leve', EVENTS.map((key) => h('div.leva-riga',
|
|
toggle({ checked: form.events[key], label: t(`impostazioni.notifiche.eventi.${key}`), onchange: (v) => { form.events[key] = v; drawSaveBar(); } }),
|
|
h('p.aiuto.piccolo', t(`impostazioni.notifiche.eventiAiuto.${key}`)))))),
|
|
h('div.sezione',
|
|
h('div.testa-sez', h('h2.sez', t('impostazioni.notifiche.windows'), ' ', data.windowsAvailable ? null : label(t('impostazioni.notifiche.soloWindows'))),
|
|
h('p.aiuto', t('impostazioni.notifiche.windowsTesto'))),
|
|
h('div.riga-azione',
|
|
toggle({ checked: form.windows && data.windowsAvailable, disabled: !data.windowsAvailable, label: t('impostazioni.notifiche.windows'), onchange: (v) => { form.windows = v; drawSaveBar(); } }),
|
|
data.windowsAvailable ? h('button.btn.piccolo', { type: 'button', onclick: () => test('windows') }, icon('bell', 's'), t('impostazioni.notifiche.prova')) : null)),
|
|
h('div.sezione',
|
|
heading(t('impostazioni.notifiche.telegram'), t('impostazioni.notifiche.telegramAiuto')),
|
|
data.telegramAvailable ? null : h('div.avviso', { style: { marginBottom: '14px' } }, icon('info', 's'), h('span', t('impostazioni.notifiche.nonAncora'))),
|
|
h('div.campi-telegram',
|
|
h('div', h('label.etichetta', { for: 'gettone' }, t('impostazioni.notifiche.gettone')), h('div.riga-campo', h('label.campo', icon('key-round', 's'), tokenInput), removeButton), tokenNote),
|
|
h('div', h('label.etichetta', { for: 'chat' }, t('impostazioni.notifiche.chat')), h('label.campo', chatInput), h('p.aiuto.piccolo', t('impostazioni.notifiche.chatEsempio')))),
|
|
h('div.riga-azione', h('button.btn', { type: 'button', onclick: () => test('telegram') }, icon('bell', 's'), t('impostazioni.notifiche.prova')))));
|
|
chatInput.placeholder = t('impostazioni.notifiche.chatEsempio');
|
|
drawSaveBar();
|
|
}
|
|
|
|
draw();
|
|
}
|