Files
skills/orchestrator/templates/page-alerts.js
T
Kevin Veen-Birkenbach f437c26b05 Merge branch 'feature/hub-notification-channels' into unify/skills-20261114
# Conflicts:
#	orchestrator/templates/page-alerts.js
#	orchestrator/templates/page-screens.js
#	orchestrator/templates/page-speech.js
2026-11-14 22:00:05 +00:00

108 lines
4.5 KiB
JavaScript

const beeps = new Set();
function silenceBeeps() {
for (const oscillator of beeps) {
oscillator.stop();
}
beeps.clear();
}
function play(kind, volume) {
const sound = context();
if (!sound) {
return;
}
const level = Math.max(volume * 0.5, 0.0002);
duck(Math.max(...TONES[kind].map(([, at, length]) => at + length)));
for (const [frequency, at, length] of TONES[kind]) {
const oscillator = sound.createOscillator();
const gain = sound.createGain();
const start = sound.currentTime + at;
oscillator.type = kind === "mayday" ? "square" : "sine";
oscillator.frequency.value = frequency;
gain.gain.setValueAtTime(0.0001, start);
gain.gain.exponentialRampToValueAtTime(level, start + 0.02);
gain.gain.exponentialRampToValueAtTime(0.0001, start + length);
oscillator.connect(gain).connect(sound.destination);
oscillator.addEventListener("ended", () => beeps.delete(oscillator), {once: true});
beeps.add(oscillator);
oscillator.start(start);
oscillator.stop(start + length + 0.05);
}
}
function notifyLabel() {
const button = document.getElementById("notify");
if (!button) {
return;
}
const permission = "Notification" in window ? Notification.permission : "unsupported";
const labels = {granted: "Notifications on", denied: "Notifications blocked", default: "Enable notifications", unsupported: "Browser has no notifications"};
relabel(button, "bi-bell", labels[permission] || labels.default);
button.disabled = permission !== "default";
}
/**
* @param {object} before the channels of the settings the page held until now.
*/
function syncChannels(before) {
if (before.sounds && !settings.channels.sounds) {
silenceBeeps();
}
if (before.aloud && !settings.channels.aloud) {
cancelSpeech();
}
drawTitle();
}
async function askNotify() {
if ("Notification" in window) {
await Notification.requestPermission();
}
notifyLabel();
}
/**
* @param {boolean} [resumed] whether this state is the first answer after the page was offline.
*/
function announce(resumed) {
const arrived = !resumed && known.now !== null && state.now - known.now <= LIVE_GAP;
const alarms = new Map(alarmRows().map((row) => [`${row.ts}|${row.from}`, row]));
const radio = new Set(state.radio.map((row) => `${row.ts}|${row.from}|${row.text.slice(0, 40)}`));
if (known.inbox !== null && live()) {
const fresh = state.inbox.filter((item) => !known.inbox.has(keyOf(item)));
const newAlarms = [...alarms].filter(([key]) => !known.alarms.has(key)).map(([, row]) => row);
const newRadio = state.radio.filter((row) => !known.radio.has(`${row.ts}|${row.from}|${row.text.slice(0, 40)}`));
const direct = newRadio.filter((row) => row.to.includes(HUMAN));
const present = {mayday: newAlarms.length > 0, review: fresh.some((item) => item.kind !== "operator"), direct: direct.length > 0, radio: newRadio.length > 0};
const kind = radioSet.mode === "silence" || !settings.channels.sounds ? null : Voice.beep(present, tones(), SOUND_KINDS.map(([id]) => id));
if (kind) {
play(kind, loudness(kind));
}
const lead = kind ? Math.max(...TONES[kind].map(([, at, length]) => at + length)) : 0;
window.setTimeout(() => speakRows(newRadio, arrived), lead * 1000);
if (settings.channels.browser && "Notification" in window && Notification.permission === "granted") {
for (const row of newAlarms) {
new Notification(`${row.class === "mayday" ? "Mayday" : "Pan-Pan"} from ${dash(row.from)}`, {body: row.text.slice(0, 200), tag: `alarm-${row.ts}`, requireInteraction: row.class === "mayday"});
}
for (const item of fresh) {
new Notification(`${ITEM_LABELS[item.kind]} waiting`, {body: item.title.slice(0, 200), tag: `item-${keyOf(item)}`, requireInteraction: item.kind === "ask"});
}
for (const row of direct) {
new Notification(`Radio from ${dash(row.from)}`, {body: row.text.slice(0, 200), tag: `direct-${row.ts}`});
}
}
}
known.inbox = new Set(state.inbox.map(keyOf));
known.alarms = new Set(alarms.keys());
known.radio = radio;
known.now = state.now;
for (const batch of state.pending) {
if (!known.batches.has(batch.id)) {
known.batches.add(batch.id);
const source = batch.source.startsWith("rule:") ? "Rule" : "Grant";
undoToast(batch, `${source}: ${batch.items.map((item) => short(item.title)).join(", ")}`);
}
}
}