diff --git a/src/components/middle/MiddleHeader.tsx b/src/components/middle/MiddleHeader.tsx index 2012118d4..d6ef0d6eb 100644 --- a/src/components/middle/MiddleHeader.tsx +++ b/src/components/middle/MiddleHeader.tsx @@ -1,17 +1,17 @@ import React, { FC, useCallback, useMemo, memo, useEffect, useRef, useState, } from '../../lib/teact/teact'; -import { withGlobal } from '../../lib/teact/teactn'; +import { getGlobal, withGlobal } from '../../lib/teact/teactn'; import cycleRestrict from '../../util/cycleRestrict'; import { GlobalActions, MessageListType } from '../../global/types'; import { ApiMessage, ApiChat, + ApiUser, ApiTypingStatus, - MAIN_THREAD_ID, ApiUser, + MAIN_THREAD_ID, } from '../../api/types'; -import { NotifyException, NotifySettings } from '../../types'; import { MIN_SCREEN_WIDTH_FOR_STATIC_LEFT_COLUMN, @@ -24,11 +24,9 @@ import { import { IS_SINGLE_COLUMN_LAYOUT, IS_TABLET_COLUMN_LAYOUT } from '../../util/environment'; import { isChatPrivate, - isChatArchived, getMessageKey, getChatTitle, getSenderTitle, - selectIsChatMuted, } from '../../modules/helpers'; import { selectChat, @@ -44,8 +42,7 @@ import { selectScheduledIds, selectIsInSelectMode, selectIsChatWithBot, - selectNotifySettings, - selectNotifyExceptions, + selectCountNotMutedUnread, } from '../../modules/selectors'; import useEnsureMessage from '../../hooks/useEnsureMessage'; import useWindowSize from '../../hooks/useWindowSize'; @@ -91,8 +88,6 @@ type StateProps = { isChatWithSelf?: boolean; isChatWithBot?: boolean; lastSyncTime?: number; - notifySettings: NotifySettings; - notifyExceptions?: Record; shouldSkipHistoryAnimations?: boolean; currentTransitionKey: number; }; @@ -122,8 +117,6 @@ const MiddleHeader: FC = ({ isChatWithSelf, isChatWithBot, lastSyncTime, - notifySettings, - notifyExceptions, shouldSkipHistoryAnimations, currentTransitionKey, openChatWithInfo, @@ -221,32 +214,8 @@ const MiddleHeader: FC = ({ return undefined; } - let isActive = false; - - const totalCount = Object.values(chatsById).reduce((total, currentChat) => { - if (isChatArchived(currentChat)) { - return total; - } - - const count = currentChat.unreadCount || 0; - if ( - count && (!selectIsChatMuted(currentChat, notifySettings, notifyExceptions) || currentChat.unreadMentionsCount) - ) { - isActive = true; - } - - return total + count; - }, 0); - - if (!totalCount) { - return undefined; - } - - return { - isActive, - totalCount, - }; - }, [isLeftColumnHideable, chatsById, notifySettings, notifyExceptions]); + return selectCountNotMutedUnread(getGlobal()) || undefined; + }, [isLeftColumnHideable, chatsById]); const canToolsCollideWithChatInfo = ( windowWidth >= MIN_SCREEN_WIDTH_FOR_STATIC_LEFT_COLUMN @@ -340,7 +309,7 @@ const MiddleHeader: FC = ({ function renderMainThreadInfo() { return ( <> - {(isLeftColumnHideable || currentTransitionKey > 0) && renderBackButton(shouldShowCloseButton, unreadCount)} + {(isLeftColumnHideable || currentTransitionKey > 0) && renderBackButton(shouldShowCloseButton, true)}
{isChatPrivate(chatId) ? ( = ({ ); } - function renderBackButton(asClose = false, unreadCountInfo?: typeof unreadCount) { + function renderBackButton(asClose = false, withUnreadCount = false) { return (
- {unreadCountInfo && ( -
- {formatIntegerCompact(unreadCountInfo.totalCount)} + {withUnreadCount && unreadCount && ( +
+ {formatIntegerCompact(unreadCount)}
)}
@@ -466,8 +435,6 @@ export default memo(withGlobal( isChatWithSelf: selectIsChatWithSelf(global, chatId), isChatWithBot: chat && selectIsChatWithBot(global, chat), lastSyncTime, - notifySettings: selectNotifySettings(global), - notifyExceptions: selectNotifyExceptions(global), shouldSkipHistoryAnimations, currentTransitionKey: Math.max(0, global.messages.messageLists.length - 1), }; diff --git a/src/modules/actions/apiUpdaters/chats.ts b/src/modules/actions/apiUpdaters/chats.ts index c76f826d0..e6c8e0b0e 100644 --- a/src/modules/actions/apiUpdaters/chats.ts +++ b/src/modules/actions/apiUpdaters/chats.ts @@ -19,7 +19,8 @@ import { selectIsChatListed, selectChatListType, selectCurrentMessageList, - selectCountNotMutedUnread, selectNotifySettings, + selectCountNotMutedUnread, + selectNotifySettings, } from '../../selectors'; import { throttle } from '../../../util/schedulers'; @@ -40,8 +41,7 @@ addReducer('apiUpdate', (global, actions, update: ApiUpdate) => { const newGlobal = updateChat(global, update.id, update.chat, update.newProfilePhoto); setGlobal(newGlobal); - const unreadCount = selectCountNotMutedUnread(newGlobal); - runThrottledForUpdateAppBadge(() => updateAppBadge(unreadCount)); + runThrottledForUpdateAppBadge(() => updateAppBadge(selectCountNotMutedUnread(getGlobal()))); break; } @@ -133,8 +133,7 @@ addReducer('apiUpdate', (global, actions, update: ApiUpdate) => { })); } - const unreadCount = selectCountNotMutedUnread(getGlobal()); - updateAppBadge(unreadCount); + updateAppBadge(selectCountNotMutedUnread(getGlobal())); const { hasWebNotifications } = selectNotifySettings(global); if (hasWebNotifications) { diff --git a/src/modules/selectors/chats.ts b/src/modules/selectors/chats.ts index 1f2b66bf3..b1e44f912 100644 --- a/src/modules/selectors/chats.ts +++ b/src/modules/selectors/chats.ts @@ -147,6 +147,7 @@ export function selectChatByUsername(global: GlobalState, username: string) { ); } +// Slow, not to be used in `withGlobal` export function selectCountNotMutedUnread(global: GlobalState) { const activeChatIds = global.chats.listIds.active; if (!activeChatIds) { @@ -154,6 +155,8 @@ export function selectCountNotMutedUnread(global: GlobalState) { } const chats = global.chats.byId; + const notifySettings = selectNotifySettings(global); + const notifyExceptions = selectNotifyExceptions(global); return activeChatIds.reduce((acc, chatId) => { const chat = chats[chatId]; @@ -161,7 +164,10 @@ export function selectCountNotMutedUnread(global: GlobalState) { if ( chat && chat.unreadCount - && !selectIsChatMuted(chat, selectNotifySettings(global), selectNotifyExceptions(global)) + && chat.isListed + && !chat.isNotJoined + && !chat.isRestricted + && (chat.unreadMentionsCount || !selectIsChatMuted(chat, notifySettings, notifyExceptions)) ) { return acc + chat.unreadCount; }