Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions src/CONST/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1632,6 +1632,7 @@ const CONST = {
TRIE_INITIALIZATION: 'trie_initialization',
COMMENT_LENGTH_DEBOUNCE_TIME: 1500,
SEARCH_OPTION_LIST_DEBOUNCE_TIME: 300,
MENTION_SUGGESTION_DEBOUNCE_TIME: 100,
RESIZE_DEBOUNCE_TIME: 100,
UNREAD_UPDATE_DEBOUNCE_TIME: 300,
SEARCH_FILTER_OPTIONS: 'search_filter_options',
Expand Down
24 changes: 18 additions & 6 deletions src/pages/home/report/ReportActionCompose/SuggestionMention.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import lodashMapValues from 'lodash/mapValues';
import lodashSortBy from 'lodash/sortBy';
import React, {useCallback, useEffect, useImperativeHandle, useMemo, useRef, useState} from 'react';
import type {OnyxCollection} from 'react-native-onyx';
import * as Expensicons from '@components/Icon/Expensicons';
import type {Mention} from '@components/MentionSuggestions';
import MentionSuggestions from '@components/MentionSuggestions';
import {usePersonalDetails} from '@components/OnyxListItemProvider';
import useArrowKeyFocusManager from '@hooks/useArrowKeyFocusManager';
import useCurrentReportID from '@hooks/useCurrentReportID';
import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails';
import useDebounce from '@hooks/useDebounce';
import {useMemoizedLazyExpensifyIcons} from '@hooks/useLazyAsset';
import useLocalize from '@hooks/useLocalize';
import useOnyx from '@hooks/useOnyx';
import usePolicy from '@hooks/usePolicy';
Expand Down Expand Up @@ -81,6 +81,8 @@ function SuggestionMention({
const currentUserPersonalDetails = useCurrentUserPersonalDetails();
const isMentionSuggestionsMenuVisible = !!suggestionValues.suggestedMentions.length && suggestionValues.shouldShowSuggestionMenu;

const expensifyIcons = useMemoizedLazyExpensifyIcons(['Megaphone', 'FallbackAvatar'] as const);

const currentReportID = useCurrentReportID();
const currentReport = reports?.[`${ONYXKEYS.COLLECTION.REPORT}${currentReportID?.currentReportID}`];
// Smaller weight means higher order in suggestion list
Expand Down Expand Up @@ -270,7 +272,7 @@ function SuggestionMention({
alternateText: translate('mentionSuggestions.hereAlternateText'),
icons: [
{
source: Expensicons.Megaphone,
source: expensifyIcons.Megaphone,
type: CONST.ICON_TYPE_AVATAR,
},
],
Expand Down Expand Up @@ -321,7 +323,7 @@ function SuggestionMention({
icons: [
{
name: detail?.login,
source: detail?.avatar ?? Expensicons.FallbackAvatar,
source: detail?.avatar ?? expensifyIcons.FallbackAvatar,
type: CONST.ICON_TYPE_AVATAR,
fallbackIcon: detail?.fallbackIcon,
id: detail?.accountID,
Expand All @@ -332,7 +334,7 @@ function SuggestionMention({

return suggestions;
},
[translate, formatPhoneNumber, formatLoginPrivateDomain, localeCompare],
[localeCompare, translate, expensifyIcons.Megaphone, expensifyIcons.FallbackAvatar, formatPhoneNumber, formatLoginPrivateDomain],
);

const getRoomMentionOptions = useCallback(
Expand Down Expand Up @@ -429,9 +431,19 @@ function SuggestionMention({
[isComposerFocused, isGroupPolicyReport, setHighlightedMentionIndex, resetSuggestions, getUserMentionOptions, weightedPersonalDetails, getRoomMentionOptions, reports],
);

const debouncedCalculateMentionSuggestion = useDebounce(
useCallback(
(newValue: string, selectionStart?: number, selectionEnd?: number) => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

❌ PERF-6

Passing the entire selection object as a dependency causes the effect to re-execute whenever any property of the selection object changes, even if only start and end are used. This creates unnecessary executions and less predictable behavior.

Suggested fix:

useEffect(() => {
    debouncedCalculateMentionSuggestion(value, selection.start, selection.end);
}, [value, selection.start, selection.end, debouncedCalculateMentionSuggestion]);

calculateMentionSuggestion(newValue, selectionStart, selectionEnd);
},
[calculateMentionSuggestion],
),
CONST.TIMING.MENTION_SUGGESTION_DEBOUNCE_TIME,
);

useEffect(() => {
calculateMentionSuggestion(value, selection.start, selection.end);
}, [value, selection, calculateMentionSuggestion]);
debouncedCalculateMentionSuggestion(value, selection.start, selection.end);
}, [value, selection.start, selection.end, debouncedCalculateMentionSuggestion]);

useEffect(() => {
debouncedSearchInServer();
Expand Down