From 7b367e267b73c4e5c94825563d4671b18517f63f Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Wed, 6 Feb 2019 10:51:29 +0000 Subject: [PATCH] move sublist badge aggregation code to RoomNotifs to reuse for tags/comm --- src/RoomNotifs.js | 41 ++++++++++++++ src/components/structures/RoomSubList.js | 70 ++++-------------------- 2 files changed, 52 insertions(+), 59 deletions(-) diff --git a/src/RoomNotifs.js b/src/RoomNotifs.js index 91e49fe09b..77fb5efb76 100644 --- a/src/RoomNotifs.js +++ b/src/RoomNotifs.js @@ -23,6 +23,47 @@ export const ALL_MESSAGES = 'all_messages'; export const MENTIONS_ONLY = 'mentions_only'; export const MUTE = 'mute'; + +function _shouldShowNotifBadge(roomNotifState) { + const showBadgeInStates = [ALL_MESSAGES, ALL_MESSAGES_LOUD]; + return showBadgeInStates.indexOf(roomNotifState) > -1; +} + +function _shouldShowMentionBadge(roomNotifState) { + return roomNotifState !== MUTE; +} + +export function aggregateNotificationCount(rooms) { + return rooms.reduce((result, room, index) => { + const roomNotifState = getRoomNotifsState(room.roomId); + const highlight = room.getUnreadNotificationCount('highlight') > 0; + const notificationCount = room.getUnreadNotificationCount(); + + const notifBadges = notificationCount > 0 && _shouldShowNotifBadge(roomNotifState); + const mentionBadges = highlight && _shouldShowMentionBadge(roomNotifState); + const badges = notifBadges || mentionBadges; + + if (badges) { + result.count += notificationCount; + if (highlight) { + result.highlight = true; + } + } + return result; + }, {count: 0, highlight: false}); +} + +export function getRoomHasBadge(room) { + const roomNotifState = getRoomNotifsState(room.roomId); + const highlight = room.getUnreadNotificationCount('highlight') > 0; + const notificationCount = room.getUnreadNotificationCount(); + + const notifBadges = notificationCount > 0 && _shouldShowNotifBadge(roomNotifState); + const mentionBadges = highlight && _shouldShowMentionBadge(roomNotifState); + + return notifBadges || mentionBadges; +} + export function getRoomNotifsState(roomId) { if (MatrixClientPeg.get().isGuest()) return ALL_MESSAGES; diff --git a/src/components/structures/RoomSubList.js b/src/components/structures/RoomSubList.js index 04d1fb4e39..a2792e3782 100644 --- a/src/components/structures/RoomSubList.js +++ b/src/components/structures/RoomSubList.js @@ -127,46 +127,6 @@ const RoomSubList = React.createClass({ }); }, - _shouldShowNotifBadge: function(roomNotifState) { - const showBadgeInStates = [RoomNotifs.ALL_MESSAGES, RoomNotifs.ALL_MESSAGES_LOUD]; - return showBadgeInStates.indexOf(roomNotifState) > -1; - }, - - _shouldShowMentionBadge: function(roomNotifState) { - return roomNotifState !== RoomNotifs.MUTE; - }, - - /** - * Total up all the notification counts from the rooms - * - * @returns {Array} The array takes the form [total, highlight] where highlight is a bool - */ - roomNotificationCount: function() { - const self = this; - - if (this.props.isInvite) { - return [0, true]; - } - - return this.props.list.reduce(function(result, room, index) { - const roomNotifState = RoomNotifs.getRoomNotifsState(room.roomId); - const highlight = room.getUnreadNotificationCount('highlight') > 0; - const notificationCount = room.getUnreadNotificationCount(); - - const notifBadges = notificationCount > 0 && self._shouldShowNotifBadge(roomNotifState); - const mentionBadges = highlight && self._shouldShowMentionBadge(roomNotifState); - const badges = notifBadges || mentionBadges; - - if (badges) { - result[0] += notificationCount; - if (highlight) { - result[1] = true; - } - } - return result; - }, [0, false]); - }, - _updateSubListCount: function() { // Force an update by setting the state to the current state // Doing it this way rather than using forceUpdate(), so that the shouldComponentUpdate() @@ -197,22 +157,12 @@ const RoomSubList = React.createClass({ // prevent the roomsublist collapsing e.preventDefault(); e.stopPropagation(); - // find first room which has notifications and switch to it - for (const room of this.props.list) { - const roomNotifState = RoomNotifs.getRoomNotifsState(room.roomId); - const highlight = room.getUnreadNotificationCount('highlight') > 0; - const notificationCount = room.getUnreadNotificationCount(); - - const notifBadges = notificationCount > 0 && this._shouldShowNotifBadge(roomNotifState); - const mentionBadges = highlight && this._shouldShowMentionBadge(roomNotifState); - - if (notifBadges || mentionBadges) { - dis.dispatch({ - action: 'view_room', - room_id: room.roomId, - }); - return; - } + const room = this.props.lists.find(room => RoomNotifs.getRoomHasBadge(room)); + if (room) { + dis.dispatch({ + action: 'view_room', + room_id: room.roomId, + }); } }, @@ -240,9 +190,11 @@ const RoomSubList = React.createClass({ _getHeaderJsx: function(isCollapsed) { const AccessibleButton = sdk.getComponent('elements.AccessibleButton'); - const subListNotifications = this.roomNotificationCount(); - const subListNotifCount = subListNotifications[0]; - const subListNotifHighlight = subListNotifications[1]; + const subListNotifications = !this.props.isInvite ? + RoomNotifs.aggregateNotificationCount(this.props.list) : + {count: 0, highlight: true}; + const subListNotifCount = subListNotifications.count; + const subListNotifHighlight = subListNotifications.highlight; let badge; if (!this.props.collapsed) {