From 262c2fcff285eac86c3eb98aff3414de68084804 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Tue, 31 Jan 2023 12:38:25 +0000 Subject: [PATCH] Fix useUnreadNotifications exploding with falsey room, like in notif panel (#10030) --- src/RoomNotifs.ts | 8 ++++++-- src/components/views/rooms/EventTile.tsx | 2 +- .../rooms/NotificationBadge/UnreadNotificationBadge.tsx | 2 +- src/hooks/useUnreadNotifications.ts | 6 +++--- 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/RoomNotifs.ts b/src/RoomNotifs.ts index 16dc84895a..0962e45cb9 100644 --- a/src/RoomNotifs.ts +++ b/src/RoomNotifs.ts @@ -26,7 +26,7 @@ import { MatrixClientPeg } from "./MatrixClientPeg"; import { NotificationColor } from "./stores/notifications/NotificationColor"; import { getUnsentMessages } from "./components/structures/RoomStatusBar"; import { doesRoomHaveUnreadMessages, doesRoomOrThreadHaveUnreadMessages } from "./Unread"; -import { getEffectiveMembership, EffectiveMembership } from "./utils/membership"; +import { EffectiveMembership, getEffectiveMembership } from "./utils/membership"; export enum RoomNotifState { AllMessagesLoud = "all_messages_loud", @@ -202,9 +202,13 @@ function isMuteRule(rule: IPushRule): boolean { } export function determineUnreadState( - room: Room, + room?: Room, threadId?: string, ): { color: NotificationColor; symbol: string | null; count: number } { + if (!room) { + return { symbol: null, count: 0, color: NotificationColor.None }; + } + if (getUnsentMessages(room, threadId).length > 0) { return { symbol: "!", count: 1, color: NotificationColor.Unsent }; } diff --git a/src/components/views/rooms/EventTile.tsx b/src/components/views/rooms/EventTile.tsx index 5620ab9356..ba7a5a9503 100644 --- a/src/components/views/rooms/EventTile.tsx +++ b/src/components/views/rooms/EventTile.tsx @@ -1348,7 +1348,7 @@ export class UnwrappedEventTile extends React.Component )} {msgOption} - + , ); } diff --git a/src/components/views/rooms/NotificationBadge/UnreadNotificationBadge.tsx b/src/components/views/rooms/NotificationBadge/UnreadNotificationBadge.tsx index 33612ad731..f09c151690 100644 --- a/src/components/views/rooms/NotificationBadge/UnreadNotificationBadge.tsx +++ b/src/components/views/rooms/NotificationBadge/UnreadNotificationBadge.tsx @@ -21,7 +21,7 @@ import { useUnreadNotifications } from "../../../../hooks/useUnreadNotifications import { StatelessNotificationBadge } from "./StatelessNotificationBadge"; interface Props { - room: Room; + room?: Room; threadId?: string; } diff --git a/src/hooks/useUnreadNotifications.ts b/src/hooks/useUnreadNotifications.ts index c5e0e3da16..8993c24b1f 100644 --- a/src/hooks/useUnreadNotifications.ts +++ b/src/hooks/useUnreadNotifications.ts @@ -19,11 +19,11 @@ import { useCallback, useEffect, useState } from "react"; import type { NotificationCount, Room } from "matrix-js-sdk/src/models/room"; import { determineUnreadState } from "../RoomNotifs"; -import type { NotificationColor } from "../stores/notifications/NotificationColor"; +import { NotificationColor } from "../stores/notifications/NotificationColor"; import { useEventEmitter } from "./useEventEmitter"; export const useUnreadNotifications = ( - room: Room, + room?: Room, threadId?: string, ): { symbol: string | null; @@ -32,7 +32,7 @@ export const useUnreadNotifications = ( } => { const [symbol, setSymbol] = useState(null); const [count, setCount] = useState(0); - const [color, setColor] = useState(0); + const [color, setColor] = useState(NotificationColor.None); useEventEmitter( room,