Skip to content

Commit 6263c24

Browse files
Merge pull request #45789 from margelo/fix/excesive-suggestion-updates-maimum-update-depth-exceeded
fix: suggestions causing excessive updates
2 parents 7975157 + 0ed014d commit 6263c24

File tree

5 files changed

+29
-20
lines changed

5 files changed

+29
-20
lines changed

src/pages/home/report/ReportActionCompose/ComposerWithSuggestions/ComposerWithSuggestions.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,6 @@ function ComposerWithSuggestions(
812812
policyID={policyID}
813813
// Input
814814
value={value}
815-
setValue={setValue}
816815
selection={selection}
817816
setSelection={setSelection}
818817
resetKeyboardInput={resetKeyboardInput}

src/pages/home/report/ReportActionCompose/SuggestionEmoji.tsx

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ function SuggestionEmoji(
6060
ref: ForwardedRef<SuggestionsRef>,
6161
) {
6262
const [suggestionValues, setSuggestionValues] = useState(defaultSuggestionsValues);
63+
const suggestionValuesRef = useRef(suggestionValues);
64+
suggestionValuesRef.current = suggestionValues;
6365

6466
const isEmojiSuggestionsMenuVisible = suggestionValues.suggestedEmojis.length > 0 && suggestionValues.shouldShowSuggestionMenu;
6567

@@ -149,15 +151,15 @@ function SuggestionEmoji(
149151
* Calculates and cares about the content of an Emoji Suggester
150152
*/
151153
const calculateEmojiSuggestion = useCallback(
152-
(selectionStart?: number, selectionEnd?: number) => {
153-
if (selectionStart !== selectionEnd || !selectionEnd || shouldBlockCalc.current || !value || (selectionStart === 0 && selectionEnd === 0)) {
154+
(newValue: string, selectionStart?: number, selectionEnd?: number) => {
155+
if (selectionStart !== selectionEnd || !selectionEnd || shouldBlockCalc.current || !newValue || (selectionStart === 0 && selectionEnd === 0)) {
154156
shouldBlockCalc.current = false;
155157
resetSuggestions();
156158
return;
157159
}
158-
const leftString = value.substring(0, selectionEnd);
160+
const leftString = newValue.substring(0, selectionEnd);
159161
const colonIndex = leftString.lastIndexOf(':');
160-
const isCurrentlyShowingEmojiSuggestion = isEmojiCode(value, selectionEnd);
162+
const isCurrentlyShowingEmojiSuggestion = isEmojiCode(newValue, selectionEnd);
161163

162164
const nextState: SuggestionsValue = {
163165
suggestedEmojis: [],
@@ -171,19 +173,25 @@ function SuggestionEmoji(
171173
nextState.shouldShowSuggestionMenu = !isEmptyObject(newSuggestedEmojis);
172174
}
173175

176+
// Early return if there is no update
177+
const currentState = suggestionValuesRef.current;
178+
if (nextState.suggestedEmojis.length === 0 && currentState.suggestedEmojis.length === 0) {
179+
return;
180+
}
181+
174182
setSuggestionValues((prevState) => ({...prevState, ...nextState}));
175183
setHighlightedEmojiIndex(0);
176184
},
177-
[value, preferredLocale, setHighlightedEmojiIndex, resetSuggestions],
185+
[preferredLocale, setHighlightedEmojiIndex, resetSuggestions],
178186
);
179187

180188
useEffect(() => {
181189
if (!isComposerFocused) {
182190
return;
183191
}
184192

185-
calculateEmojiSuggestion(selection.start, selection.end);
186-
}, [selection, calculateEmojiSuggestion, isComposerFocused]);
193+
calculateEmojiSuggestion(value, selection.start, selection.end);
194+
}, [value, selection, calculateEmojiSuggestion, isComposerFocused]);
187195

188196
const setShouldBlockSuggestionCalc = useCallback(
189197
(shouldBlockSuggestionCalc: boolean) => {

src/pages/home/report/ReportActionCompose/SuggestionMention.tsx

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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();

src/pages/home/report/ReportActionCompose/Suggestions.tsx

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ type SuggestionProps = {
1414
/** The current input value */
1515
value: string;
1616

17-
/** Callback to update the current input value */
18-
setValue: (newValue: string) => void;
19-
2017
/** The current selection value */
2118
selection: TextSelection;
2219

@@ -56,7 +53,6 @@ type SuggestionProps = {
5653
function Suggestions(
5754
{
5855
value,
59-
setValue,
6056
selection,
6157
setSelection,
6258
updateComment,
@@ -153,7 +149,6 @@ function Suggestions(
153149

154150
const baseProps = {
155151
value,
156-
setValue,
157152
setSelection,
158153
selection,
159154
updateComment,

src/pages/home/report/ReportActionItemMessageEdit.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,6 @@ function ReportActionItemMessageEdit(
534534
measureParentContainerAndReportCursor={measureParentContainerAndReportCursor}
535535
isGroupPolicyReport={false}
536536
value={draft}
537-
setValue={setDraft}
538537
selection={selection}
539538
setSelection={setSelection}
540539
/>

0 commit comments

Comments
 (0)