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: UpdateFrequentlyUsedEmojis API spamming #21772

Merged
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
18 changes: 15 additions & 3 deletions src/pages/home/report/ReportActionCompose.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,9 @@ class ReportActionCompose extends React.Component {
this.setExceededMaxCommentLength = this.setExceededMaxCommentLength.bind(this);
this.updateNumberOfLines = this.updateNumberOfLines.bind(this);
this.showPopoverMenu = this.showPopoverMenu.bind(this);
this.debouncedUpdateFrequentlyUsedEmojis = _.debounce(this.debouncedUpdateFrequentlyUsedEmojis.bind(this), 1000, false);
this.comment = props.comment;
this.insertedEmojis = [];

// React Native will retain focus on an input for native devices but web/mWeb behave differently so we have some focus management
// code that will refocus the compose input after a user closes a modal or some other actions, see usage of ReportActionComposeFocusManager
Expand Down Expand Up @@ -633,8 +635,8 @@ class ReportActionCompose extends React.Component {
},
suggestedEmojis: [],
}));
const frequentEmojiList = EmojiUtils.getFrequentlyUsedEmojis(emojiObject);
User.updateFrequentlyUsedEmojis(frequentEmojiList);
this.insertedEmojis = [...this.insertedEmojis, emojiObject];
this.debouncedUpdateFrequentlyUsedEmojis(emojiObject);
}

/**
Expand Down Expand Up @@ -719,6 +721,15 @@ class ReportActionCompose extends React.Component {
Report.broadcastUserIsTyping(this.props.reportID);
}

/**
* Update frequently used emojis list. We debounce this method in the constructor so that UpdateFrequentlyUsedEmojis
* API is not called too often.
*/
debouncedUpdateFrequentlyUsedEmojis() {
User.updateFrequentlyUsedEmojis(EmojiUtils.getFrequentlyUsedEmojis(this.insertedEmojis));
this.insertedEmojis = [];
}

/**
* Update the value of the comment in Onyx
*
Expand All @@ -729,7 +740,8 @@ class ReportActionCompose extends React.Component {
const {text: newComment = '', emojis = []} = EmojiUtils.replaceEmojis(comment, this.props.preferredSkinTone);

if (!_.isEmpty(emojis)) {
User.updateFrequentlyUsedEmojis(EmojiUtils.getFrequentlyUsedEmojis(emojis));
this.insertedEmojis = [...this.insertedEmojis, ...emojis];
this.debouncedUpdateFrequentlyUsedEmojis();
}

this.setState((prevState) => {
Expand Down
19 changes: 17 additions & 2 deletions src/pages/home/report/ReportActionItemMessageEdit.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ function ReportActionItemMessageEdit(props) {

const textInputRef = useRef(null);
const isFocusedRef = useRef(false);
const insertedEmojis = useRef([]);

useEffect(() => {
// required for keeping last state of isFocused variable
Expand Down Expand Up @@ -138,6 +139,19 @@ function ReportActionItemMessageEdit(props) {
[props.reportID, props.action.reportActionID],
);

/**
* Update frequently used emojis list. We debounce this method in the constructor so that UpdateFrequentlyUsedEmojis
* API is not called too often.
*/
const debouncedUpdateFrequentlyUsedEmojis = useMemo(
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's add a comment in here about why this is necessary

() =>
_.debounce(() => {
User.updateFrequentlyUsedEmojis(EmojiUtils.getFrequentlyUsedEmojis(insertedEmojis.current));
insertedEmojis.current = [];
}, 1000),
[],
);

/**
* Update the value of the draft in Onyx
*
Expand All @@ -148,7 +162,8 @@ function ReportActionItemMessageEdit(props) {
const {text: newDraft = '', emojis = []} = EmojiUtils.replaceEmojis(newDraftInput, props.preferredSkinTone);

if (!_.isEmpty(emojis)) {
User.updateFrequentlyUsedEmojis(EmojiUtils.getFrequentlyUsedEmojis(emojis));
insertedEmojis.current = [...insertedEmojis.current, ...emojis];
debouncedUpdateFrequentlyUsedEmojis();
}
setDraft((prevDraft) => {
if (newDraftInput !== newDraft) {
Expand All @@ -172,7 +187,7 @@ function ReportActionItemMessageEdit(props) {
debouncedSaveDraft(props.action.message[0].html);
}
},
[props.action.message, debouncedSaveDraft, props.preferredSkinTone],
[props.action.message, debouncedSaveDraft, debouncedUpdateFrequentlyUsedEmojis, props.preferredSkinTone],
);

/**
Expand Down