Skip to content

Commit 4aa4dd0

Browse files
committed
perf: messages rerendering on every composer change
1 parent d71cb3f commit 4aa4dd0

File tree

6 files changed

+31
-11
lines changed

6 files changed

+31
-11
lines changed

package/src/components/Message/Message.tsx

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { useMessageActions } from './hooks/useMessageActions';
99
import { useProcessReactions } from './hooks/useProcessReactions';
1010
import { messageActions as defaultMessageActions } from './utils/messageActions';
1111

12+
import { useMessageComposer } from '../../contexts';
1213
import {
1314
ChannelContextValue,
1415
useChannelContext,
@@ -31,6 +32,7 @@ import {
3132
useTranslationContext,
3233
} from '../../contexts/translationContext/TranslationContext';
3334

35+
import { useStableCallback } from '../../hooks';
3436
import { isVideoPlayerAvailable, NativeHandlers } from '../../native';
3537
import { FileTypes } from '../../types/types';
3638
import {
@@ -140,7 +142,10 @@ export type MessagePropsWithContext = Pick<
140142
'groupStyles' | 'handleReaction' | 'message' | 'isMessageAIGenerated' | 'readBy'
141143
>
142144
> &
143-
Pick<MessageContextValue, 'groupStyles' | 'message' | 'isMessageAIGenerated' | 'readBy'> &
145+
Pick<
146+
MessageContextValue,
147+
'groupStyles' | 'message' | 'isMessageAIGenerated' | 'readBy' | 'setQuotedMessage'
148+
> &
144149
Pick<
145150
MessagesContextValue,
146151
| 'sendReaction'
@@ -264,6 +269,7 @@ const MessageWithContext = (props: MessagePropsWithContext) => {
264269
threadList = false,
265270
updateMessage,
266271
readBy,
272+
setQuotedMessage,
267273
} = props;
268274
const isMessageAIGenerated = messagesContext.isMessageAIGenerated;
269275
const isAIGenerated = useMemo(
@@ -499,6 +505,7 @@ const MessageWithContext = (props: MessagePropsWithContext) => {
499505
retrySendMessage,
500506
sendReaction,
501507
setEditingState,
508+
setQuotedMessage,
502509
supportedReactions,
503510
});
504511

@@ -543,6 +550,7 @@ const MessageWithContext = (props: MessagePropsWithContext) => {
543550
selectReaction,
544551
sendReaction,
545552
setEditingState,
553+
setQuotedMessage,
546554
supportedReactions,
547555
t,
548556
updateMessage,
@@ -691,6 +699,7 @@ const MessageWithContext = (props: MessagePropsWithContext) => {
691699
reactions,
692700
readBy,
693701
setIsEditedMessageOpen,
702+
setQuotedMessage,
694703
showAvatar,
695704
showMessageOverlay,
696705
showMessageStatus: typeof showMessageStatus === 'boolean' ? showMessageStatus : isMyMessage,
@@ -938,6 +947,10 @@ export const Message = (props: MessageProps) => {
938947
const { openThread } = useThreadContext();
939948
const { t } = useTranslationContext();
940949
const readBy = useMemo(() => getReadState(message, read), [message, read]);
950+
const messageComposer = useMessageComposer();
951+
const setQuotedMessage = useStableCallback((message: LocalMessage | null) =>
952+
messageComposer.setQuotedMessage(message),
953+
);
941954

942955
return (
943956
<MemoizedMessage
@@ -951,6 +964,7 @@ export const Message = (props: MessageProps) => {
951964
messagesContext,
952965
openThread,
953966
readBy,
967+
setQuotedMessage,
954968
t,
955969
}}
956970
{...props}

package/src/components/Message/MessageSimple/MessageSimple.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import {
1818
MessageContextValue,
1919
useMessageContext,
2020
} from '../../../contexts/messageContext/MessageContext';
21-
import { useMessageComposer } from '../../../contexts/messageInputContext/hooks/useMessageComposer';
2221
import {
2322
MessagesContextValue,
2423
useMessagesContext,
@@ -70,6 +69,7 @@ export type MessageSimplePropsWithContext = Pick<
7069
| 'onlyEmojis'
7170
| 'otherAttachments'
7271
| 'showMessageStatus'
72+
| 'setQuotedMessage'
7373
> &
7474
Pick<
7575
MessagesContextValue,
@@ -130,6 +130,7 @@ const MessageSimpleWithContext = (props: MessageSimplePropsWithContext) => {
130130
ReactionListTop,
131131
showMessageStatus,
132132
shouldRenderSwipeableWrapper,
133+
setQuotedMessage,
133134
} = props;
134135

135136
const {
@@ -161,7 +162,6 @@ const MessageSimpleWithContext = (props: MessageSimplePropsWithContext) => {
161162
isVeryLastMessage,
162163
messageGroupedSingleOrBottom,
163164
} = useMessageData({});
164-
const messageComposer = useMessageComposer();
165165

166166
const lastMessageInMessageListStyles = [styles.lastMessageContainer, lastMessageContainer];
167167
const messageGroupedSingleOrBottomStyles = {
@@ -215,8 +215,8 @@ const MessageSimpleWithContext = (props: MessageSimplePropsWithContext) => {
215215
);
216216

217217
const onSwipeToReply = useCallback(() => {
218-
messageComposer.setQuotedMessage(message);
219-
}, [messageComposer, message]);
218+
setQuotedMessage(message);
219+
}, [setQuotedMessage, message]);
220220

221221
const THRESHOLD = 25;
222222

@@ -608,6 +608,7 @@ export const MessageSimple = (props: MessageSimpleProps) => {
608608
otherAttachments,
609609
showMessageStatus,
610610
isMessageAIGenerated,
611+
setQuotedMessage,
611612
} = useMessageContext();
612613
const {
613614
enableMessageGroupingByUser,
@@ -662,6 +663,7 @@ export const MessageSimple = (props: MessageSimpleProps) => {
662663
ReactionListBottom,
663664
reactionListPosition,
664665
ReactionListTop,
666+
setQuotedMessage,
665667
shouldRenderSwipeableWrapper,
666668
showMessageStatus,
667669
}}

package/src/components/Message/hooks/useCreateMessageContext.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export const useCreateMessageContext = ({
4141
showMessageStatus,
4242
threadList,
4343
videos,
44+
setQuotedMessage,
4445
}: MessageContextValue) => {
4546
const groupStylesLength = groupStyles.length;
4647
const reactionsValue = reactions.map(({ count, own, type }) => `${own}${type}${count}`).join();
@@ -84,6 +85,7 @@ export const useCreateMessageContext = ({
8485
reactions,
8586
readBy,
8687
setIsEditedMessageOpen,
88+
setQuotedMessage,
8789
showAvatar,
8890
showMessageOverlay,
8991
showMessageStatus,

package/src/components/Message/hooks/useMessageActionHandlers.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { Alert } from 'react-native';
33
import type { ChannelContextValue } from '../../../contexts/channelContext/ChannelContext';
44
import type { ChatContextValue } from '../../../contexts/chatContext/ChatContext';
55
import type { MessageContextValue } from '../../../contexts/messageContext/MessageContext';
6-
import { useMessageComposer } from '../../../contexts/messageInputContext/hooks/useMessageComposer';
76
import type { MessagesContextValue } from '../../../contexts/messagesContext/MessagesContext';
87

98
import { useTranslationContext } from '../../../contexts/translationContext/TranslationContext';
@@ -18,6 +17,7 @@ export const useMessageActionHandlers = ({
1817
retrySendMessage,
1918
sendReaction,
2019
setEditingState,
20+
setQuotedMessage,
2121
}: Pick<
2222
MessagesContextValue,
2323
| 'sendReaction'
@@ -29,13 +29,12 @@ export const useMessageActionHandlers = ({
2929
> &
3030
Pick<ChannelContextValue, 'channel' | 'enforceUniqueReaction'> &
3131
Pick<ChatContextValue, 'client'> &
32-
Pick<MessageContextValue, 'message'>) => {
32+
Pick<MessageContextValue, 'message' | 'setQuotedMessage'>) => {
3333
const { t } = useTranslationContext();
3434
const handleResendMessage = () => retrySendMessage(message);
35-
const messageComposer = useMessageComposer();
3635

3736
const handleQuotedReplyMessage = () => {
38-
messageComposer.setQuotedMessage(message);
37+
setQuotedMessage(message);
3938
};
4039

4140
const isMuted = (client.mutedUsers || []).some(

package/src/components/Message/hooks/useMessageActions.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export type MessageActionsHookProps = Pick<
5858
Pick<ChannelContextValue, 'channel' | 'enforceUniqueReaction'> &
5959
Pick<ChatContextValue, 'client'> &
6060
Pick<ThreadContextValue, 'openThread'> &
61-
Pick<MessageContextValue, 'dismissOverlay' | 'message'> &
61+
Pick<MessageContextValue, 'dismissOverlay' | 'message' | 'setQuotedMessage'> &
6262
Pick<TranslationContextValue, 't'> & {
6363
onThreadSelect?: (message: LocalMessage) => void;
6464
};
@@ -91,6 +91,7 @@ export const useMessageActions = ({
9191
setEditingState,
9292
supportedReactions,
9393
t,
94+
setQuotedMessage,
9495
}: MessageActionsHookProps) => {
9596
const {
9697
theme: {
@@ -119,6 +120,7 @@ export const useMessageActions = ({
119120
retrySendMessage,
120121
sendReaction,
121122
setEditingState,
123+
setQuotedMessage,
122124
supportedReactions,
123125
});
124126

package/src/contexts/messageContext/MessageContext.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, { PropsWithChildren, useContext } from 'react';
22

3-
import type { Attachment, LocalMessage } from 'stream-chat';
3+
import type { Attachment, LocalMessage, MessageComposer } from 'stream-chat';
44

55
import type { ActionHandler } from '../../components/Attachment/Attachment';
66
import { ReactionSummary } from '../../components/Message/hooks/useProcessReactions';
@@ -119,6 +119,7 @@ export type MessageContextValue = {
119119
preventPress?: boolean;
120120
/** Whether or not the avatar show show next to Message */
121121
showAvatar?: boolean;
122+
setQuotedMessage: MessageComposer['setQuotedMessage'];
122123
} & Pick<ChannelContextValue, 'channel' | 'members'>;
123124

124125
export const MessageContext = React.createContext(

0 commit comments

Comments
 (0)