Skip to content

Commit 4ec3518

Browse files
authored
fix: update quoting message on update of quoted message (#2408)
1 parent 7929bc2 commit 4ec3518

File tree

2 files changed

+79
-12
lines changed

2 files changed

+79
-12
lines changed

src/components/Message/__tests__/utils.test.js

+58
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,64 @@ describe('Message utils', () => {
150150
expect(shouldUpdate).toBe(true);
151151
});
152152

153+
it('should update if rendered with a different quoted message', () => {
154+
const cases = [
155+
['deleted_at', undefined, new Date().toISOString()],
156+
['latest_reactions', [], [1]],
157+
['own_reactions', [], [1]],
158+
['pinned', false, true],
159+
['reply_count', 0, 1],
160+
['status', 'sending', 'received'],
161+
['text', '', 'a'],
162+
['type', 'X', 'Y'],
163+
['updated_at', new Date(1).toISOString(), new Date(2).toISOString()],
164+
[
165+
'user',
166+
{ updated_at: new Date(1).toISOString() },
167+
{ updated_at: new Date(2).toISOString() },
168+
],
169+
];
170+
const message = generateMessage();
171+
const quotedMessage = generateMessage();
172+
cases.forEach(([key, prevVal, nextVal]) => {
173+
const shouldUpdate = !areMessagePropsEqual(
174+
{
175+
message: {
176+
...message,
177+
quoted_message: { ...quotedMessage, [key]: prevVal },
178+
quoted_message_id: quotedMessage.id,
179+
},
180+
},
181+
{
182+
message: {
183+
...message,
184+
quoted_message: { ...quotedMessage, [key]: nextVal },
185+
quoted_message_id: quotedMessage.id,
186+
},
187+
},
188+
);
189+
expect(shouldUpdate).toBe(true);
190+
});
191+
expect(
192+
!areMessagePropsEqual(
193+
{
194+
message: {
195+
...message,
196+
quoted_message: undefined,
197+
quoted_message_id: undefined,
198+
},
199+
},
200+
{
201+
message: {
202+
...message,
203+
quoted_message: quotedMessage,
204+
quoted_message_id: quotedMessage.id,
205+
},
206+
},
207+
),
208+
).toBe(true);
209+
});
210+
153211
it('should update if rendered with a different message is read by other users', () => {
154212
const message = generateMessage();
155213
const currentReadBy = [alice];

src/components/Message/utils.tsx

+21-12
Original file line numberDiff line numberDiff line change
@@ -232,22 +232,31 @@ export const showMessageActionsBox = (
232232
return true;
233233
};
234234

235-
const areMessagesEqual = <
235+
function areMessagesEqual<
236236
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics
237237
>(
238238
prevMessage: StreamMessage<StreamChatGenerics>,
239239
nextMessage: StreamMessage<StreamChatGenerics>,
240-
) =>
241-
prevMessage.deleted_at === nextMessage.deleted_at &&
242-
prevMessage.latest_reactions?.length === nextMessage.latest_reactions?.length &&
243-
prevMessage.own_reactions?.length === nextMessage.own_reactions?.length &&
244-
prevMessage.pinned === nextMessage.pinned &&
245-
prevMessage.reply_count === nextMessage.reply_count &&
246-
prevMessage.status === nextMessage.status &&
247-
prevMessage.text === nextMessage.text &&
248-
prevMessage.type === nextMessage.type &&
249-
prevMessage.updated_at === nextMessage.updated_at &&
250-
prevMessage.user?.updated_at === nextMessage.user?.updated_at;
240+
): boolean {
241+
return (
242+
prevMessage.deleted_at === nextMessage.deleted_at &&
243+
prevMessage.latest_reactions?.length === nextMessage.latest_reactions?.length &&
244+
prevMessage.own_reactions?.length === nextMessage.own_reactions?.length &&
245+
prevMessage.pinned === nextMessage.pinned &&
246+
prevMessage.reply_count === nextMessage.reply_count &&
247+
prevMessage.status === nextMessage.status &&
248+
prevMessage.text === nextMessage.text &&
249+
prevMessage.type === nextMessage.type &&
250+
prevMessage.updated_at === nextMessage.updated_at &&
251+
prevMessage.user?.updated_at === nextMessage.user?.updated_at &&
252+
Boolean(prevMessage.quoted_message) === Boolean(nextMessage.quoted_message) &&
253+
(!prevMessage.quoted_message ||
254+
areMessagesEqual(
255+
prevMessage.quoted_message as StreamMessage<StreamChatGenerics>,
256+
nextMessage.quoted_message as StreamMessage<StreamChatGenerics>,
257+
))
258+
);
259+
}
251260

252261
export const areMessagePropsEqual = <
253262
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics

0 commit comments

Comments
 (0)