Files
Alfredv2/alfred/app/static/js/alfred-reply-auto-close.js
T
2026-10-30 22:10:03 +01:00

63 lines
2.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* DONA_REPLY_AUTO_CLOSE_V1 — Ferme uniquement la réponse, jamais Hermes/Piper. */
(() => {
'use strict';
if (window.__donaReplyAutoCloseV1) return;
window.__donaReplyAutoCloseV1 = true;
const interval = Number(window.DONA_REPLY_CLOSE_SECONDS || 60);
const delay = Math.min(600, Math.max(10, Number.isFinite(interval) ? interval : 60)) * 1000;
let task = 0;
let speaking = false;
let focused = false;
const $ = (id) => document.getElementById(id);
const isFocused = () => Boolean($('alfredMainV1')?.classList.contains('is-focused'));
const clear = () => { if (task) window.clearTimeout(task); task=0; };
const canClose = () => isFocused() && !speaking && !document.querySelector('dialog[open], [role="dialog"][aria-modal="true"]');
const close = () => {
task = 0;
if (!canClose()) { schedule(); return; }
const back = $('avBack');
if (back) back.click();
};
function schedule() {
clear();
if (canClose()) task = window.setTimeout(close,delay);
}
function stateChanged() {
const now=isFocused();
if (!now) { clear(); focused=false; return; }
if (!focused) { focused=true; schedule(); }
}
function init() {
const main=$('alfredMainV1');
if (!main) { window.setTimeout(init,600); return; }
new MutationObserver(stateChanged).observe(main,{attributes:true,attributeFilter:['class']});
const content=$('content');
if (content) new MutationObserver(() => {if (isFocused()) schedule();})
.observe(content,{childList:true,subtree:true,characterData:true});
const socket=window.alfredSocket;
if (socket?.on) {
socket.on('alfred_state', (state) => {
const mode=String(state?.mode || '').toLowerCase();
const wasSpeaking=speaking;
speaking=(mode==='speaking' || mode==='thinking' || mode==='listening');
if (speaking) clear();
else if (wasSpeaking || isFocused()) schedule();
});
socket.on('alfred_render', () => window.setTimeout(() => {stateChanged();schedule();},0));
}
document.addEventListener('pointerdown',(ev)=>{
if (isFocused() && ev.target?.closest('#stage')) schedule();
},{passive:true});
document.addEventListener('keydown',(ev)=>{
if (isFocused() && ['ArrowUp','ArrowDown','PageUp','PageDown'].includes(ev.key)) schedule();
});
document.addEventListener('visibilitychange',()=>{
if (document.hidden) clear(); else schedule();
});
stateChanged();
console.info('[Dona] Retour automatique à l’accueil activé après',delay/1000,'secondes (hors lecture vocale).');
}
if (document.readyState==='loading') document.addEventListener('DOMContentLoaded',init,{once:true});
else init();
})();