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

Remove UpdateFrequentlyUsedEmojis and support new frequentlyUsedEmojis NVP format #42208

Merged
merged 21 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
3 changes: 0 additions & 3 deletions src/libs/API/parameters/UpdateFrequentlyUsedEmojisParams.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/libs/API/parameters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ export type {default as UpdateAutomaticTimezoneParams} from './UpdateAutomaticTi
export type {default as UpdateChatPriorityModeParams} from './UpdateChatPriorityModeParams';
export type {default as UpdateDateOfBirthParams} from './UpdateDateOfBirthParams';
export type {default as UpdateDisplayNameParams} from './UpdateDisplayNameParams';
export type {default as UpdateFrequentlyUsedEmojisParams} from './UpdateFrequentlyUsedEmojisParams';
export type {default as UpdateGroupChatNameParams} from './UpdateGroupChatNameParams';
export type {default as UpdateGroupChatMemberRolesParams} from './UpdateGroupChatMemberRolesParams';
export type {default as UpdateHomeAddressParams} from './UpdateHomeAddressParams';
Expand Down
2 changes: 0 additions & 2 deletions src/libs/API/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ const WRITE_COMMANDS = {
VALIDATE_LOGIN: 'ValidateLogin',
VALIDATE_SECONDARY_LOGIN: 'ValidateSecondaryLogin',
UPDATE_PREFERRED_EMOJI_SKIN_TONE: 'UpdatePreferredEmojiSkinTone',
UPDATE_FREQUENTLY_USED_EMOJIS: 'UpdateFrequentlyUsedEmojis',
UPDATE_CHAT_PRIORITY_MODE: 'UpdateChatPriorityMode',
SET_CONTACT_METHOD_AS_DEFAULT: 'SetContactMethodAsDefault',
UPDATE_THEME: 'UpdateTheme',
Expand Down Expand Up @@ -299,7 +298,6 @@ type WriteCommandParameters = {
[WRITE_COMMANDS.VALIDATE_LOGIN]: Parameters.ValidateLoginParams;
[WRITE_COMMANDS.VALIDATE_SECONDARY_LOGIN]: Parameters.ValidateSecondaryLoginParams;
[WRITE_COMMANDS.UPDATE_PREFERRED_EMOJI_SKIN_TONE]: Parameters.UpdatePreferredEmojiSkinToneParams;
[WRITE_COMMANDS.UPDATE_FREQUENTLY_USED_EMOJIS]: Parameters.UpdateFrequentlyUsedEmojisParams;
[WRITE_COMMANDS.UPDATE_CHAT_PRIORITY_MODE]: Parameters.UpdateChatPriorityModeParams;
[WRITE_COMMANDS.SET_CONTACT_METHOD_AS_DEFAULT]: Parameters.SetContactMethodAsDefaultParams;
[WRITE_COMMANDS.UPDATE_THEME]: Parameters.UpdateThemeParams;
Expand Down
33 changes: 0 additions & 33 deletions src/libs/EmojiUtils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {getUnixTime} from 'date-fns';
import {Str} from 'expensify-common';
import memoize from 'lodash/memoize';
import Onyx from 'react-native-onyx';
Expand Down Expand Up @@ -235,37 +234,6 @@ function mergeEmojisWithFrequentlyUsedEmojis(emojis: PickerEmojis): EmojiPickerL
return addSpacesToEmojiCategories(mergedEmojis);
}

/**
* Get the updated frequently used emojis list by usage
*/
function getFrequentlyUsedEmojis(newEmoji: Emoji | Emoji[]): FrequentlyUsedEmoji[] {
let frequentEmojiList = [...frequentlyUsedEmojis];

const maxFrequentEmojiCount = CONST.EMOJI_FREQUENT_ROW_COUNT * CONST.EMOJI_NUM_PER_ROW - 1;

jasperhuangg marked this conversation as resolved.
Show resolved Hide resolved
const currentTimestamp = getUnixTime(new Date());
(Array.isArray(newEmoji) ? [...newEmoji] : [newEmoji]).forEach((emoji) => {
let currentEmojiCount = 1;
const emojiIndex = frequentEmojiList.findIndex((e) => e.code === emoji.code);
if (emojiIndex >= 0) {
currentEmojiCount = frequentEmojiList[emojiIndex].count + 1;
frequentEmojiList.splice(emojiIndex, 1);
}

const updatedEmoji = {...Emojis.emojiCodeTableWithSkinTones[emoji.code], count: currentEmojiCount, lastUpdatedAt: currentTimestamp};

// We want to make sure the current emoji is added to the list
// Hence, we take one less than the current frequent used emojis
frequentEmojiList = frequentEmojiList.slice(0, maxFrequentEmojiCount);
frequentEmojiList.push(updatedEmoji);

// Sort the list by count and lastUpdatedAt in descending order
frequentEmojiList.sort((a, b) => b.count - a.count || b.lastUpdatedAt - a.lastUpdatedAt);
});

return frequentEmojiList;
}

/**
* Given an emoji item object, return an emoji code based on its type.
*/
Expand Down Expand Up @@ -601,7 +569,6 @@ export {
getLocalizedEmojiName,
getHeaderEmojis,
mergeEmojisWithFrequentlyUsedEmojis,
getFrequentlyUsedEmojis,
containsOnlyEmojis,
replaceEmojis,
suggestEmojis,
Expand Down
21 changes: 1 addition & 20 deletions src/libs/actions/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import type {
SetContactMethodAsDefaultParams,
SetNameValuePairParams,
UpdateChatPriorityModeParams,
UpdateFrequentlyUsedEmojisParams,
UpdateNewsletterSubscriptionParams,
UpdatePreferredEmojiSkinToneParams,
UpdateStatusParams,
Expand All @@ -37,7 +36,7 @@ import Visibility from '@libs/Visibility';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type {BlockedFromConcierge, CustomStatusDraft, FrequentlyUsedEmoji, Policy} from '@src/types/onyx';
import type {BlockedFromConcierge, CustomStatusDraft, Policy} from '@src/types/onyx';
import type Login from '@src/types/onyx/Login';
import type {OnyxServerUpdate} from '@src/types/onyx/OnyxUpdatesFromServer';
import type OnyxPersonalDetails from '@src/types/onyx/PersonalDetails';
Expand Down Expand Up @@ -655,23 +654,6 @@ function updatePreferredSkinTone(skinTone: number) {
API.write(WRITE_COMMANDS.UPDATE_PREFERRED_EMOJI_SKIN_TONE, parameters, {optimisticData});
}

/**
* Sync frequentlyUsedEmojis with Onyx and Server
*/
function updateFrequentlyUsedEmojis(frequentlyUsedEmojis: FrequentlyUsedEmoji[]) {
const optimisticData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.SET,
key: ONYXKEYS.FREQUENTLY_USED_EMOJIS,
value: frequentlyUsedEmojis,
},
];

const parameters: UpdateFrequentlyUsedEmojisParams = {value: JSON.stringify(frequentlyUsedEmojis)};

API.write(WRITE_COMMANDS.UPDATE_FREQUENTLY_USED_EMOJIS, parameters, {optimisticData});
}

/**
* Sync user chat priority mode with Onyx and Server
* @param mode
Expand Down Expand Up @@ -1045,7 +1027,6 @@ export {
setShouldUseStagingServer,
setMuteAllSounds,
clearUserErrorMessage,
updateFrequentlyUsedEmojis,
joinScreenShare,
clearScreenShareRequest,
generateStatementPDF,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ import * as EmojiPickerActions from '@userActions/EmojiPickerAction';
import * as InputFocus from '@userActions/InputFocus';
import * as Modal from '@userActions/Modal';
import * as Report from '@userActions/Report';
import * as User from '@userActions/User';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type * as OnyxTypes from '@src/types/onyx';
Expand Down Expand Up @@ -311,15 +310,6 @@ function ComposerWithSuggestions(
// The ref to check whether the comment saving is in progress
const isCommentPendingSaved = useRef(false);

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

/**
* Set the TextInput Ref
*/
Expand Down Expand Up @@ -422,7 +412,6 @@ function ComposerWithSuggestions(
suggestionsRef.current.resetSuggestions();
}
insertedEmojisRef.current = [...insertedEmojisRef.current, ...newEmojis];
jasperhuangg marked this conversation as resolved.
Show resolved Hide resolved
debouncedUpdateFrequentlyUsedEmojis();
}
}
const newCommentConverted = convertToLTRForComposer(newComment);
Expand Down Expand Up @@ -461,18 +450,7 @@ function ComposerWithSuggestions(
debouncedBroadcastUserIsTyping(reportID);
}
},
[
debouncedUpdateFrequentlyUsedEmojis,
findNewlyAddedChars,
preferredLocale,
preferredSkinTone,
reportID,
setIsCommentEmpty,
suggestionsRef,
raiseIsScrollLikelyLayoutTriggered,
debouncedSaveReportComment,
selection.end,
],
[findNewlyAddedChars, preferredLocale, preferredSkinTone, reportID, setIsCommentEmpty, suggestionsRef, raiseIsScrollLikelyLayoutTriggered, debouncedSaveReportComment, selection.end],
);

