From 73b9ad41da12e1092a850efa32c4e6a296342103 Mon Sep 17 00:00:00 2001 From: Jaiwanth Date: Wed, 5 May 2021 12:38:44 +0530 Subject: [PATCH] Navigate to room with maximum notifications when clicked on already selected space --- src/stores/SpaceStore.tsx | 15 +++++++++++++-- .../notifications/SpaceNotificationState.ts | 5 +++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/stores/SpaceStore.tsx b/src/stores/SpaceStore.tsx index 43822007c9..7c0f8cf59b 100644 --- a/src/stores/SpaceStore.tsx +++ b/src/stores/SpaceStore.tsx @@ -120,8 +120,19 @@ export class SpaceStoreClass extends AsyncStoreWithClient { * should not be done when the space switch is done implicitly due to another event like switching room. */ public async setActiveSpace(space: Room | null, contextSwitch = true) { - if (space === this.activeSpace || (space && !space?.isSpaceRoom())) return; - + if (space && !space?.isSpaceRoom()) return; + if (space === this.activeSpace) { + const notificationState = this.getNotificationState(space.roomId); + if (notificationState.count) { + const roomId = notificationState.getRoomWithMaxNotifications(); + defaultDispatcher.dispatch({ + action: "view_room", + room_id: roomId, + context_switch: true, + }); + } + return; + } this._activeSpace = space; this.emit(UPDATE_SELECTED_SPACE, this.activeSpace); this.emit(SUGGESTED_ROOMS, this._suggestedRooms = []); diff --git a/src/stores/notifications/SpaceNotificationState.ts b/src/stores/notifications/SpaceNotificationState.ts index 61a9701a07..fb04648a2a 100644 --- a/src/stores/notifications/SpaceNotificationState.ts +++ b/src/stores/notifications/SpaceNotificationState.ts @@ -53,6 +53,11 @@ export class SpaceNotificationState extends NotificationState { this.calculateTotalState(); } + public getRoomWithMaxNotifications() { + return this.rooms.reduce((prev, curr) => + (prev._notificationCounts.total > curr._notificationCounts.total ? prev : curr)).roomId; + } + public destroy() { super.destroy(); for (const state of Object.values(this.states)) {