Selaa lähdekoodia

spike: feat(notifications): prioritize based on type (appearance) (#2188)

* feat(notifications): prioritize based on type (appearance)

* squash: add return description to doc
master
virtuacoplenny 8 vuotta sitten
vanhempi
commit
4ead402388

+ 13
- 0
react/features/notifications/constants.js Näytä tiedosto

@@ -10,3 +10,16 @@ export const NOTIFICATION_TYPE = {
10 10
     SUCCESS: 'success',
11 11
     WARNING: 'warning'
12 12
 };
13
+
14
+/**
15
+ * A mapping of notification type to priority of display.
16
+ *
17
+ * @enum {number}
18
+ */
19
+export const NOTIFICATION_TYPE_PRIORITIES = {
20
+    [NOTIFICATION_TYPE.ERROR]: 5,
21
+    [NOTIFICATION_TYPE.INFO]: 3,
22
+    [NOTIFICATION_TYPE.NORMAL]: 3,
23
+    [NOTIFICATION_TYPE.SUCCESS]: 3,
24
+    [NOTIFICATION_TYPE.WARNING]: 4
25
+};

+ 44
- 5
react/features/notifications/reducer.js Näytä tiedosto

@@ -5,6 +5,7 @@ import {
5 5
     SET_NOTIFICATIONS_ENABLED,
6 6
     SHOW_NOTIFICATION
7 7
 } from './actionTypes';
8
+import { NOTIFICATION_TYPE_PRIORITIES } from './constants';
8 9
 
9 10
 /**
10 11
  * The initial state of the feature notifications.
@@ -43,17 +44,55 @@ ReducerRegistry.register('features/notifications',
43 44
         case SHOW_NOTIFICATION:
44 45
             return {
45 46
                 ...state,
46
-                notifications: [
47
-                    ...state.notifications,
48
-                    {
47
+                notifications:
48
+                    _insertNotificationByPriority(state.notifications, {
49 49
                         component: action.component,
50 50
                         props: action.props,
51 51
                         timeout: action.timeout,
52 52
                         uid: action.uid
53
-                    }
54
-                ]
53
+                    })
55 54
             };
56 55
         }
57 56
 
58 57
         return state;
59 58
     });
59
+
60
+/**
61
+ * Creates a new notification queue with the passed in notification placed at
62
+ * the end of other notifications with higher or the same priority.
63
+ *
64
+ * @param {Object[]} notifications - The queue of notifications to be displayed.
65
+ * @param {Object} notification - The new notification to add to the queue.
66
+ * @private
67
+ * @returns {Object[]} A new array with an updated order of the notification
68
+ * queue.
69
+ */
70
+function _insertNotificationByPriority(notifications, notification) {
71
+    const newNotificationPriority
72
+        = NOTIFICATION_TYPE_PRIORITIES[notification.props.appearance] || 0;
73
+
74
+    // Default to putting the new notification at the end of the queue.
75
+    let insertAtLocation = notifications.length;
76
+
77
+    // Find where to insert the new notification based on priority. Do not
78
+    // insert at the front of the queue so that the user can finish acting on
79
+    // any notification currently being read.
80
+    for (let i = 1; i < notifications.length; i++) {
81
+        const queuedNotification = notifications[i];
82
+        const queuedNotificationPriority
83
+            = NOTIFICATION_TYPE_PRIORITIES[queuedNotification.props.appearance]
84
+                || 0;
85
+
86
+        if (queuedNotificationPriority < newNotificationPriority) {
87
+            insertAtLocation = i;
88
+            break;
89
+        }
90
+    }
91
+
92
+    // Create a copy to avoid mutation and insert the notification.
93
+    const copyOfNotifications = notifications.slice();
94
+
95
+    copyOfNotifications.splice(insertAtLocation, 0, notification);
96
+
97
+    return copyOfNotifications;
98
+}

Loading…
Peruuta
Tallenna