const prepareCommentAndResetComposer = useCallback((): string => {
Expand Down
17 changes: 1 addition & 16 deletions src/pages/home/report/ReportActionItemMessageEdit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import * as ComposerActions from '@userActions/Composer';
import * as EmojiPickerAction from '@userActions/EmojiPickerAction';
import * as InputFocus from '@userActions/InputFocus';
import * as Report from '@userActions/Report';
import * as User from '@userActions/User';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type * as OnyxTypes from '@src/types/onyx';
Expand Down Expand Up @@ -214,19 +213,6 @@ function ReportActionItemMessageEdit(
[debouncedSaveDraft],
);

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

/**
* Update the value of the draft in Onyx
*
Expand All @@ -240,7 +226,6 @@ function ReportActionItemMessageEdit(
const newEmojis = EmojiUtils.getAddedEmojis(emojis, emojisPresentBefore.current);
if (newEmojis?.length > 0) {
insertedEmojis.current = [...insertedEmojis.current, ...newEmojis];
debouncedUpdateFrequentlyUsedEmojis();
jasperhuangg marked this conversation as resolved.
Show resolved Hide resolved
}
}
emojisPresentBefore.current = emojis;
Expand All @@ -261,7 +246,7 @@ function ReportActionItemMessageEdit(
debouncedSaveDraft(newDraft);
isCommentPendingSaved.current = true;
},
[debouncedSaveDraft, debouncedUpdateFrequentlyUsedEmojis, preferredSkinTone, preferredLocale, selection.end],
[debouncedSaveDraft, preferredSkinTone, preferredLocale, selection.end],
);

useEffect(() => {
Expand Down
Loading
Loading