@@ -88,6 +88,8 @@ function SuggestionMention(
8888 const personalDetails = usePersonalDetails ( ) ?? CONST . EMPTY_OBJECT ;
8989 const { translate, formatPhoneNumber} = useLocalize ( ) ;
9090 const [ suggestionValues , setSuggestionValues ] = useState ( defaultSuggestionsValues ) ;
91+ const suggestionValuesRef = useRef ( suggestionValues ) ;
92+ suggestionValuesRef . current = suggestionValues ;
9193
9294 const [ reports ] = useOnyx ( ONYXKEYS . COLLECTION . REPORT ) ;
9395
@@ -344,15 +346,15 @@ function SuggestionMention(
344346 ) ;
345347
346348 const calculateMentionSuggestion = useCallback (
347- ( selectionStart ?: number , selectionEnd ?: number ) => {
349+ ( newValue : string , selectionStart ?: number , selectionEnd ?: number ) => {
348350 if ( selectionEnd !== selectionStart || ! selectionEnd || shouldBlockCalc . current || selectionEnd < 1 || ! isComposerFocused ) {
349351 shouldBlockCalc . current = false ;
350352 resetSuggestions ( ) ;
351353 return ;
352354 }
353355
354- const afterLastBreakLineIndex = value . lastIndexOf ( '\n' , selectionEnd - 1 ) + 1 ;
355- const leftString = value . substring ( afterLastBreakLineIndex , selectionEnd ) ;
356+ const afterLastBreakLineIndex = newValue . lastIndexOf ( '\n' , selectionEnd - 1 ) + 1 ;
357+ const leftString = newValue . substring ( afterLastBreakLineIndex , selectionEnd ) ;
356358 const words = leftString . split ( CONST . REGEX . SPACE_OR_EMOJI ) ;
357359 const lastWord : string = words . at ( - 1 ) ?? '' ;
358360 const secondToLastWord = words [ words . length - 3 ] ;
@@ -401,18 +403,24 @@ function SuggestionMention(
401403 nextState . shouldShowSuggestionMenu = true ;
402404 }
403405
406+ // Early return if there is no update
407+ const currentState = suggestionValuesRef . current ;
408+ if ( currentState . suggestedMentions . length === 0 && nextState . suggestedMentions ?. length === 0 ) {
409+ return ;
410+ }
411+
404412 setSuggestionValues ( ( prevState ) => ( {
405413 ...prevState ,
406414 ...nextState ,
407415 } ) ) ;
408416 setHighlightedMentionIndex ( 0 ) ;
409417 } ,
410- [ isComposerFocused , value , isGroupPolicyReport , setHighlightedMentionIndex , resetSuggestions , getUserMentionOptions , weightedPersonalDetails , getRoomMentionOptions , reports ] ,
418+ [ isComposerFocused , isGroupPolicyReport , setHighlightedMentionIndex , resetSuggestions , getUserMentionOptions , weightedPersonalDetails , getRoomMentionOptions , reports ] ,
411419 ) ;
412420
413421 useEffect ( ( ) => {
414- calculateMentionSuggestion ( selection . start , selection . end ) ;
415- } , [ selection , calculateMentionSuggestion ] ) ;
422+ calculateMentionSuggestion ( value , selection . start , selection . end ) ;
423+ } , [ value , selection , calculateMentionSuggestion ] ) ;
416424
417425 useEffect ( ( ) => {
418426 debouncedSearchInServer ( ) ;
0 commit comments