From 864aeee11a510e946ac3caa5d20b2d1c8e97e87f Mon Sep 17 00:00:00 2001 From: Ginsu Eddy Date: Sun, 2 Jul 2023 22:10:50 -0700 Subject: [PATCH] [native] switch getViewerAlreadySelectedMessageReactions to useGetViewerAlreadySelectedMessageReactions Summary: small follow up diff to address comment in D8288 Test Plan: confirmed that the already selected emojis still behaved as expected in the emoji keyboard Reviewers: atul, kamil Reviewed By: kamil Subscribers: ashoat, tomek Differential Revision: https://phab.comm.dev/D8410 --- lib/shared/reaction-utils.js | 20 ++++++++++--------- ...multimedia-message-tooltip-button.react.js | 8 +++----- .../robotext-message-tooltip-button.react.js | 8 +++----- .../chat/text-message-tooltip-button.react.js | 8 +++----- 4 files changed, 20 insertions(+), 24 deletions(-) diff --git a/lib/shared/reaction-utils.js b/lib/shared/reaction-utils.js index df97cc007b..5f8e64c963 100644 --- a/lib/shared/reaction-utils.js +++ b/lib/shared/reaction-utils.js @@ -33,20 +33,22 @@ function stringForReactionList(reactions: ReactionInfo): string { return reactionText.join(' '); } -function getViewerAlreadySelectedMessageReactions( +function useViewerAlreadySelectedMessageReactions( reactions: ReactionInfo, ): $ReadOnlyArray { - const alreadySelectedEmojis = []; + return React.useMemo(() => { + const alreadySelectedEmojis = []; - for (const reaction in reactions) { - const reactionInfo = reactions[reaction]; + for (const reaction in reactions) { + const reactionInfo = reactions[reaction]; - if (reactionInfo.viewerReacted) { - alreadySelectedEmojis.push(reaction); + if (reactionInfo.viewerReacted) { + alreadySelectedEmojis.push(reaction); + } } - } - return alreadySelectedEmojis; + return alreadySelectedEmojis; + }, [reactions]); } type MessageReactionListInfo = { @@ -119,7 +121,7 @@ function useCanCreateReactionFromMessage( export { stringForReactionList, - getViewerAlreadySelectedMessageReactions, + useViewerAlreadySelectedMessageReactions, useMessageReactionsList, useCanCreateReactionFromMessage, }; diff --git a/native/chat/multimedia-message-tooltip-button.react.js b/native/chat/multimedia-message-tooltip-button.react.js index 10d3461ebf..b303feba27 100644 --- a/native/chat/multimedia-message-tooltip-button.react.js +++ b/native/chat/multimedia-message-tooltip-button.react.js @@ -5,7 +5,7 @@ import Animated from 'react-native-reanimated'; import { localIDPrefix } from 'lib/shared/message-utils.js'; import { - getViewerAlreadySelectedMessageReactions, + useViewerAlreadySelectedMessageReactions, useCanCreateReactionFromMessage, } from 'lib/shared/reaction-utils.js'; @@ -155,10 +155,8 @@ function MultimediaMessageTooltipButton(props: Props): React.Node { [sendReaction, dismissTooltip], ); - const alreadySelectedEmojis = React.useMemo( - () => getViewerAlreadySelectedMessageReactions(item.reactions), - [item.reactions], - ); + const alreadySelectedEmojis = + useViewerAlreadySelectedMessageReactions(reactions); return ( <> diff --git a/native/chat/robotext-message-tooltip-button.react.js b/native/chat/robotext-message-tooltip-button.react.js index 00d409c0af..3e63c1aac4 100644 --- a/native/chat/robotext-message-tooltip-button.react.js +++ b/native/chat/robotext-message-tooltip-button.react.js @@ -5,7 +5,7 @@ import Animated from 'react-native-reanimated'; import { localIDPrefix } from 'lib/shared/message-utils.js'; import { - getViewerAlreadySelectedMessageReactions, + useViewerAlreadySelectedMessageReactions, useCanCreateReactionFromMessage, } from 'lib/shared/reaction-utils.js'; @@ -138,10 +138,8 @@ function RobotextMessageTooltipButton(props: Props): React.Node { [sendReaction, dismissTooltip], ); - const alreadySelectedEmojis = React.useMemo( - () => getViewerAlreadySelectedMessageReactions(item.reactions), - [item.reactions], - ); + const alreadySelectedEmojis = + useViewerAlreadySelectedMessageReactions(reactions); return ( <> diff --git a/native/chat/text-message-tooltip-button.react.js b/native/chat/text-message-tooltip-button.react.js index 45996265b8..37cbfd4966 100644 --- a/native/chat/text-message-tooltip-button.react.js +++ b/native/chat/text-message-tooltip-button.react.js @@ -5,7 +5,7 @@ import Animated from 'react-native-reanimated'; import { localIDPrefix } from 'lib/shared/message-utils.js'; import { - getViewerAlreadySelectedMessageReactions, + useViewerAlreadySelectedMessageReactions, useCanCreateReactionFromMessage, } from 'lib/shared/reaction-utils.js'; @@ -152,10 +152,8 @@ function TextMessageTooltipButton(props: Props): React.Node { [sendReaction, dismissTooltip], ); - const alreadySelectedEmojis = React.useMemo( - () => getViewerAlreadySelectedMessageReactions(item.reactions), - [item.reactions], - ); + const alreadySelectedEmojis = + useViewerAlreadySelectedMessageReactions(reactions); return (