From 0502e06fbfd2fb5e747029c89b8cd3423a612ccc Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pr=C3=A9nom=20Nom?= <adresse@mail.com>
Date: Mon, 24 Feb 2025 07:06:29 +0100
Subject: [PATCH] ajout notif ajout auto

---
 src/utils/stats.js            | 35 +++++++++++++++++++++++++++--------
 src/workers/pyodide_worker.js |  2 +-
 2 files changed, 28 insertions(+), 9 deletions(-)

diff --git a/src/utils/stats.js b/src/utils/stats.js
index fca6b40..09a9307 100644
--- a/src/utils/stats.js
+++ b/src/utils/stats.js
@@ -21,19 +21,38 @@
                 }
                 if (message.command === "threshold-exceeded") {
                     log("[Stats] Mots dépassant le seuil :", message.wordsAboveThreshold);
-                    let alertMessage = "Nouveaux mots dépassant le seuil :\n";
+                
                     if (typeof message.wordsAboveThreshold !== "object" || message.wordsAboveThreshold === null) {
                         return;
                     }
-                        for (const [lang, words] of Object.entries(message.wordsAboveThreshold)) {
-                            if (!Array.isArray(words)) {
-                                continue;
+                    let notificationText = "Mots ajoutés :\n";
+                    let wordsList = [];
+                    for (const [lang, words] of Object.entries(message.wordsAboveThreshold)) {
+                        if (!Array.isArray(words) || words.length === 0) continue;
+                
+                        words.forEach(word => {
+                            wordsList.push(`${lang.toUpperCase()} : ${word}`);
+                        });
+                    }
+                    if (wordsList.length === 0) {
+                        log("[Stats] Aucun mot à afficher dans la notification.");
+                        return;
+                    }
+                    notificationText += wordsList.join("\n");
+                
+                    // Afficher une notification
+                    if (Notification.permission === "granted") {
+                        new Notification("Ajout automatique", {
+                            body: notificationText,
+                            icon: browser.runtime.getURL("icons/border-48.png")
+                        });
+                    } else if (Notification.permission !== "denied") {
+                        Notification.requestPermission().then(permission => {
+                            if (permission === "granted") {
+                                new Notification("Ajout automatique", { body: notificationText });
                             }
-            
-                        alertMessage += `\n${lang.toUpperCase()} : ${words.join(", ")}`;
+                        });
                     }
-            
-                    alert(alertMessage);
                 }
             });
             workerPort.onDisconnect.addListener(() => {
diff --git a/src/workers/pyodide_worker.js b/src/workers/pyodide_worker.js
index f099a86..99c90ab 100644
--- a/src/workers/pyodide_worker.js
+++ b/src/workers/pyodide_worker.js
@@ -231,7 +231,7 @@ async function checkThreshold(lang) {
         finalWords.forEach(word => notifiedSet.add(word));
 
         log("Mots dépassant le seuil :", finalWords);
-        self.postMessage({ type: "threshold-exceeded", wordsAboveThreshold: finalWords });
+        self.postMessage({ type: "threshold-exceeded", wordsAboveThreshold: { [lang]: finalWords } });
 
         // Ajout aux mots en attente pour un envoi groupé
         if (!pendingWords[lang]) pendingWords[lang] = [];
-- 
GitLab