diff --git a/lib/shared/reaction-utils.js b/lib/shared/reaction-utils.js --- a/lib/shared/reaction-utils.js +++ b/lib/shared/reaction-utils.js @@ -2,7 +2,9 @@ import invariant from 'invariant'; import _sortBy from 'lodash/fp/sortBy'; +import * as React from 'react'; +import { useENSNames } from '../hooks/ens-cache'; import type { MessageReactionInfo } from '../selectors/chat-selectors'; import type { RobotextMessageInfo, @@ -41,31 +43,34 @@ +username: string, }; -function createMessageReactionsList( +function useMessageReactionsList( reactions: $ReadOnlyMap, ): $ReadOnlyArray { - const result = []; - - for (const [reaction, reactionInfo] of reactions) { - reactionInfo.users.forEach(user => { - result.push({ - ...user, - username: stringForUserExplicit(user), - reaction, + const withoutENSNames = React.useMemo(() => { + const result = []; + + for (const [reaction, reactionInfo] of reactions) { + reactionInfo.users.forEach(user => { + result.push({ + ...user, + username: stringForUserExplicit(user), + reaction, + }); }); - }); - } - - const sortByNumReactions = (reactionInfo: MessageReactionListInfo) => { - const numOfReactions = reactions.get(reactionInfo.reaction)?.users.length; - return numOfReactions ? -numOfReactions : 0; - }; + } - return _sortBy( - ([sortByNumReactions, 'username']: $ReadOnlyArray< - ((reactionInfo: MessageReactionListInfo) => mixed) | string, - >), - )(result); + const sortByNumReactions = (reactionInfo: MessageReactionListInfo) => { + const numOfReactions = reactions.get(reactionInfo.reaction)?.users.length; + return numOfReactions ? -numOfReactions : 0; + }; + + return _sortBy( + ([sortByNumReactions, 'username']: $ReadOnlyArray< + ((reactionInfo: MessageReactionListInfo) => mixed) | string, + >), + )(result); + }, [reactions]); + return useENSNames(withoutENSNames); } function useCanCreateReactionFromMessage( @@ -99,6 +104,6 @@ export { stringForReactionList, - createMessageReactionsList, + useMessageReactionsList, useCanCreateReactionFromMessage, }; diff --git a/native/chat/message-reactions-modal.react.js b/native/chat/message-reactions-modal.react.js --- a/native/chat/message-reactions-modal.react.js +++ b/native/chat/message-reactions-modal.react.js @@ -6,7 +6,7 @@ import { View, Text, FlatList, TouchableHighlight } from 'react-native'; import type { MessageReactionInfo } from 'lib/selectors/chat-selectors'; -import { createMessageReactionsList } from 'lib/shared/reaction-utils'; +import { useMessageReactionsList } from 'lib/shared/reaction-utils'; import Modal from '../components/modal.react'; import type { RootNavigationProp } from '../navigation/root-navigator.react'; @@ -32,10 +32,7 @@ const close = React.useCallback(() => navigation.goBack(), [navigation]); - const reactionsListData = React.useMemo( - () => createMessageReactionsList(reactions), - [reactions], - ); + const reactionsListData = useMessageReactionsList(reactions); const renderItem = React.useCallback( ({ item }) => { diff --git a/web/modals/chat/message-reactions-modal.react.js b/web/modals/chat/message-reactions-modal.react.js --- a/web/modals/chat/message-reactions-modal.react.js +++ b/web/modals/chat/message-reactions-modal.react.js @@ -3,7 +3,7 @@ import * as React from 'react'; import type { MessageReactionInfo } from 'lib/selectors/chat-selectors'; -import { createMessageReactionsList } from 'lib/shared/reaction-utils'; +import { useMessageReactionsList } from 'lib/shared/reaction-utils'; import Modal from '../modal.react'; import css from './message-reactions-modal.css'; @@ -16,16 +16,18 @@ function MessageReactionsModal(props: Props): React.Node { const { onClose, reactions } = props; - const reactionsList = React.useMemo(() => { - const messageReactionsList = createMessageReactionsList(reactions); - - return messageReactionsList.map(messageReactionUser => ( -
-
{messageReactionUser.username}
-
{messageReactionUser.reaction}
-
- )); - }, [reactions]); + const messageReactionsList = useMessageReactionsList(reactions); + + const reactionsList = React.useMemo( + () => + messageReactionsList.map(messageReactionUser => ( +
+
{messageReactionUser.username}
+
{messageReactionUser.reaction}
+
+ )), + [messageReactionsList], + ); return (