diff --git a/lib/shared/mention-utils.js b/lib/shared/mention-utils.js --- a/lib/shared/mention-utils.js +++ b/lib/shared/mention-utils.js @@ -134,23 +134,32 @@ }, [userSearchIndex, resolvedThredMembers, usernamePrefix, viewerID]); } -function getMentionTypeaheadChatSuggestions( +function useMentionTypeaheadChatSuggestions( chatSearchIndex: SentencePrefixSearchIndex, chatMentionCandidates: ChatMentionCandidates, - chatNamePrefix: string, + typeaheadMatchedStrings: ?TypeaheadMatchedStrings, ): $ReadOnlyArray { - const result = []; - const threadIDs = chatSearchIndex.getSearchResults(chatNamePrefix); - for (const threadID of threadIDs) { - if (!chatMentionCandidates[threadID]) { - continue; + const chatNamePrefix: ?string = typeaheadMatchedStrings?.query; + + return React.useMemo(() => { + const result = []; + + if (chatNamePrefix === undefined || chatNamePrefix === null) { + return result; } - result.push({ - type: 'chat', - threadInfo: chatMentionCandidates[threadID], - }); - } - return result; + + const threadIDs = chatSearchIndex.getSearchResults(chatNamePrefix); + for (const threadID of threadIDs) { + if (!chatMentionCandidates[threadID]) { + continue; + } + result.push({ + type: 'chat', + threadInfo: chatMentionCandidates[threadID], + }); + } + return result; + }, [chatSearchIndex, chatMentionCandidates, chatNamePrefix]); } function getNewTextAndSelection( @@ -197,7 +206,7 @@ isUserMentioned, extractUserMentionsFromText, useMentionTypeaheadUserSuggestions, - getMentionTypeaheadChatSuggestions, + useMentionTypeaheadChatSuggestions, getNewTextAndSelection, getTypeaheadRegexMatches, useUserMentionsCandidates, diff --git a/native/chat/chat-input-bar.react.js b/native/chat/chat-input-bar.react.js --- a/native/chat/chat-input-bar.react.js +++ b/native/chat/chat-input-bar.react.js @@ -41,7 +41,7 @@ import { useEditMessage } from 'lib/shared/edit-messages-utils.js'; import { useMentionTypeaheadUserSuggestions, - getMentionTypeaheadChatSuggestions, + useMentionTypeaheadChatSuggestions, getTypeaheadRegexMatches, type Selection, useUserMentionsCandidates, @@ -1316,26 +1316,17 @@ typeaheadMatchedStrings, ); - const suggestions: $ReadOnlyArray = - React.useMemo(() => { - if (!typeaheadRegexMatches || !typeaheadMatchedStrings) { - return []; - } - - const suggestedChats = getMentionTypeaheadChatSuggestions( - chatMentionSearchIndex, - chatMentionCandidates, - typeaheadMatchedStrings.query, - ); + const suggestedChats = useMentionTypeaheadChatSuggestions( + chatMentionSearchIndex, + chatMentionCandidates, + typeaheadMatchedStrings, + ); - return [...suggestedUsers, ...suggestedChats]; - }, [ - chatMentionCandidates, - chatMentionSearchIndex, - typeaheadRegexMatches, - typeaheadMatchedStrings, - suggestedUsers, - ]); + const suggestions: $ReadOnlyArray = + React.useMemo( + () => [...suggestedUsers, ...suggestedChats], + [suggestedUsers, suggestedChats], + ); return ( { - if (!typeaheadMatchedStrings) { - return ([]: $ReadOnlyArray); - } - const suggestedChats = getMentionTypeaheadChatSuggestions( - chatMentionSearchIndex, - props.inputState.typeaheadState.frozenChatMentionsCandidates, - typeaheadMatchedStrings.query, - ); - return ([ - ...suggestedUsers, - ...suggestedChats, - ]: $ReadOnlyArray); - }, [ - suggestedUsers, - typeaheadMatchedStrings, - props.inputState.typeaheadState.frozenChatMentionsCandidates, + const suggestedChats = useMentionTypeaheadChatSuggestions( chatMentionSearchIndex, - ]); + props.inputState.typeaheadState.frozenChatMentionsCandidates, + typeaheadMatchedStrings, + ); + + const suggestions: $ReadOnlyArray = + React.useMemo( + () => [...suggestedUsers, ...suggestedChats], + [suggestedUsers, suggestedChats], + ); return (