From bf2d26ef21664e427540ff4cb29c89c7f14dcb70 Mon Sep 17 00:00:00 2001 From: Jaiwanth Date: Thu, 20 May 2021 10:55:22 +0530 Subject: [PATCH] Modify to navigate only on notification dots click --- src/components/views/spaces/SpacePanel.tsx | 6 +++- .../views/spaces/SpaceTreeLevel.tsx | 6 +++- src/stores/SpaceStore.tsx | 35 ++++++++++--------- .../notifications/SpaceNotificationState.ts | 5 ++- .../SummarizedNotificationState.ts | 12 +++---- 5 files changed, 36 insertions(+), 28 deletions(-) diff --git a/src/components/views/spaces/SpacePanel.tsx b/src/components/views/spaces/SpacePanel.tsx index 411b0f9b5e..74fd01954d 100644 --- a/src/components/views/spaces/SpacePanel.tsx +++ b/src/components/views/spaces/SpacePanel.tsx @@ -74,7 +74,11 @@ const SpaceButton: React.FC = ({ let notifBadge; if (notificationState) { notifBadge =
- + SpaceStore.instance.setActiveRoomInSpace(space)} + forceCount={false} + notification={notificationState} + />
; } diff --git a/src/components/views/spaces/SpaceTreeLevel.tsx b/src/components/views/spaces/SpaceTreeLevel.tsx index e48e1d5dc2..d8569a0387 100644 --- a/src/components/views/spaces/SpaceTreeLevel.tsx +++ b/src/components/views/spaces/SpaceTreeLevel.tsx @@ -326,7 +326,11 @@ export class SpaceItem extends React.PureComponent { let notifBadge; if (notificationState) { notifBadge =
- + SpaceStore.instance.setActiveRoomInSpace(space)} + forceCount={false} + notification={notificationState} + />
; } diff --git a/src/stores/SpaceStore.tsx b/src/stores/SpaceStore.tsx index b1993d9625..e154463408 100644 --- a/src/stores/SpaceStore.tsx +++ b/src/stores/SpaceStore.tsx @@ -108,6 +108,24 @@ export class SpaceStoreClass extends AsyncStoreWithClient { return this._suggestedRooms; } + public async setActiveRoomInSpace(space: Room | null) { + if (space && !space.isSpaceRoom()) return; + if (space !== this.activeSpace) await this.setActiveSpace(space); + + const notificationState = space + ? this.getNotificationState(space.roomId) + : RoomNotificationStateStore.instance.globalState; + + if (notificationState.count) { + const roomId = notificationState.getFirstRoomWithNotifications(); + defaultDispatcher.dispatch({ + action: "view_room", + room_id: roomId, + context_switch: true, + }); + } + } + /** * Sets the active space, updates room list filters, * optionally switches the user's room back to where they were when they last viewed that space. @@ -116,22 +134,7 @@ 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 && !space.isSpaceRoom()) return; - if (space === this.activeSpace) { - const notificationState = space - ? this.getNotificationState(space.roomId) - : RoomNotificationStateStore.instance.globalState; - - if (notificationState.count) { - const roomId = notificationState.getRoomWithMaxNotifications(); - defaultDispatcher.dispatch({ - action: "view_room", - room_id: roomId, - context_switch: true, - }); - } - return; - } + if (space === this.activeSpace || (space && !space.isSpaceRoom())) return; this._activeSpace = space; this.emit(UPDATE_SELECTED_SPACE, this.activeSpace); diff --git a/src/stores/notifications/SpaceNotificationState.ts b/src/stores/notifications/SpaceNotificationState.ts index fb04648a2a..cdb9f2d06a 100644 --- a/src/stores/notifications/SpaceNotificationState.ts +++ b/src/stores/notifications/SpaceNotificationState.ts @@ -53,9 +53,8 @@ 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 getFirstRoomWithNotifications() { + return this.rooms.find((room) => room._notificationCounts.total > 0).roomId; } public destroy() { diff --git a/src/stores/notifications/SummarizedNotificationState.ts b/src/stores/notifications/SummarizedNotificationState.ts index 4a3473792a..ec6db1015d 100644 --- a/src/stores/notifications/SummarizedNotificationState.ts +++ b/src/stores/notifications/SummarizedNotificationState.ts @@ -16,7 +16,6 @@ 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"; /** @@ -27,13 +26,13 @@ import { RoomNotificationState } from "./RoomNotificationState"; */ export class SummarizedNotificationState extends NotificationState { private totalStatesWithUnread = 0; - unreadRooms: Room[]; + private unreadRoomId: string; constructor() { super(); this._symbol = null; this._count = 0; - this.unreadRooms = []; + this.unreadRoomId = null; this._color = NotificationColor.None; } @@ -41,9 +40,8 @@ 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; + public getFirstRoomWithNotifications() { + return this.unreadRoomId; } /** @@ -65,7 +63,7 @@ export class SummarizedNotificationState extends NotificationState { this._color = other.color; } if (other.hasUnreadCount) { - this.unreadRooms.push(other.room); + this.unreadRoomId = !this.unreadRoomId ? other.room.roomId : this.unreadRoomId; this.totalStatesWithUnread++; } }