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 @@ -23,6 +23,10 @@ sortMessageInfoList, } from '../shared/message-utils.js'; import { messageSpecs } from '../shared/messages/message-specs.js'; +import { + getSidebarItems, + type SidebarItem, +} from '../shared/sidebar-item-utils.js'; import { threadInChatList, threadIsPending } from '../shared/thread-utils.js'; import { messageTypes } from '../types/message-types-enum.js'; import { @@ -39,32 +43,15 @@ } from '../types/minimally-encoded-thread-permissions-types.js'; import type { BaseAppState } from '../types/redux-types.js'; import { threadTypeIsSidebar } from '../types/thread-types-enum.js'; -import { - maxReadSidebars, - maxUnreadSidebars, - type SidebarInfo, -} from '../types/thread-types.js'; import type { AccountUserInfo, RelativeUserInfo, UserInfo, } from '../types/user-types.js'; -import { threeDays } from '../utils/date-utils.js'; import type { EntityText } from '../utils/entity-text.js'; import memoize2 from '../utils/memoize.js'; import { useSelector } from '../utils/redux-utils.js'; -export type SidebarItem = - | { - ...SidebarInfo, - +type: 'sidebar', - } - | { - +type: 'seeMore', - +unread: boolean, - } - | { +type: 'spacer' }; - export type ChatThreadItem = { +type: 'chatThreadItem', +threadInfo: ThreadInfo, @@ -121,47 +108,7 @@ ? 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', - }); - } + const sidebarItems = getSidebarItems(allSidebarItems); return { type: 'chatThreadItem', diff --git a/lib/shared/sidebar-item-utils.js b/lib/shared/sidebar-item-utils.js new file mode 100644 --- /dev/null +++ b/lib/shared/sidebar-item-utils.js @@ -0,0 +1,68 @@ +// @flow + +import type { ThreadInfo } from '../types/minimally-encoded-thread-permissions-types.js'; +import { maxReadSidebars, maxUnreadSidebars } from '../types/thread-types.js'; +import { threeDays } from '../utils/date-utils.js'; + +type SidebarThreadItem = { + +type: 'sidebar', + +threadInfo: ThreadInfo, + +mostRecentNonLocalMessage: ?string, + +lastUpdatedTime: number, +}; +export type SidebarItem = + | SidebarThreadItem + | { + +type: 'seeMore', + +unread: boolean, + } + | { +type: 'spacer' }; + +function getSidebarItems( + allSidebarItems: $ReadOnlyArray, +): SidebarItem[] { + 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 sidebarItems; +} + +export { getSidebarItems }; diff --git a/lib/shared/thread-utils.js b/lib/shared/thread-utils.js --- a/lib/shared/thread-utils.js +++ b/lib/shared/thread-utils.js @@ -12,6 +12,7 @@ import { generatePendingThreadColor } from './color-utils.js'; import { extractUserMentionsFromText } from './mention-utils.js'; import { relationshipBlockedInEitherDirection } from './relationship-utils.js'; +import type { SidebarItem } from './sidebar-item-utils.js'; import ashoat from '../facts/ashoat.js'; import genesis from '../facts/genesis.js'; import { useLoggedInUserInfo } from '../hooks/account-hooks.js'; @@ -30,10 +31,7 @@ getAllThreadPermissions, makePermissionsBlob, } from '../permissions/thread-permissions.js'; -import type { - ChatThreadItem, - SidebarItem, -} from '../selectors/chat-selectors.js'; +import type { ChatThreadItem } from '../selectors/chat-selectors.js'; import { threadInfoSelector, pendingToRealizedThreadIDsSelector,