count globally number of message unread in selector

This commit is contained in:
Audric Ackermann 2020-01-07 15:43:49 +11:00
parent 9921be8fcb
commit 7cac734ff5
6 changed files with 29 additions and 51 deletions

View File

@ -44,6 +44,7 @@ interface Props {
friends: Array<ConversationType>;
sentFriendsRequest: Array<ConversationListItemPropsType>;
receivedFriendsRequest: Array<ConversationListItemPropsType>;
unreadMessageCount: number;
searchResults?: SearchResultsProps;
searchTerm: string;
isSecondaryDevice: boolean;
@ -170,6 +171,8 @@ export class LeftPane extends React.Component<Props, State> {
selectedSection={this.state.selectedSection}
onSectionSelected={this.handleSectionSelected}
conversations={this.props.conversations}
receivedFriendsRequest={this.props.receivedFriendsRequest}
unreadMessageCount={this.props.unreadMessageCount}
/>
<div className="module-left-pane">{this.renderSection()}</div>
</div>

View File

@ -20,6 +20,8 @@ interface Props {
onSectionSelected: any;
selectedSection: SectionType;
conversations: Array<ConversationListItemPropsType> | undefined;
receivedFriendsRequest: Array<ConversationListItemPropsType>;
unreadMessageCount: number;
}
const Section = ({
@ -122,26 +124,6 @@ export class ActionsPanel extends React.Component<Props, State> {
};
}
public static GET_FRIEND_REQUESTS_COUNT(
conversations: Array<ConversationListItemPropsType> | 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<Props, State> {
}
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<Props, State> {
);
}
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);
};

View File

@ -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<Props, State> {
}
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,

View File

@ -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';

View File

@ -100,6 +100,7 @@ export const _getLeftPaneLists = (
friends: Array<ConversationType>;
receivedFriendsRequest: Array<ConversationListItemPropsType>;
sentFriendsRequest: Array<ConversationListItemPropsType>;
unreadCount: number;
} => {
const values = Object.values(lookup);
const sorted = values.sort(comparator);
@ -111,6 +112,8 @@ export const _getLeftPaneLists = (
const sentFriendsRequest: Array<ConversationListItemPropsType> = [];
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,
};
};

View File

@ -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,
};
};