Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix mention suggestion highlight doesn't move when the text selection moves #39917

Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ function SuggestionEmoji(
<EmojiSuggestions
highlightedEmojiIndex={highlightedEmojiIndex}
emojis={suggestionValues.suggestedEmojis}
prefix={value.slice(suggestionValues.colonIndex + 1, selection.start)}
prefix={value.slice(suggestionValues.colonIndex + 1, selection.end)}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just left a comment in case someone is confused about this change.

I think it makes sense to use selection.end, see below comparison
image

image

onSelect={insertSelectedEmoji}
preferredSkinToneIndex={preferredSkinTone}
isEmojiPickerLarge={!!isAutoSuggestionPickerLarge}
Expand Down
39 changes: 22 additions & 17 deletions src/pages/home/report/ReportActionCompose/SuggestionMention.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Str from 'expensify-common/lib/str';
import lodashSortBy from 'lodash/sortBy';
import type {ForwardedRef} from 'react';
import React, {forwardRef, useCallback, useEffect, useImperativeHandle, useRef, useState} from 'react';
import type {NativeSyntheticEvent, TextInputSelectionChangeEventData} from 'react-native';
import * as Expensicons from '@components/Icon/Expensicons';
import type {Mention} from '@components/MentionSuggestions';
import MentionSuggestions from '@components/MentionSuggestions';
Expand Down Expand Up @@ -219,19 +220,8 @@ function SuggestionMention(
return;
}

const valueAfterTheCursor = value.substring(selectionEnd);
const indexOfFirstSpecialCharOrEmojiAfterTheCursor = valueAfterTheCursor.search(CONST.REGEX.MENTION_BREAKER);

let suggestionEndIndex;
if (indexOfFirstSpecialCharOrEmojiAfterTheCursor === -1) {
// We didn't find a special char/whitespace/emoji after the cursor, so we will use the entire string
suggestionEndIndex = value.length;
} else {
suggestionEndIndex = indexOfFirstSpecialCharOrEmojiAfterTheCursor + selectionEnd;
}

const afterLastBreakLineIndex = value.lastIndexOf('\n', selectionEnd - 1) + 1;
const leftString = value.substring(afterLastBreakLineIndex, suggestionEndIndex);
const leftString = value.substring(afterLastBreakLineIndex, selectionEnd);
const words = leftString.split(CONST.REGEX.SPACE_OR_EMOJI);
const lastWord: string = words.at(-1) ?? '';
const secondToLastWord = words[words.length - 3];
Expand Down Expand Up @@ -261,9 +251,7 @@ function SuggestionMention(
mentionPrefix: prefix,
};

const isCursorBeforeTheMention = valueAfterTheCursor.startsWith(suggestionWord);

if (!isCursorBeforeTheMention && isMentionCode(suggestionWord)) {
if (isMentionCode(suggestionWord)) {
const suggestions = getMentionOptions(personalDetails, prefix);
nextState.suggestedMentions = suggestions;
nextState.shouldShowSuggestionMenu = !!suggestions.length;
Expand All @@ -279,8 +267,12 @@ function SuggestionMention(
);

useEffect(() => {
if (!isComposerFocused) {
return;
}
calculateMentionSuggestion(selection.end);
}, [selection, calculateMentionSuggestion]);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [isComposerFocused]);
bernhardoj marked this conversation as resolved.
Show resolved Hide resolved

const updateShouldShowSuggestionMenuToFalse = useCallback(() => {
setSuggestionValues((prevState) => {
Expand All @@ -300,16 +292,29 @@ function SuggestionMention(

const getSuggestions = useCallback(() => suggestionValues.suggestedMentions, [suggestionValues]);

const onSelectionChange = useCallback(
(e: NativeSyntheticEvent<TextInputSelectionChangeEventData>) => {
/**
* we pass here e.nativeEvent.selection.end directly to calculateEmojiSuggestion
* because in other case calculateEmojiSuggestion will have an old calculation value
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update the comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

* of suggestion instead of current one
*/
calculateMentionSuggestion(e.nativeEvent.selection.end);
},
[calculateMentionSuggestion],
);
bernhardoj marked this conversation as resolved.
Show resolved Hide resolved

useImperativeHandle(
ref,
() => ({
resetSuggestions,
triggerHotkeyActions,
setShouldBlockSuggestionCalc,
onSelectionChange,
updateShouldShowSuggestionMenuToFalse,
getSuggestions,
}),
[resetSuggestions, setShouldBlockSuggestionCalc, triggerHotkeyActions, updateShouldShowSuggestionMenuToFalse, getSuggestions],
[onSelectionChange, resetSuggestions, setShouldBlockSuggestionCalc, triggerHotkeyActions, updateShouldShowSuggestionMenuToFalse, getSuggestions],
);

if (!isMentionSuggestionsMenuVisible) {
Expand Down
3 changes: 2 additions & 1 deletion src/pages/home/report/ReportActionCompose/Suggestions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ function Suggestions(

const onSelectionChange = useCallback((e: NativeSyntheticEvent<TextInputSelectionChangeEventData>) => {
const emojiHandler = suggestionEmojiRef.current?.onSelectionChange?.(e);
return emojiHandler;
const mentionHandler = suggestionMentionRef.current?.onSelectionChange?.(e);
return emojiHandler ?? mentionHandler;
Copy link
Contributor

@eh2077 eh2077 Apr 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to test a corner case with precondition - have a user with Eric :smile: as the first name and empty second name.

By pasting text @eric :sm into composer, I got both two suggestion menus at the same time.
imageimage

As prod has same issue and I'm not sure if this is expected, so I think we can also skip this one in this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I always wondering whether both suggestions will show or not since we don't return anything inside onSelectionChange.

}, []);

const updateShouldShowSuggestionMenuToFalse = useCallback(() => {
Expand Down
Loading