Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F3323531
D6477.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
3 KB
Referenced Files
None
Subscribers
None
D6477.diff
View Options
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<string>(),
});
- 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<RawThreadInfo | ThreadInfo>,
+): 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,
};
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Thu, Nov 21, 3:44 AM (22 h, 13 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2552532
Default Alt Text
D6477.diff (3 KB)
Attached To
Mode
D6477: [lib] Introduce useThreadSearchIndex hook
Attached
Detach File
Event Timeline
Log In to Comment