Skip to content

Commit

Permalink
Merge pull request #42208 from Expensify/jasper-removeUpdateFrequentl…
Browse files Browse the repository at this point in the history
…yUsedEmojis

Remove `UpdateFrequentlyUsedEmojis` and support new frequentlyUsedEmojis NVP format
  • Loading branch information
marcochavezf authored Jul 10, 2024
2 parents 7bed113 + 4ae11d5 commit 4f4dd9a
Show file tree
Hide file tree
Showing 9 changed files with 3 additions and 440 deletions.
2 changes: 0 additions & 2 deletions src/CONST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1123,8 +1123,6 @@ const CONST = {
// around each header.
EMOJI_NUM_PER_ROW: 8,

EMOJI_FREQUENT_ROW_COUNT: 3,

EMOJI_DEFAULT_SKIN_TONE: -1,

// Amount of emojis to render ahead at the end of the update cycle
Expand Down
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 @@ -64,7 +64,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 @@ -57,7 +57,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 @@ -351,7 +350,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;

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 @@ -304,22 +303,12 @@ function ComposerWithSuggestions(
const [composerHeight, setComposerHeight] = useState(0);

const textInputRef = useRef<TextInput | null>(null);
const insertedEmojisRef = useRef<Emoji[]>([]);

const syncSelectionWithOnChangeTextRef = useRef<SyncSelection | null>(null);

// 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 @@ -421,8 +410,6 @@ function ComposerWithSuggestions(
if (suggestionsRef.current) {
suggestionsRef.current.resetSuggestions();
}
insertedEmojisRef.current = [...insertedEmojisRef.current, ...newEmojis];
debouncedUpdateFrequentlyUsedEmojis();
}
}
const newCommentConverted = convertToLTRForComposer(newComment);
Expand Down Expand Up @@ -461,18 +448,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
24 changes: 1 addition & 23 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 @@ -105,7 +104,6 @@ function ReportActionItemMessageEdit(

const textInputRef = useRef<(HTMLTextAreaElement & TextInput) | null>(null);
const isFocusedRef = useRef<boolean>(false);
const insertedEmojis = useRef<Emoji[]>([]);
const draftRef = useRef(draft);
const emojiPickerSelectionRef = useRef<Selection | undefined>(undefined);
// The ref to check whether the comment saving is in progress
Expand Down Expand Up @@ -214,19 +212,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 @@ -236,13 +221,6 @@ function ReportActionItemMessageEdit(
(newDraftInput: string) => {
const {text: newDraft, emojis, cursorPosition} = EmojiUtils.replaceAndExtractEmojis(newDraftInput, preferredSkinTone, preferredLocale);

if (emojis?.length > 0) {
const newEmojis = EmojiUtils.getAddedEmojis(emojis, emojisPresentBefore.current);
if (newEmojis?.length > 0) {
insertedEmojis.current = [...insertedEmojis.current, ...newEmojis];
debouncedUpdateFrequentlyUsedEmojis();
}
}
emojisPresentBefore.current = emojis;

setDraft(newDraft);
Expand All @@ -261,7 +239,7 @@ function ReportActionItemMessageEdit(
debouncedSaveDraft(newDraft);
isCommentPendingSaved.current = true;
},
[debouncedSaveDraft, debouncedUpdateFrequentlyUsedEmojis, preferredSkinTone, preferredLocale, selection.end],
[debouncedSaveDraft, preferredSkinTone, preferredLocale, selection.end],
);

useEffect(() => {
Expand Down
Loading

0 comments on commit 4f4dd9a

Please sign in to comment.