Skip to content

Commit

Permalink
Avoid recreate notify method on notifications list update
Browse files Browse the repository at this point in the history
  • Loading branch information
Evgeniy committed Feb 14, 2024
1 parent 0425228 commit efdf13e
Showing 1 changed file with 22 additions and 27 deletions.
49 changes: 22 additions & 27 deletions src/layers/Notification/Notifications.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,40 +58,35 @@ export const Notifications: FC<NotificationsProps> = ({

const notify = useCallback(
(title: string, options: NotificationOptions = {}) => {
// If we are flooded with the same message over and over,
// dont add more of the same type. Mainly used for error use cases.
if (preventFlooding) {
const has = notifications.find(n => n.title === title);

if (has) {
return false;
setNotifications(notifications => {
// If we are flooded with the same message over and over,
// don't add more of the same type. Mainly used for error use cases.
if (preventFlooding && notifications.find(n => n.title === title)) {
return notifications;
}
}

const id = nextId++;

const obj = {
title,
id,
variant: 'default',
timeout,
showClose,
...options
};
const id = nextId++;

const sorted = [obj, ...notifications];
const obj = {
title,
id,
variant: 'default',
timeout,
showClose,
...options
};

// Clear old notifications if we hit limit
if (sorted.length > limit) {
sorted.pop();
}
const sorted = [obj, ...notifications];

// Update the container instance
setNotifications(sorted);
// Clear old notifications if we hit limit
if (sorted.length > limit) {
sorted.pop();
}

return id;
return sorted;
});
},
[limit, notifications, preventFlooding, showClose, timeout]
[limit, preventFlooding, showClose, timeout]
);

const notifyError = useCallback(
Expand Down

0 comments on commit efdf13e

Please sign in to comment.