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 @@ -2,7 +2,7 @@ import * as React from 'react'; -import { sidebarInfoSelector } from '../hooks/sidebar-hooks.js'; +import { useSidebarInfos } from './sidebar-hooks.js'; import { type ChatThreadItem, useFilteredChatListData, @@ -15,7 +15,6 @@ RawThreadInfo, } from '../types/minimally-encoded-thread-permissions-types.js'; import type { SidebarInfo } from '../types/thread-types.js'; -import { useSelector } from '../utils/redux-utils.js'; export type ThreadSearchState = { +text: string, @@ -90,12 +89,13 @@ ); } +const emptyArray: $ReadOnlyArray = []; + function useSearchSidebars( threadInfo: ThreadInfo, ): SearchThreadsResult { - const childThreadInfos = useSelector( - state => sidebarInfoSelector(state)[threadInfo.id] ?? [], - ); + const sidebarsByParentID = useSidebarInfos(); + const childThreadInfos = sidebarsByParentID[threadInfo.id] ?? emptyArray; return useSearchThreads(threadInfo, childThreadInfos); } 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 @@ -1,16 +1,16 @@ // @flow import _orderBy from 'lodash/fp/orderBy.js'; -import { createObjectSelector } from 'reselect-map'; +import * as React from 'react'; import { childThreadInfos } from '../selectors/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 { threadTypeIsSidebar } from '../types/thread-types-enum.js'; import type { SidebarInfo } from '../types/thread-types.js'; +import { useSelector } from '../utils/redux-utils.js'; function getMostRecentRawMessageInfo( threadInfo: ThreadInfo, @@ -26,38 +26,42 @@ 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) || - !threadTypeIsSidebar(childThreadInfo.type) - ) { - continue; +function useSidebarInfos(): { +[id: string]: $ReadOnlyArray } { + const childThreadInfoByParentID = useSelector(childThreadInfos); + const messageStore = useSelector(state => state.messageStore); + + return React.useMemo(() => { + const result: { [id: string]: $ReadOnlyArray } = {}; + for (const parentID in childThreadInfoByParentID) { + const childThreads = childThreadInfoByParentID[parentID]; + const sidebarInfos = []; + for (const childThreadInfo of childThreads) { + if ( + !threadInChatList(childThreadInfo) || + !threadTypeIsSidebar(childThreadInfo.type) + ) { + continue; + } + const mostRecentRawMessageInfo = getMostRecentRawMessageInfo( + childThreadInfo, + messageStore, + ); + const lastUpdatedTime = + mostRecentRawMessageInfo?.time ?? childThreadInfo.creationTime; + const mostRecentNonLocalMessage = getMostRecentNonLocalMessageID( + childThreadInfo.id, + messageStore, + ); + sidebarInfos.push({ + threadInfo: childThreadInfo, + lastUpdatedTime, + mostRecentNonLocalMessage, + }); } - const mostRecentRawMessageInfo = getMostRecentRawMessageInfo( - childThreadInfo, - messageStore, - ); - const lastUpdatedTime = - mostRecentRawMessageInfo?.time ?? childThreadInfo.creationTime; - const mostRecentNonLocalMessage = getMostRecentNonLocalMessageID( - childThreadInfo.id, - messageStore, - ); - sidebarInfos.push({ - threadInfo: childThreadInfo, - lastUpdatedTime, - mostRecentNonLocalMessage, - }); + result[parentID] = _orderBy('lastUpdatedTime')('desc')(sidebarInfos); } - return _orderBy('lastUpdatedTime')('desc')(sidebarInfos); - }, -); + return result; + }, [childThreadInfoByParentID, messageStore]); +} -export { sidebarInfoSelector }; +export { useSidebarInfos }; 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 @@ -13,7 +13,7 @@ threadInfoFromSourceMessageIDSelector, threadInfoSelector, } from './thread-selectors.js'; -import { sidebarInfoSelector } from '../hooks/sidebar-hooks.js'; +import { useSidebarInfos } from '../hooks/sidebar-hooks.js'; import { createMessageInfo, getMostRecentNonLocalMessageID, @@ -113,7 +113,7 @@ function useCreateChatThreadItem(): ThreadInfo => ChatThreadItem { const messageInfos = useSelector(messageInfoSelector); - const sidebarInfos = useSelector(sidebarInfoSelector); + const sidebarInfos = useSidebarInfos(); const messageStore = useSelector(state => state.messageStore); return React.useCallback( threadInfo => {