diff --git a/lib/selectors/chat-selectors.js b/lib/selectors/chat-selectors.js --- a/lib/selectors/chat-selectors.js +++ b/lib/selectors/chat-selectors.js @@ -121,97 +121,88 @@ : threadInfo.creationTime; } -function createChatThreadItem( - threadInfo: ThreadInfo, - messageStore: MessageStore, - messages: { +[id: string]: ?MessageInfo }, - sidebarInfos: ?$ReadOnlyArray, -): ChatThreadItem { - const mostRecentMessageInfo = getMostRecentMessageInfo( - threadInfo, - messageStore, - messages, - ); - const mostRecentNonLocalMessage = getMostRecentNonLocalMessageID( - threadInfo.id, - messageStore, - ); - const lastUpdatedTime = getLastUpdatedTime(threadInfo, mostRecentMessageInfo); - - const sidebars = sidebarInfos ?? []; - const allSidebarItems = sidebars.map(sidebarInfo => ({ - type: 'sidebar', - ...sidebarInfo, - })); - const lastUpdatedTimeIncludingSidebars = - allSidebarItems.length > 0 - ? Math.max(lastUpdatedTime, allSidebarItems[0].lastUpdatedTime) - : lastUpdatedTime; - - const numUnreadSidebars = allSidebarItems.filter( - sidebar => sidebar.threadInfo.currentUser.unread, - ).length; - let numReadSidebarsToShow = maxReadSidebars - numUnreadSidebars; - const threeDaysAgo = Date.now() - threeDays; - - const sidebarItems: SidebarItem[] = []; - for (const sidebar of allSidebarItems) { - if (sidebarItems.length >= maxUnreadSidebars) { - break; - } else if (sidebar.threadInfo.currentUser.unread) { - sidebarItems.push(sidebar); - } else if ( - sidebar.lastUpdatedTime > threeDaysAgo && - numReadSidebarsToShow > 0 - ) { - sidebarItems.push(sidebar); - numReadSidebarsToShow--; - } - } - - const numReadButRecentSidebars = allSidebarItems.filter( - sidebar => - !sidebar.threadInfo.currentUser.unread && - sidebar.lastUpdatedTime > threeDaysAgo, - ).length; - if ( - sidebarItems.length < numUnreadSidebars + numReadButRecentSidebars || - (sidebarItems.length < allSidebarItems.length && sidebarItems.length > 0) - ) { - sidebarItems.push({ - type: 'seeMore', - unread: numUnreadSidebars > maxUnreadSidebars, - }); - } - if (sidebarItems.length !== 0) { - sidebarItems.push({ - type: 'spacer', - }); - } - - return { - type: 'chatThreadItem', - threadInfo, - mostRecentMessageInfo, - mostRecentNonLocalMessage, - lastUpdatedTime, - lastUpdatedTimeIncludingSidebars, - sidebars: sidebarItems, - }; -} - function useCreateChatThreadItem(): ThreadInfo => ChatThreadItem { const messageInfos = useSelector(messageInfoSelector); const sidebarInfos = useSelector(sidebarInfoSelector); const messageStore = useSelector(state => state.messageStore); return React.useCallback( - threadInfo => - createChatThreadItem( + threadInfo => { + const mostRecentMessageInfo = getMostRecentMessageInfo( threadInfo, messageStore, messageInfos, - sidebarInfos[threadInfo.id], - ), + ); + const mostRecentNonLocalMessage = getMostRecentNonLocalMessageID( + threadInfo.id, + messageStore, + ); + const lastUpdatedTime = getLastUpdatedTime( + threadInfo, + mostRecentMessageInfo, + ); + + const sidebars = sidebarInfos[threadInfo.id] ?? []; + const allSidebarItems = sidebars.map(sidebarInfo => ({ + type: 'sidebar', + ...sidebarInfo, + })); + const lastUpdatedTimeIncludingSidebars = + allSidebarItems.length > 0 + ? Math.max(lastUpdatedTime, allSidebarItems[0].lastUpdatedTime) + : lastUpdatedTime; + + const numUnreadSidebars = allSidebarItems.filter( + sidebar => sidebar.threadInfo.currentUser.unread, + ).length; + let numReadSidebarsToShow = maxReadSidebars - numUnreadSidebars; + const threeDaysAgo = Date.now() - threeDays; + + const sidebarItems: SidebarItem[] = []; + for (const sidebar of allSidebarItems) { + if (sidebarItems.length >= maxUnreadSidebars) { + break; + } else if (sidebar.threadInfo.currentUser.unread) { + sidebarItems.push(sidebar); + } else if ( + sidebar.lastUpdatedTime > threeDaysAgo && + numReadSidebarsToShow > 0 + ) { + sidebarItems.push(sidebar); + numReadSidebarsToShow--; + } + } + + const numReadButRecentSidebars = allSidebarItems.filter( + sidebar => + !sidebar.threadInfo.currentUser.unread && + sidebar.lastUpdatedTime > threeDaysAgo, + ).length; + if ( + sidebarItems.length < numUnreadSidebars + numReadButRecentSidebars || + (sidebarItems.length < allSidebarItems.length && + sidebarItems.length > 0) + ) { + sidebarItems.push({ + type: 'seeMore', + unread: numUnreadSidebars > maxUnreadSidebars, + }); + } + if (sidebarItems.length !== 0) { + sidebarItems.push({ + type: 'spacer', + }); + } + + return { + type: 'chatThreadItem', + threadInfo, + mostRecentMessageInfo, + mostRecentNonLocalMessage, + lastUpdatedTime, + lastUpdatedTimeIncludingSidebars, + sidebars: sidebarItems, + }; + }, [messageInfos, messageStore, sidebarInfos], ); }