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 @@ -46,6 +46,8 @@ getTypeaheadRegexMatches, type Selection, getUserMentionsCandidates, + type MentionTypeaheadSuggestionItem, + type TypeaheadMatchedStrings, } from 'lib/shared/mention-utils.js'; import { useNextLocalID, @@ -311,6 +313,8 @@ +messageEditingContext: ?MessageEditingContextType, +selectionState: SyncedSelectionData, +setSelectionState: SetState, + +suggestions: $ReadOnlyArray, + +typeaheadMatchedStrings: ?TypeaheadMatchedStrings, }; type State = { +text: string, @@ -671,45 +675,23 @@ ); } - const typeaheadRegexMatches = getTypeaheadRegexMatches( - this.props.selectionState.text, - this.props.selectionState.selection, - nativeMentionTypeaheadRegex, - ); - let typeaheadTooltip = null; - if (typeaheadRegexMatches && !isEditMode) { - const typeaheadMatchedStrings = { - textBeforeAtSymbol: typeaheadRegexMatches[1] ?? '', - query: typeaheadRegexMatches[4] ?? '', - }; - - const suggestedUsers = getMentionTypeaheadUserSuggestions( - this.props.userSearchIndex, - this.props.userMentionsCandidates, - this.props.viewerID, - typeaheadMatchedStrings.query, - ); - const suggestedChats = getMentionTypeaheadChatSuggestions( - this.props.chatMentionSearchIndex, - this.props.chatMentionCandidates, - typeaheadMatchedStrings.query, + if ( + this.props.suggestions.length > 0 && + this.props.typeaheadMatchedStrings && + !isEditMode + ) { + typeaheadTooltip = ( + ); - const suggestions = [...suggestedUsers, ...suggestedChats]; - - if (suggestions.length > 0) { - typeaheadTooltip = ( - - ); - } } let content; @@ -1311,6 +1293,61 @@ selection: { start: 0, end: 0 }, }); + const typeaheadRegexMatches = React.useMemo( + () => + getTypeaheadRegexMatches( + selectionState.text, + selectionState.selection, + nativeMentionTypeaheadRegex, + ), + [selectionState.text, selectionState.selection], + ); + + const typeaheadResults: { + typeaheadMatchedStrings: ?TypeaheadMatchedStrings, + suggestions: $ReadOnlyArray, + } = React.useMemo(() => { + if (!typeaheadRegexMatches) { + return { + typeaheadMatchedStrings: null, + suggestions: [], + }; + } + + const typeaheadMatchedStrings: TypeaheadMatchedStrings = { + textBeforeAtSymbol: typeaheadRegexMatches[1] ?? '', + query: typeaheadRegexMatches[4] ?? '', + }; + + const suggestedUsers = getMentionTypeaheadUserSuggestions( + userSearchIndex, + userMentionsCandidates, + viewerID, + typeaheadMatchedStrings.query, + ); + const suggestedChats = getMentionTypeaheadChatSuggestions( + chatMentionSearchIndex, + chatMentionCandidates, + typeaheadMatchedStrings.query, + ); + const suggestions: $ReadOnlyArray = [ + ...suggestedUsers, + ...suggestedChats, + ]; + + return { + typeaheadMatchedStrings, + suggestions, + }; + }, [ + chatMentionCandidates, + chatMentionSearchIndex, + typeaheadRegexMatches, + userMentionsCandidates, + userSearchIndex, + viewerID, + ]); + return ( ); }