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 @@ -6,9 +6,9 @@ type ChatThreadItem, useFilteredChatListData, } from '../selectors/chat-selectors'; +import { useThreadSearchIndex } from '../selectors/nav-selectors'; import { sidebarInfoSelector } from '../selectors/thread-selectors'; -import SearchIndex from '../shared/search-index'; -import { threadIsChannel, threadSearchText } from '../shared/thread-utils'; +import { threadIsChannel } from '../shared/thread-utils'; import type { SetState } from '../types/hook-types'; import type { SidebarInfo, @@ -39,8 +39,6 @@ results: new Set(), }); - const userInfos = useSelector(state => state.userStore.userInfos); - const listData = React.useMemo(() => { if (!searchState.text) { return childThreadInfos; @@ -50,20 +48,9 @@ ); }, [childThreadInfos, searchState]); - const viewerID = useSelector( - state => state.currentUserInfo && state.currentUserInfo.id, + const searchIndex = useThreadSearchIndex( + childThreadInfos.map(childThreadInfo => childThreadInfo.threadInfo), ); - const searchIndex = React.useMemo(() => { - const index = new SearchIndex(); - for (const thread of childThreadInfos) { - const threadInfoFromItem = thread.threadInfo; - index.addEntry( - threadInfoFromItem.id, - threadSearchText(threadInfoFromItem, userInfos, viewerID), - ); - } - return index; - }, [childThreadInfos, userInfos, viewerID]); const onChangeSearchInputText = React.useCallback( (text: string) => { diff --git a/lib/selectors/nav-selectors.js b/lib/selectors/nav-selectors.js --- a/lib/selectors/nav-selectors.js +++ b/lib/selectors/nav-selectors.js @@ -1,5 +1,6 @@ // @flow +import * as React from 'react'; import { createSelector } from 'reselect'; import SearchIndex from '../shared/search-index'; @@ -9,9 +10,10 @@ import type { CalendarFilter } from '../types/filter-types'; import type { BaseNavInfo } from '../types/nav-types'; import type { BaseAppState } from '../types/redux-types'; -import type { RawThreadInfo } from '../types/thread-types'; +import type { RawThreadInfo, ThreadInfo } from '../types/thread-types'; import type { UserInfos } from '../types/user-types'; import { getConfig } from '../utils/config'; +import { useSelector } from '../utils/redux-utils'; function timeUntilCalendarRangeExpiration( lastUserInteractionCalendar: number, @@ -65,6 +67,25 @@ }, ); +function useThreadSearchIndex( + threadInfos: $ReadOnlyArray, +): SearchIndex { + const userInfos = useSelector(state => state.userStore.userInfos); + const viewerID = useSelector( + state => state.currentUserInfo && state.currentUserInfo.id, + ); + return React.useMemo(() => { + const searchIndex = new SearchIndex(); + for (const threadInfo of threadInfos) { + searchIndex.addEntry( + threadInfo.id, + threadSearchText(threadInfo, userInfos, viewerID), + ); + } + return searchIndex; + }, [threadInfos, userInfos, viewerID]); +} + const threadSearchIndex: ( state: BaseAppState<*>, ) => SearchIndex = createSelector( @@ -92,4 +113,5 @@ timeUntilCalendarRangeExpiration, currentCalendarQuery, threadSearchIndex, + useThreadSearchIndex, };