Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove redundant, stringly-typed fields from PmConversationData #4116

Closed
Closed
Changes from 1 commit
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
selectors [nfc]: Rename some variables, mostly for clarity.
... but also to simplify further refactoring.
rk-for-zulip committed May 18, 2020
commit b7096014c1cb64bd4192a9158ffe3131c2721fb8
26 changes: 11 additions & 15 deletions src/pm-conversations/pmConversationsSelectors.js
Original file line number Diff line number Diff line change
@@ -18,30 +18,26 @@ export const getRecentConversations: Selector<PmConversationData[]> = createSele
unreadPms: { [number]: number },
unreadHuddles: { [string]: number },
): PmConversationData[] => {
const recipients = messages.map(msg => ({
const items = messages.map(msg => ({
ids: pmUnreadsKeyFromMessage(msg, ownUser.user_id),
emails: normalizeRecipientsSansMe(msg.display_recipient, ownUser.email),
recipients: normalizeRecipientsSansMe(msg.display_recipient, ownUser.email),
msgId: msg.id,
}));

const latestByRecipient = new Map();
recipients.forEach(recipient => {
const prev = latestByRecipient.get(recipient.emails);
if (!prev || recipient.msgId > prev.msgId) {
latestByRecipient.set(recipient.emails, {
ids: recipient.ids,
recipients: recipient.emails,
msgId: recipient.msgId,
});
const latestByRecipients = new Map();
items.forEach(item => {
const prev = latestByRecipients.get(item.recipients);
if (!prev || item.msgId > prev.msgId) {
latestByRecipients.set(item.recipients, item);
}
});

const sortedByMostRecent = Array.from(latestByRecipient.values()).sort(
const sortedByMostRecent = Array.from(latestByRecipients.values()).sort(
(a, b) => +b.msgId - +a.msgId,
);

return sortedByMostRecent.map(recipient => ({
...recipient,
return sortedByMostRecent.map(conversation => ({
...conversation,
unread:
// This business of looking in one place and then the other is kind
// of messy. Fortunately it always works, because the key spaces
@@ -50,7 +46,7 @@ export const getRecentConversations: Selector<PmConversationData[]> = createSele
/* $FlowFixMe: The keys of unreadPms are logically numbers, but because it's an object they
end up converted to strings, so this access with string keys works. We should probably use
a Map for this and similar maps. */
unreadPms[recipient.ids] || unreadHuddles[recipient.ids],
unreadPms[conversation.ids] || unreadHuddles[conversation.ids],
}));
},
);