From 3e8863fc9af0d5932c3393d0156ab6063baee524 Mon Sep 17 00:00:00 2001 From: Jaiwanth Date: Tue, 11 May 2021 13:00:42 +0530 Subject: [PATCH] Adjust behaviour for the home space --- src/stores/SpaceStore.tsx | 5 ++++- .../notifications/SummarizedNotificationState.ts | 12 +++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/stores/SpaceStore.tsx b/src/stores/SpaceStore.tsx index edc6bbef77..b1993d9625 100644 --- a/src/stores/SpaceStore.tsx +++ b/src/stores/SpaceStore.tsx @@ -118,7 +118,10 @@ export class SpaceStoreClass extends AsyncStoreWithClient { public async setActiveSpace(space: Room | null, contextSwitch = true) { if (space && !space.isSpaceRoom()) return; if (space === this.activeSpace) { - const notificationState = this.getNotificationState(space ? space.roomId : HOME_SPACE); + const notificationState = space + ? this.getNotificationState(space.roomId) + : RoomNotificationStateStore.instance.globalState; + if (notificationState.count) { const roomId = notificationState.getRoomWithMaxNotifications(); defaultDispatcher.dispatch({ diff --git a/src/stores/notifications/SummarizedNotificationState.ts b/src/stores/notifications/SummarizedNotificationState.ts index 372da74f36..4a3473792a 100644 --- a/src/stores/notifications/SummarizedNotificationState.ts +++ b/src/stores/notifications/SummarizedNotificationState.ts @@ -16,6 +16,8 @@ limitations under the License. import { NotificationColor } from "./NotificationColor"; import { NotificationState } from "./NotificationState"; +import { Room } from "matrix-js-sdk/src/models/room"; +import { RoomNotificationState } from "./RoomNotificationState"; /** * Summarizes a number of states into a unique snapshot. To populate, call @@ -25,11 +27,13 @@ import { NotificationState } from "./NotificationState"; */ export class SummarizedNotificationState extends NotificationState { private totalStatesWithUnread = 0; + unreadRooms: Room[]; constructor() { super(); this._symbol = null; this._count = 0; + this.unreadRooms = []; this._color = NotificationColor.None; } @@ -37,6 +41,11 @@ export class SummarizedNotificationState extends NotificationState { return this.totalStatesWithUnread; } + public getRoomWithMaxNotifications() { + return this.unreadRooms.reduce((prev, curr) => + (prev._notificationCounts.total > curr._notificationCounts.total ? prev : curr)).roomId; + } + /** * Append a notification state to this snapshot, taking the loudest NotificationColor * of the two. By default this will not adopt the symbol of the other notification @@ -45,7 +54,7 @@ export class SummarizedNotificationState extends NotificationState { * @param includeSymbol If true, the notification state's symbol will be taken if one * is present. */ - public add(other: NotificationState, includeSymbol = false) { + public add(other: RoomNotificationState, includeSymbol = false) { if (other.symbol && includeSymbol) { this._symbol = other.symbol; } @@ -56,6 +65,7 @@ export class SummarizedNotificationState extends NotificationState { this._color = other.color; } if (other.hasUnreadCount) { + this.unreadRooms.push(other.room); this.totalStatesWithUnread++; } }