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 @@ -7,7 +7,7 @@ useFilteredChatListData, } from '../selectors/chat-selectors.js'; import { useThreadSearchIndex } from '../selectors/nav-selectors.js'; -import { sidebarInfoSelector } from '../selectors/thread-selectors.js'; +import { sidebarInfoSelector } from '../selectors/sidebar-selectors.js'; import { threadIsChannel } from '../shared/thread-utils.js'; import type { SetState } from '../types/hook-types.js'; import type { 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 @@ -10,10 +10,10 @@ import { createObjectSelector } from 'reselect-map'; import { - sidebarInfoSelector, threadInfoFromSourceMessageIDSelector, threadInfoSelector, } from './thread-selectors.js'; +import { sidebarInfoSelector } from '../selectors/sidebar-selectors.js'; import { createMessageInfo, getMostRecentNonLocalMessageID, diff --git a/lib/selectors/sidebar-selectors.js b/lib/selectors/sidebar-selectors.js new file mode 100644 --- /dev/null +++ b/lib/selectors/sidebar-selectors.js @@ -0,0 +1,63 @@ +// @flow + +import _orderBy from 'lodash/fp/orderBy.js'; +import { createObjectSelector } from 'reselect-map'; + +import { childThreadInfos } from './thread-selectors.js'; +import { getMostRecentNonLocalMessageID } from '../shared/message-utils.js'; +import { threadInChatList } from '../shared/thread-utils.js'; +import type { MessageStore, RawMessageInfo } from '../types/message-types.js'; +import type { ThreadInfo } from '../types/minimally-encoded-thread-permissions-types.js'; +import type { BaseAppState } from '../types/redux-types.js'; +import { threadTypes } from '../types/thread-types-enum.js'; +import type { SidebarInfo } from '../types/thread-types.js'; + +function getMostRecentRawMessageInfo( + threadInfo: ThreadInfo, + messageStore: MessageStore, +): ?RawMessageInfo { + const thread = messageStore.threads[threadInfo.id]; + if (!thread) { + return null; + } + for (const messageID of thread.messageIDs) { + return messageStore.messages[messageID]; + } + return null; +} + +const sidebarInfoSelector: (state: BaseAppState<>) => { + +[id: string]: $ReadOnlyArray, +} = createObjectSelector( + childThreadInfos, + (state: BaseAppState<>) => state.messageStore, + (childThreads: $ReadOnlyArray, messageStore: MessageStore) => { + const sidebarInfos = []; + for (const childThreadInfo of childThreads) { + if ( + !threadInChatList(childThreadInfo) || + childThreadInfo.type !== threadTypes.SIDEBAR + ) { + continue; + } + const mostRecentRawMessageInfo = getMostRecentRawMessageInfo( + childThreadInfo, + messageStore, + ); + const lastUpdatedTime = + mostRecentRawMessageInfo?.time ?? childThreadInfo.creationTime; + const mostRecentNonLocalMessage = getMostRecentNonLocalMessageID( + childThreadInfo.id, + messageStore, + ); + sidebarInfos.push({ + threadInfo: childThreadInfo, + lastUpdatedTime, + mostRecentNonLocalMessage, + }); + } + return _orderBy('lastUpdatedTime')('desc')(sidebarInfos); + }, +); + +export { sidebarInfoSelector }; diff --git a/lib/selectors/thread-selectors.js b/lib/selectors/thread-selectors.js --- a/lib/selectors/thread-selectors.js +++ b/lib/selectors/thread-selectors.js @@ -6,7 +6,6 @@ import _groupBy from 'lodash/fp/groupBy.js'; import _map from 'lodash/fp/map.js'; import _mapValues from 'lodash/fp/mapValues.js'; -import _orderBy from 'lodash/fp/orderBy.js'; import _some from 'lodash/fp/some.js'; import _sortBy from 'lodash/fp/sortBy.js'; import _memoize from 'lodash/memoize.js'; @@ -25,13 +24,11 @@ getRandomDefaultEmojiAvatar, } from '../shared/avatar-utils.js'; import { createEntryInfo } from '../shared/entry-utils.js'; -import { getMostRecentNonLocalMessageID } from '../shared/message-utils.js'; import { threadInHomeChatList, threadInBackgroundChatList, threadInFilterList, threadInfoFromRawThreadInfo, - threadInChatList, threadHasAdminRole, roleIsAdminRole, threadIsPending, @@ -39,7 +36,7 @@ } from '../shared/thread-utils.js'; import type { ClientAvatar, ClientEmojiAvatar } from '../types/avatar-types'; import type { EntryInfo } from '../types/entry-types.js'; -import type { MessageStore, RawMessageInfo } from '../types/message-types.js'; +import type { MessageStore } from '../types/message-types.js'; import type { RelativeMemberInfo, ThreadInfo, @@ -52,7 +49,6 @@ type ThreadType, } from '../types/thread-types-enum.js'; import type { - SidebarInfo, MixedRawThreadInfos, RawThreadInfos, } from '../types/thread-types.js'; @@ -221,54 +217,6 @@ }, ); -function getMostRecentRawMessageInfo( - threadInfo: ThreadInfo, - messageStore: MessageStore, -): ?RawMessageInfo { - const thread = messageStore.threads[threadInfo.id]; - if (!thread) { - return null; - } - for (const messageID of thread.messageIDs) { - return messageStore.messages[messageID]; - } - return null; -} - -const sidebarInfoSelector: (state: BaseAppState<>) => { - +[id: string]: $ReadOnlyArray, -} = createObjectSelector( - childThreadInfos, - (state: BaseAppState<>) => state.messageStore, - (childThreads: $ReadOnlyArray, messageStore: MessageStore) => { - const sidebarInfos = []; - for (const childThreadInfo of childThreads) { - if ( - !threadInChatList(childThreadInfo) || - childThreadInfo.type !== threadTypes.SIDEBAR - ) { - continue; - } - const mostRecentRawMessageInfo = getMostRecentRawMessageInfo( - childThreadInfo, - messageStore, - ); - const lastUpdatedTime = - mostRecentRawMessageInfo?.time ?? childThreadInfo.creationTime; - const mostRecentNonLocalMessage = getMostRecentNonLocalMessageID( - childThreadInfo.id, - messageStore, - ); - sidebarInfos.push({ - threadInfo: childThreadInfo, - lastUpdatedTime, - mostRecentNonLocalMessage, - }); - } - return _orderBy('lastUpdatedTime')('desc')(sidebarInfos); - }, -); - const unreadCount: (state: BaseAppState<>) => number = createSelector( (state: BaseAppState<>) => state.threadStore.threadInfos, (threadInfos: RawThreadInfos): number => @@ -567,7 +515,6 @@ otherUsersButNoOtherAdmins, mostRecentlyReadThread, mostRecentlyReadThreadSelector, - sidebarInfoSelector, threadInfoFromSourceMessageIDSelector, pendingToRealizedThreadIDsSelector, savedEmojiAvatarSelectorForThread, diff --git a/web/selectors/chat-selectors.js b/web/selectors/chat-selectors.js --- a/web/selectors/chat-selectors.js +++ b/web/selectors/chat-selectors.js @@ -8,10 +8,8 @@ createChatThreadItem, messageInfoSelector, } from 'lib/selectors/chat-selectors.js'; -import { - sidebarInfoSelector, - threadInfoSelector, -} from 'lib/selectors/thread-selectors.js'; +import { sidebarInfoSelector } from 'lib/selectors/sidebar-selectors.js'; +import { threadInfoSelector } from 'lib/selectors/thread-selectors.js'; import { threadIsPending } from 'lib/shared/thread-utils.js'; import type { MessageInfo, MessageStore } from 'lib/types/message-types.js'; import type { ThreadInfo } from 'lib/types/minimally-encoded-thread-permissions-types.js';