From 7cac734ff522a81913c52aade3b4b5549206a2ca Mon Sep 17 00:00:00 2001 From: Audric Ackermann Date: Tue, 7 Jan 2020 15:43:49 +1100 Subject: [PATCH] count globally number of message unread in selector --- ts/components/LeftPane.tsx | 3 ++ ts/components/session/ActionsPanel.tsx | 53 +++---------------- .../session/LeftPaneContactSection.tsx | 6 +-- ts/state/ducks/search.ts | 3 +- ts/state/selectors/conversations.ts | 10 ++++ ts/state/smart/LeftPane.tsx | 5 +- 6 files changed, 29 insertions(+), 51 deletions(-) diff --git a/ts/components/LeftPane.tsx b/ts/components/LeftPane.tsx index 5d4ef374e..6bd84503d 100644 --- a/ts/components/LeftPane.tsx +++ b/ts/components/LeftPane.tsx @@ -44,6 +44,7 @@ interface Props { friends: Array; sentFriendsRequest: Array; receivedFriendsRequest: Array; + unreadMessageCount: number; searchResults?: SearchResultsProps; searchTerm: string; isSecondaryDevice: boolean; @@ -170,6 +171,8 @@ export class LeftPane extends React.Component { selectedSection={this.state.selectedSection} onSectionSelected={this.handleSectionSelected} conversations={this.props.conversations} + receivedFriendsRequest={this.props.receivedFriendsRequest} + unreadMessageCount={this.props.unreadMessageCount} />
{this.renderSection()}
diff --git a/ts/components/session/ActionsPanel.tsx b/ts/components/session/ActionsPanel.tsx index 7184ab4e6..485e69e1d 100644 --- a/ts/components/session/ActionsPanel.tsx +++ b/ts/components/session/ActionsPanel.tsx @@ -20,6 +20,8 @@ interface Props { onSectionSelected: any; selectedSection: SectionType; conversations: Array | undefined; + receivedFriendsRequest: Array; + unreadMessageCount: number; } const Section = ({ @@ -122,26 +124,6 @@ export class ActionsPanel extends React.Component { }; } - public static GET_FRIEND_REQUESTS_COUNT( - conversations: Array | undefined - ): number { - let friendRequestCount = 0; - if (conversations !== undefined) { - // We assume a friend request already read is still a friend valid request - conversations.some(conversation => { - // Ignore friend request with lastmessage status as sent as this is a friend request we made ourself - friendRequestCount += conversation.hasReceivedFriendRequest ? 1 : 0; - if (friendRequestCount > 9) { - return true; - } - - return false; - }); - } - - return friendRequestCount; - } - public componentDidMount() { // tslint:disable-next-line: no-backbone-get-set-outside-model const ourNumber = window.storage.get('primaryDevicePubKey'); @@ -156,12 +138,13 @@ export class ActionsPanel extends React.Component { } public render(): JSX.Element { - const { selectedSection, conversations } = this.props; + const { + selectedSection, + receivedFriendsRequest, + unreadMessageCount, + } = this.props; - const friendRequestCount = ActionsPanel.GET_FRIEND_REQUESTS_COUNT( - conversations - ); - const unreadMessageCount = this.getUnreadMessageCount(); + const friendRequestCount = receivedFriendsRequest.length; const isProfilePageSelected = selectedSection === SectionType.Profile; const isMessagePageSelected = selectedSection === SectionType.Message; @@ -209,26 +192,6 @@ export class ActionsPanel extends React.Component { ); } - private getUnreadMessageCount(): number { - const { conversations } = this.props; - let unreadCount = 0; - if (conversations !== undefined) { - conversations.some(conversation => { - if (conversation.isPendingFriendRequest) { - return false; - } - unreadCount += conversation.unreadCount; - if (unreadCount > 9) { - return true; - } - - return false; - }); - } - - return unreadCount; - } - private readonly handleSectionSelect = (section: SectionType): void => { this.props.onSectionSelected(section); }; diff --git a/ts/components/session/LeftPaneContactSection.tsx b/ts/components/session/LeftPaneContactSection.tsx index 511c18e30..ab4123958 100644 --- a/ts/components/session/LeftPaneContactSection.tsx +++ b/ts/components/session/LeftPaneContactSection.tsx @@ -16,7 +16,6 @@ import { } from './SessionButton'; import { AutoSizer, List } from 'react-virtualized'; import { validateNumber } from '../../types/PhoneNumber'; -import { ActionsPanel } from './ActionsPanel'; import { ConversationType } from '../../state/ducks/conversations'; export interface Props { @@ -79,10 +78,9 @@ export class LeftPaneContactSection extends React.Component { } public renderHeader(): JSX.Element | undefined { + const { receivedFriendsRequest } = this.props; const labels = [window.i18n('contactsHeader'), window.i18n('lists')]; - const friendRequestCount = ActionsPanel.GET_FRIEND_REQUESTS_COUNT( - this.props.conversations - ); + const friendRequestCount = receivedFriendsRequest.length; return LeftPane.RENDER_HEADER( labels, diff --git a/ts/state/ducks/search.ts b/ts/state/ducks/search.ts index 4af5db9a7..b91409176 100644 --- a/ts/state/ducks/search.ts +++ b/ts/state/ducks/search.ts @@ -7,7 +7,8 @@ import { getMessageModel } from '../../shims/Whisper'; import { cleanSearchTerm } from '../../util/cleanSearchTerm'; import { getPrimaryDeviceFor, - searchConversations, searchMessages, + searchConversations, + searchMessages, } from '../../../js/modules/data'; import { makeLookup } from '../../util/makeLookup'; diff --git a/ts/state/selectors/conversations.ts b/ts/state/selectors/conversations.ts index 42dbd6717..a66dc1569 100644 --- a/ts/state/selectors/conversations.ts +++ b/ts/state/selectors/conversations.ts @@ -100,6 +100,7 @@ export const _getLeftPaneLists = ( friends: Array; receivedFriendsRequest: Array; sentFriendsRequest: Array; + unreadCount: number; } => { const values = Object.values(lookup); const sorted = values.sort(comparator); @@ -111,6 +112,8 @@ export const _getLeftPaneLists = ( const sentFriendsRequest: Array = []; const max = sorted.length; + let unreadCount = 0; + for (let i = 0; i < max; i += 1) { let conversation = sorted[i]; @@ -127,6 +130,12 @@ export const _getLeftPaneLists = ( if (conversation.hasReceivedFriendRequest) { receivedFriendsRequest.push(conversation); + } else if ( + unreadCount < 9 && + conversation.isFriend && + conversation.unreadCount > 0 + ) { + unreadCount += conversation.unreadCount; } if (conversation.hasSentFriendRequest) { sentFriendsRequest.push(conversation); @@ -149,6 +158,7 @@ export const _getLeftPaneLists = ( friends, receivedFriendsRequest, sentFriendsRequest, + unreadCount, }; }; diff --git a/ts/state/smart/LeftPane.tsx b/ts/state/smart/LeftPane.tsx index 669fe1b45..ed326c6f5 100644 --- a/ts/state/smart/LeftPane.tsx +++ b/ts/state/smart/LeftPane.tsx @@ -18,7 +18,8 @@ import { getLeftPaneLists, getShowArchived } from '../selectors/conversations'; const mapStateToProps = (state: StateType) => { const showSearch = isSearching(state); - const lists = showSearch ? undefined : getLeftPaneLists(state); + const leftPaneList = getLeftPaneLists(state); + const lists = showSearch ? undefined : leftPaneList; const searchResults = showSearch ? getSearchResults(state) : undefined; return { @@ -30,6 +31,8 @@ const mapStateToProps = (state: StateType) => { searchResults, showArchived: getShowArchived(state), i18n: getIntl(state), + unreadMessageCount: leftPaneList.unreadCount, + receivedFriendRequestCount: leftPaneList.receivedFriendsRequest.length, }; };