diff --git a/lib/hooks/search-threads.js b/lib/hooks/search-threads.js --- a/lib/hooks/search-threads.js +++ b/lib/hooks/search-threads.js @@ -1,5 +1,6 @@ // @flow +import _orderBy from 'lodash/fp/orderBy.js'; import * as React from 'react'; import { useSidebarInfos } from './sidebar-hooks.js'; @@ -99,6 +100,7 @@ } const emptyArray: $ReadOnlyArray = []; +const sortFunc = _orderBy('lastUpdatedTime')('desc'); function useSearchSidebars( threadInfo: ThreadInfo, @@ -167,7 +169,12 @@ })(); }, [childThreadInfos, initialSidebarItems]); - return useSearchThreads(threadInfo, sidebarItems); + const sortedSidebarItems = React.useMemo( + () => sortFunc(sidebarItems), + [sidebarItems], + ); + + return useSearchThreads(threadInfo, sortedSidebarItems); } function useSearchSubchannels( diff --git a/lib/hooks/sidebar-hooks.js b/lib/hooks/sidebar-hooks.js --- a/lib/hooks/sidebar-hooks.js +++ b/lib/hooks/sidebar-hooks.js @@ -44,7 +44,9 @@ mostRecentNonLocalMessage, }); } - result[parentID] = _orderBy('lastUpdatedTime')('desc')(sidebarInfos); + result[parentID] = _orderBy('lastUpdatedAtLeastTime')('desc')( + sidebarInfos, + ); } return result; }, [childThreadInfoByParentID, messageStore, getLastUpdatedTimes]); 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 @@ -115,12 +115,13 @@ : lastUpdatedAtLeastTime; const lastUpdatedTimeIncludingSidebars = (async () => { - if (sidebars.length === 0) { - return await lastUpdatedTime; - } - const [lastUpdatedTimeResult, sidebarLastUpdatedTimeResult] = - await Promise.all([lastUpdatedTime, sidebars[0].lastUpdatedTime]); - return Math.max(lastUpdatedTimeResult, sidebarLastUpdatedTimeResult); + const lastUpdatedTimePromises = [ + lastUpdatedTime, + ...sidebars.map(sidebar => sidebar.lastUpdatedTime), + ]; + const lastUpdatedTimes = await Promise.all(lastUpdatedTimePromises); + const max = lastUpdatedTimes.reduce((a, b) => Math.max(a, b), -1); + return max; })(); const allInitialSidebarItems = getAllInitialSidebarItems(sidebars); diff --git a/lib/shared/sidebar-item-utils.js b/lib/shared/sidebar-item-utils.js --- a/lib/shared/sidebar-item-utils.js +++ b/lib/shared/sidebar-item-utils.js @@ -1,5 +1,7 @@ // @flow +import _orderBy from 'lodash/fp/orderBy.js'; + import type { ThreadInfo } from '../types/minimally-encoded-thread-permissions-types.js'; import { maxReadSidebars, @@ -22,17 +24,21 @@ } | { +type: 'spacer' }; +const sortFunc = _orderBy('lastUpdatedTime')('desc'); + function getSidebarItems( allSidebarItems: $ReadOnlyArray, ): SidebarItem[] { - const numUnreadSidebars = allSidebarItems.filter( + const sorted = sortFunc(allSidebarItems); + + const numUnreadSidebars = sorted.filter( sidebar => sidebar.threadInfo.currentUser.unread, ).length; let numReadSidebarsToShow = maxReadSidebars - numUnreadSidebars; const threeDaysAgo = Date.now() - threeDays; const sidebarItems: SidebarItem[] = []; - for (const sidebar of allSidebarItems) { + for (const sidebar of sorted) { if (sidebarItems.length >= maxUnreadSidebars) { break; } else if (sidebar.threadInfo.currentUser.unread) { @@ -46,14 +52,14 @@ } } - const numReadButRecentSidebars = allSidebarItems.filter( + const numReadButRecentSidebars = sorted.filter( sidebar => !sidebar.threadInfo.currentUser.unread && sidebar.lastUpdatedTime > threeDaysAgo, ).length; if ( sidebarItems.length < numUnreadSidebars + numReadButRecentSidebars || - (sidebarItems.length < allSidebarItems.length && sidebarItems.length > 0) + (sidebarItems.length < sorted.length && sidebarItems.length > 0) ) { sidebarItems.push({ type: 'seeMore',