|
1 | 1 | /* @flow strict-local */
|
| 2 | +import invariant from 'invariant'; |
2 | 3 | import { createSelector } from 'reselect';
|
3 | 4 |
|
4 | 5 | import type { GlobalState, Message, PmConversationData, Selector, User } from '../types';
|
5 | 6 | import { getPrivateMessages } from '../message/messageSelectors';
|
6 |
| -import { getAllUsersById, getOwnUser } from '../users/userSelectors'; |
| 7 | +import { getAllUsersById, getOwnUser, getOwnUserId } from '../users/userSelectors'; |
7 | 8 | import { getUnreadByPms, getUnreadByHuddles } from '../unread/unreadSelectors';
|
8 |
| -import { pmUnreadsKeyFromMessage, pmKeyRecipientUsersFromMessage } from '../utils/recipient'; |
| 9 | +import { |
| 10 | + pmUnreadsKeyFromMessage, |
| 11 | + pmKeyRecipientUsersFromMessage, |
| 12 | + pmKeyRecipientsFromIds, |
| 13 | + pmUnreadsKeyFromPmKeyIds, |
| 14 | +} from '../utils/recipient'; |
9 | 15 | import { getServerVersion } from '../account/accountsSelectors';
|
10 | 16 | import * as model from './pmConversationsModel';
|
| 17 | +import { type PmConversationsState } from './pmConversationsModel'; |
11 | 18 |
|
12 | 19 | function unreadCount(unreadsKey, unreadPms, unreadHuddles): number {
|
13 | 20 | // This business of looking in one place and then the other is kind
|
@@ -65,9 +72,50 @@ export const getRecentConversationsLegacy: Selector<PmConversationData[]> = crea
|
65 | 72 | },
|
66 | 73 | );
|
67 | 74 |
|
68 |
| -export const getRecentConversationsModern: Selector<PmConversationData[]> = state => |
69 |
| - // TODO implement for real |
70 |
| - getRecentConversationsLegacy(state); |
| 75 | +export const getRecentConversationsModern: Selector<PmConversationData[]> = createSelector( |
| 76 | + state => state.pmConversations, |
| 77 | + getUnreadByPms, |
| 78 | + getUnreadByHuddles, |
| 79 | + getAllUsersById, |
| 80 | + getOwnUserId, |
| 81 | + // This is defined separately, just below. When this is defined inline, |
| 82 | + // if there's a type error in it, the message Flow gives is often pretty |
| 83 | + // terrible: just highlighting the whole thing and pointing at something |
| 84 | + // in the `reselect` libdef. Defining it separately seems to help. |
| 85 | + getRecentConversationsModernImpl, // eslint-disable-line no-use-before-define |
| 86 | +); |
| 87 | + |
| 88 | +function getRecentConversationsModernImpl( |
| 89 | + { sorted, map }: PmConversationsState, |
| 90 | + unreadPms, |
| 91 | + unreadHuddles, |
| 92 | + allUsersById, |
| 93 | + ownUserId, |
| 94 | +): PmConversationData[] { |
| 95 | + return sorted |
| 96 | + .toSeq() |
| 97 | + .map(recentsKey => { |
| 98 | + const keyRecipients = pmKeyRecipientsFromIds( |
| 99 | + model.usersOfKey(recentsKey), |
| 100 | + allUsersById, |
| 101 | + ownUserId, |
| 102 | + ); |
| 103 | + if (keyRecipients === null) { |
| 104 | + return null; |
| 105 | + } |
| 106 | + |
| 107 | + const unreadsKey = pmUnreadsKeyFromPmKeyIds(keyRecipients.map(r => r.user_id), ownUserId); |
| 108 | + |
| 109 | + const msgId = map.get(recentsKey); |
| 110 | + invariant(msgId !== undefined, 'pm-conversations: key in sorted should be in map'); |
| 111 | + |
| 112 | + const unread = unreadCount(unreadsKey, unreadPms, unreadHuddles); |
| 113 | + |
| 114 | + return { key: unreadsKey, keyRecipients, msgId, unread }; |
| 115 | + }) |
| 116 | + .filter(Boolean) |
| 117 | + .toArray(); |
| 118 | +} |
71 | 119 |
|
72 | 120 | const getServerIsOld: Selector<boolean> = createSelector(
|
73 | 121 | getServerVersion,
|
|
0 commit comments