2018-03-07 00:28:19 +00:00
|
|
|
// @flow
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Selector for calculating the number of unread chat messages.
|
|
|
|
*
|
|
|
|
* @param {Object} state - The redux state.
|
|
|
|
* @returns {number} The number of unread messages.
|
|
|
|
*/
|
|
|
|
export function getUnreadCount(state: Object) {
|
|
|
|
const { lastReadMessage, messages } = state['features/chat'];
|
|
|
|
const messagesCount = messages.length;
|
|
|
|
|
|
|
|
if (!messagesCount) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-04-25 13:17:49 +00:00
|
|
|
if (navigator.product === 'ReactNative') {
|
|
|
|
// React native stores the messages in a reversed order.
|
|
|
|
return messages.indexOf(lastReadMessage);
|
|
|
|
}
|
|
|
|
|
2018-03-07 00:28:19 +00:00
|
|
|
const lastReadIndex = messages.lastIndexOf(lastReadMessage);
|
|
|
|
|
|
|
|
return messagesCount - (lastReadIndex + 1);
|
|
|
|
}
|