diff --git a/lib/components/global-search-index-provider.react.js b/lib/components/global-search-index-provider.react.js new file mode 100644 --- /dev/null +++ b/lib/components/global-search-index-provider.react.js @@ -0,0 +1,42 @@ +// @flow + +import invariant from 'invariant'; +import * as React from 'react'; + +import { useThreadSearchIndex } from '../selectors/nav-selectors.js'; +import SearchIndex from '../shared/search-index.js'; +import { values } from '../utils/objects.js'; +import { useSelector } from '../utils/redux-utils.js'; + +type GlobalSearchIndexContextType = { + +searchIndex: SearchIndex, +}; + +const GlobalSearchIndexContext: React.Context = + React.createContext(); + +type Props = { + +children: React.Node, +}; +function GlobalSearchIndexProvider(props: Props): React.Node { + const threadInfos = useSelector(state => state.threadStore.threadInfos); + const threadInfosArray = React.useMemo( + () => values(threadInfos), + [threadInfos], + ); + const searchIndex = useThreadSearchIndex(threadInfosArray); + const context = React.useMemo(() => ({ searchIndex }), [searchIndex]); + return ( + + {props.children} + + ); +} + +function useGlobalThreadSearchIndex(): SearchIndex { + const globalSearchIndexContext = React.useContext(GlobalSearchIndexContext); + invariant(globalSearchIndexContext, 'globalSearchIndexContext should be set'); + return globalSearchIndexContext.searchIndex; +} + +export { GlobalSearchIndexProvider, useGlobalThreadSearchIndex }; diff --git a/lib/hooks/child-threads.js b/lib/hooks/child-threads.js --- a/lib/hooks/child-threads.js +++ b/lib/hooks/child-threads.js @@ -6,11 +6,11 @@ fetchSingleMostRecentMessagesFromThreadsActionTypes, useFetchSingleMostRecentMessagesFromThreads, } from '../actions/message-actions.js'; +import { useGlobalThreadSearchIndex } from '../components/global-search-index-provider.react.js'; import { type ChatThreadItem, useFilteredChatListData, } from '../selectors/chat-selectors.js'; -import { useGlobalThreadSearchIndex } from '../selectors/nav-selectors.js'; import { childThreadInfos } from '../selectors/thread-selectors.js'; import { useThreadsInChatList } from '../shared/thread-utils.js'; import threadWatcher from '../shared/thread-watcher.js'; diff --git a/lib/hooks/thread-search-hooks.js b/lib/hooks/thread-search-hooks.js --- a/lib/hooks/thread-search-hooks.js +++ b/lib/hooks/thread-search-hooks.js @@ -4,8 +4,8 @@ import { useUsersSupportThickThreads } from './user-identities-hooks.js'; import { searchUsers as searchUserCall } from '../actions/user-actions.js'; +import { useGlobalThreadSearchIndex } from '../components/global-search-index-provider.react.js'; import { useLegacyAshoatKeyserverCall } from '../keyserver-conn/legacy-keyserver-call.js'; -import { useGlobalThreadSearchIndex } from '../selectors/nav-selectors.js'; import { usersWithPersonalThreadSelector } from '../selectors/user-selectors.js'; import { useForwardLookupSearchText, 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 @@ -21,7 +21,6 @@ import { threadTypeIsPrivate } from '../types/thread-types-enum.js'; import type { UserInfo } from '../types/user-types.js'; import { getConfig } from '../utils/config.js'; -import { values } from '../utils/objects.js'; import { useSelector } from '../utils/redux-utils.js'; function timeUntilCalendarRangeExpiration( @@ -192,19 +191,9 @@ }, [threadInfos, viewerID, userInfos, memberMap]); } -function useGlobalThreadSearchIndex(): SearchIndex { - const threadInfos = useSelector(state => state.threadStore.threadInfos); - const threadInfosArray = React.useMemo( - () => values(threadInfos), - [threadInfos], - ); - return useThreadSearchIndex(threadInfosArray); -} - export { timeUntilCalendarRangeExpiration, currentCalendarQuery, useUserSearchIndex, useThreadSearchIndex, - useGlobalThreadSearchIndex, }; diff --git a/native/calendar/thread-picker-modal.react.js b/native/calendar/thread-picker-modal.react.js --- a/native/calendar/thread-picker-modal.react.js +++ b/native/calendar/thread-picker-modal.react.js @@ -8,7 +8,7 @@ createLocalEntry, createLocalEntryActionType, } from 'lib/actions/entry-actions.js'; -import { useGlobalThreadSearchIndex } from 'lib/selectors/nav-selectors.js'; +import { useGlobalThreadSearchIndex } from 'lib/components/global-search-index-provider.react.js'; import { useOnScreenEntryEditableThreadInfos } from 'lib/shared/thread-utils.js'; import { useDispatch } from 'lib/utils/redux-utils.js'; diff --git a/native/root.react.js b/native/root.react.js --- a/native/root.react.js +++ b/native/root.react.js @@ -27,6 +27,7 @@ import { EditUserAvatarProvider } from 'lib/components/edit-user-avatar-provider.react.js'; import { ENSCacheProvider } from 'lib/components/ens-cache-provider.react.js'; import { FarcasterDataHandler } from 'lib/components/farcaster-data-handler.react.js'; +import { GlobalSearchIndexProvider } from 'lib/components/global-search-index-provider.react.js'; import IntegrityHandler from 'lib/components/integrity-handler.react.js'; import { MediaCacheProvider } from 'lib/components/media-cache-provider.react.js'; import { NeynarClientProvider } from 'lib/components/neynar-client-provider.react.js'; @@ -313,9 +314,11 @@ - - - + + + + + diff --git a/web/app.react.js b/web/app.react.js --- a/web/app.react.js +++ b/web/app.react.js @@ -16,6 +16,7 @@ import { ChatMentionContextProvider } from 'lib/components/chat-mention-provider.react.js'; import { EditUserAvatarProvider } from 'lib/components/edit-user-avatar-provider.react.js'; import { FarcasterDataHandler } from 'lib/components/farcaster-data-handler.react.js'; +import { GlobalSearchIndexProvider } from 'lib/components/global-search-index-provider.react.js'; import { ModalProvider, useModalContext, @@ -245,24 +246,26 @@ - - - - - - - - - - - - - - - - - - {content} + + + + + + + + + + + + + + + + + + + {content} + diff --git a/web/modals/threads/thread-picker-modal.react.js b/web/modals/threads/thread-picker-modal.react.js --- a/web/modals/threads/thread-picker-modal.react.js +++ b/web/modals/threads/thread-picker-modal.react.js @@ -3,7 +3,7 @@ import invariant from 'invariant'; import * as React from 'react'; -import { useGlobalThreadSearchIndex } from 'lib/selectors/nav-selectors.js'; +import { useGlobalThreadSearchIndex } from 'lib/components/global-search-index-provider.react.js'; import { useOnScreenEntryEditableThreadInfos, reorderThreadSearchResults,