Skip to content

Commit

Permalink
Merge pull request #789 from NeurodataWithoutBorders/limit-errors-shown
Browse files Browse the repository at this point in the history
Limit duplicated notifications
  • Loading branch information
CodyCBakerPhD authored May 21, 2024
2 parents ee53d49 + 03ba0e8 commit 9960b42
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion src/renderer/src/dependencies/globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,39 @@ 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);

0 comments on commit 9960b42

Please sign in to comment.