diff --git a/lib/hooks/thread-search-hooks.js b/lib/hooks/thread-search-hooks.js new file mode 100644 --- /dev/null +++ b/lib/hooks/thread-search-hooks.js @@ -0,0 +1,84 @@ +// @flow + +import * as React from 'react'; + +import { searchUsers as searchUserCall } from '../actions/user-actions.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, + useSearchUsers, +} from '../shared/search-utils.js'; +import type { GlobalAccountUserInfo } from '../types/user-types.js'; +import { useSelector } from '../utils/redux-utils.js'; +import { usingCommServicesAccessToken } from '../utils/services-utils.js'; + +type ThreadListSearchResult = { + +threadSearchResults: $ReadOnlySet, + +usersSearchResults: $ReadOnlyArray, +}; + +function useThreadListSearch( + searchText: string, + viewerID: ?string, +): ThreadListSearchResult { + const usersWithPersonalThread = useSelector(usersWithPersonalThreadSelector); + const forwardLookupSearchText = useForwardLookupSearchText(searchText); + + const filterAndSetUserResults = React.useCallback( + (userInfos: $ReadOnlyArray) => { + const usersResults = userInfos.filter( + info => !usersWithPersonalThread.has(info.id) && info.id !== viewerID, + ); + setUsersSearchResults(usersResults); + }, + [usersWithPersonalThread, viewerID], + ); + + const legacyCallSearchUsers = useLegacyAshoatKeyserverCall(searchUserCall); + const legacySearchUsers = React.useCallback( + async (usernamePrefix: string) => { + if (usernamePrefix.length === 0) { + filterAndSetUserResults([]); + } + + const { userInfos } = await legacyCallSearchUsers(usernamePrefix); + filterAndSetUserResults(userInfos); + }, + [filterAndSetUserResults, legacyCallSearchUsers], + ); + + const [threadSearchResults, setThreadSearchResults] = React.useState( + new Set(), + ); + const [usersSearchResults, setUsersSearchResults] = React.useState< + $ReadOnlyArray, + >([]); + const threadSearchIndex = useGlobalThreadSearchIndex(); + React.useEffect(() => { + void (async () => { + const results = threadSearchIndex.getSearchResults(searchText); + setThreadSearchResults(new Set(results)); + if (!usingCommServicesAccessToken) { + await legacySearchUsers(forwardLookupSearchText); + } + })(); + }, [ + searchText, + forwardLookupSearchText, + threadSearchIndex, + legacySearchUsers, + ]); + + const identitySearchUsers = useSearchUsers(forwardLookupSearchText); + React.useEffect(() => { + if (usingCommServicesAccessToken) { + filterAndSetUserResults(identitySearchUsers); + } + }, [filterAndSetUserResults, identitySearchUsers]); + + return { threadSearchResults, usersSearchResults }; +} + +export { useThreadListSearch }; diff --git a/lib/shared/thread-utils.js b/lib/shared/thread-utils.js --- a/lib/shared/thread-utils.js +++ b/lib/shared/thread-utils.js @@ -13,13 +13,10 @@ import { extractUserMentionsFromText } from './mention-utils.js'; import { getMessageTitle, isInvalidSidebarSource } from './message-utils.js'; import { relationshipBlockedInEitherDirection } from './relationship-utils.js'; -import { useForwardLookupSearchText, useSearchUsers } from './search-utils.js'; -import { searchUsers as searchUserCall } from '../actions/user-actions.js'; import ashoat from '../facts/ashoat.js'; import genesis from '../facts/genesis.js'; import { useLoggedInUserInfo } from '../hooks/account-hooks.js'; import { extractKeyserverIDFromID } from '../keyserver-conn/keyserver-call-utils.js'; -import { useLegacyAshoatKeyserverCall } from '../keyserver-conn/legacy-keyserver-call.js'; import { hasPermission, permissionsToBitmaskHex, @@ -36,7 +33,6 @@ ChatThreadItem, ChatMessageInfoItem, } from '../selectors/chat-selectors.js'; -import { useGlobalThreadSearchIndex } from '../selectors/nav-selectors.js'; import { threadInfoSelector, pendingToRealizedThreadIDsSelector, @@ -113,7 +109,6 @@ import type { GetFCNames } from '../utils/farcaster-helpers.js'; import { entries, values } from '../utils/objects.js'; import { useSelector } from '../utils/redux-utils.js'; -import { usingCommServicesAccessToken } from '../utils/services-utils.js'; import { firstLine } from '../utils/string-utils.js'; import { trimText } from '../utils/text-utils.js'; import { pendingThreadIDRegex } from '../utils/validation-utils.js'; @@ -1485,73 +1480,6 @@ return chatItems; } -type ThreadListSearchResult = { - +threadSearchResults: $ReadOnlySet, - +usersSearchResults: $ReadOnlyArray, -}; - -function useThreadListSearch( - searchText: string, - viewerID: ?string, -): ThreadListSearchResult { - const usersWithPersonalThread = useSelector(usersWithPersonalThreadSelector); - const forwardLookupSearchText = useForwardLookupSearchText(searchText); - - const filterAndSetUserResults = React.useCallback( - (userInfos: $ReadOnlyArray) => { - const usersResults = userInfos.filter( - info => !usersWithPersonalThread.has(info.id) && info.id !== viewerID, - ); - setUsersSearchResults(usersResults); - }, - [usersWithPersonalThread, viewerID], - ); - - const legacyCallSearchUsers = useLegacyAshoatKeyserverCall(searchUserCall); - const legacySearchUsers = React.useCallback( - async (usernamePrefix: string) => { - if (usernamePrefix.length === 0) { - filterAndSetUserResults([]); - } - - const { userInfos } = await legacyCallSearchUsers(usernamePrefix); - filterAndSetUserResults(userInfos); - }, - [filterAndSetUserResults, legacyCallSearchUsers], - ); - - const [threadSearchResults, setThreadSearchResults] = React.useState( - new Set(), - ); - const [usersSearchResults, setUsersSearchResults] = React.useState< - $ReadOnlyArray, - >([]); - const threadSearchIndex = useGlobalThreadSearchIndex(); - React.useEffect(() => { - void (async () => { - const results = threadSearchIndex.getSearchResults(searchText); - setThreadSearchResults(new Set(results)); - if (!usingCommServicesAccessToken) { - await legacySearchUsers(forwardLookupSearchText); - } - })(); - }, [ - searchText, - forwardLookupSearchText, - threadSearchIndex, - legacySearchUsers, - ]); - - const identitySearchUsers = useSearchUsers(forwardLookupSearchText); - React.useEffect(() => { - if (usingCommServicesAccessToken) { - filterAndSetUserResults(identitySearchUsers); - } - }, [filterAndSetUserResults, identitySearchUsers]); - - return { threadSearchResults, usersSearchResults }; -} - function useAvailableThreadMemberActions( memberInfo: RelativeMemberInfo, threadInfo: ThreadInfo, @@ -1881,7 +1809,6 @@ getContainingThreadID, getCommunity, getThreadListSearchResults, - useThreadListSearch, useAvailableThreadMemberActions, threadMembersWithoutAddedAdmin, patchThreadInfoToIncludeMentionedMembersOfParent, diff --git a/native/chat/chat-thread-list.react.js b/native/chat/chat-thread-list.react.js --- a/native/chat/chat-thread-list.react.js +++ b/native/chat/chat-thread-list.react.js @@ -22,6 +22,7 @@ import { FloatingAction } from 'react-native-floating-action'; import { useLoggedInUserInfo } from 'lib/hooks/account-hooks.js'; +import { useThreadListSearch } from 'lib/hooks/thread-search-hooks.js'; import { type ChatThreadItem, useFlattenedChatListData, @@ -29,7 +30,6 @@ import { createPendingThread, getThreadListSearchResults, - useThreadListSearch, } from 'lib/shared/thread-utils.js'; import type { ThreadInfo } from 'lib/types/minimally-encoded-thread-permissions-types.js'; import { threadTypes } from 'lib/types/thread-types-enum.js'; diff --git a/web/chat/thread-list-provider.js b/web/chat/thread-list-provider.js --- a/web/chat/thread-list-provider.js +++ b/web/chat/thread-list-provider.js @@ -4,6 +4,7 @@ import * as React from 'react'; import { useLoggedInUserInfo } from 'lib/hooks/account-hooks.js'; +import { useThreadListSearch } from 'lib/hooks/thread-search-hooks.js'; import { type ChatThreadItem, useFlattenedChatListData, @@ -13,7 +14,6 @@ threadInBackgroundChatList, threadInHomeChatList, getThreadListSearchResults, - useThreadListSearch, threadIsPending, useIsThreadInChatList, } from 'lib/shared/thread-utils.js';