Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid recreate notify method on notifications list update #102

Merged
merged 1 commit into from
Feb 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading