From 352c1b5ec282f675d2958fae533dd92cf19fe798 Mon Sep 17 00:00:00 2001 From: Garrett Michael Flynn Date: Mon, 20 May 2024 15:58:55 -0700 Subject: [PATCH 1/2] Limit duplicated notifications --- src/renderer/src/dependencies/globals.js | 32 +++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/src/renderer/src/dependencies/globals.js b/src/renderer/src/dependencies/globals.js index e391b27b6..12583aaea 100644 --- a/src/renderer/src/dependencies/globals.js +++ b/src/renderer/src/dependencies/globals.js @@ -109,10 +109,40 @@ export const notyf = new Notyf({ ], }); +let activeNotifications = []; // Counter for active error notifications +const maxConcurrentErrors = 3; // Maximum number of concurrent error notifications + +function removeNotification(notification) { + const index = activeNotifications.indexOf(notification); + if (index > -1) activeNotifications.splice(index, 1); +} + export const notify = (message, type = "success", duration) => { + + const id = `${type}_${message}`; + + const duplicate = activeNotifications.find((n) => n.id === id) + if (duplicate) { + removeNotification(duplicate) + dismissNotification(duplicate) + } + + if (Object.keys(activeNotifications).length >= maxConcurrentErrors) { + const popped = activeNotifications.shift(); + dismissNotification(popped) + } + const info = { type, message }; if (duration) info.duration = duration; - return notyf.open(info); + + const notification = notyf.open(info); + notification.id = id; + + activeNotifications.push(notification); + + notification.on('dismiss', () => removeNotification(notification)) + + return notification; }; export const dismissNotification = (notification) => notyf.dismiss(notification); From 03ba0e82215c0fd292dd70c3ffe8fb937b31d91b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 20 May 2024 23:00:08 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/renderer/src/dependencies/globals.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/renderer/src/dependencies/globals.js b/src/renderer/src/dependencies/globals.js index 12583aaea..dc2abb84d 100644 --- a/src/renderer/src/dependencies/globals.js +++ b/src/renderer/src/dependencies/globals.js @@ -118,18 +118,17 @@ function removeNotification(notification) { } export const notify = (message, type = "success", duration) => { - const id = `${type}_${message}`; - const duplicate = activeNotifications.find((n) => n.id === id) + const duplicate = activeNotifications.find((n) => n.id === id); if (duplicate) { - removeNotification(duplicate) - dismissNotification(duplicate) + removeNotification(duplicate); + dismissNotification(duplicate); } if (Object.keys(activeNotifications).length >= maxConcurrentErrors) { const popped = activeNotifications.shift(); - dismissNotification(popped) + dismissNotification(popped); } const info = { type, message }; @@ -140,7 +139,7 @@ export const notify = (message, type = "success", duration) => { activeNotifications.push(notification); - notification.on('dismiss', () => removeNotification(notification)) + notification.on("dismiss", () => removeNotification(notification)); return notification; };