Skip to content

Commit 4ce36e7

Browse files
author
Cameron Dawson
authored
Bug 1499559 - Fix notifications not auto-expiring (#4420)
1 parent 25b6fb8 commit 4ce36e7

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

ui/shared/context/Notifications.jsx

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import React from 'react';
22
import PropTypes from 'prop-types';
3+
import findLastIndex from 'lodash/findLastIndex';
34

45
export const NotificationsContext = React.createContext({});
6+
const maxTransientNotifications = 5;
7+
const maxStoredNotifications = 40;
58

69
export class Notifications extends React.Component {
710
constructor(props) {
@@ -49,12 +52,16 @@ export class Notifications extends React.Component {
4952
opts = opts || {};
5053
severity = severity || 'info';
5154
const { notifications, storedNotifications } = this.state;
52-
const maxNsNotifications = 5;
5355
const notification = { ...opts, message, severity, created: Date.now() };
54-
const newNotifications = [notification, ...notifications];
56+
const trimmedNotifications =
57+
notifications >= maxTransientNotifications
58+
? this.withoutOldestTransient(notifications)
59+
: notifications;
60+
61+
const newNotifications = [notification, ...trimmedNotifications];
5562

5663
storedNotifications.unshift(notification);
57-
storedNotifications.splice(40);
64+
storedNotifications.splice(maxStoredNotifications);
5865
localStorage.setItem('notifications', JSON.stringify(storedNotifications));
5966

6067
this.setValue(
@@ -64,10 +71,6 @@ export class Notifications extends React.Component {
6471
},
6572
() => {
6673
if (!opts.sticky) {
67-
if (notifications.length > maxNsNotifications) {
68-
this.shift();
69-
return;
70-
}
7174
setTimeout(this.shift, 4000, true);
7275
}
7376
},
@@ -96,6 +99,15 @@ export class Notifications extends React.Component {
9699
this.removeNotification(notifications.findIndex(n => !n.sticky), delay);
97100
};
98101

102+
withoutOldestTransient = notifications => {
103+
const last = findLastIndex(notifications, n => !n.sticky);
104+
105+
if (last) {
106+
notifications.splice(last, 1);
107+
}
108+
return notifications;
109+
};
110+
99111
/*
100112
* Clear the list of stored notifications
101113
*/

0 commit comments

Comments
 (0)