Skip to content

Commit 8ca441b

Browse files
cleanup and fixes around goToMessage
1 parent b3e4c9b commit 8ca441b

File tree

7 files changed

+87
-79
lines changed

7 files changed

+87
-79
lines changed

src/components/Channel/Channel.tsx

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -895,18 +895,18 @@ const ChannelWithContext = <
895895
},
896896
});
897897

898-
const ol_topMessage = messages[0];
899-
const ol_topMessage_id = messages[0]?.id;
900-
const ol_bottomMessage = messages[messages.length - 1];
898+
const oldListTopMessage = messages[0];
899+
const oldListTopMessageId = messages[0]?.id;
900+
const oldListBottomMessage = messages[messages.length - 1];
901901

902-
const nl_topMessage = state.messages[0];
903-
const nl_bottomMessage = state.messages[state.messages.length - 1];
902+
const newListTopMessage = state.messages[0];
903+
const newListBottomMessage = state.messages[state.messages.length - 1];
904904

905905
if (
906-
!ol_topMessage || // previous list was empty
907-
!ol_bottomMessage || // previous list was empty
908-
!nl_topMessage || // new list is truncated
909-
!nl_bottomMessage // new list is truncated
906+
!oldListTopMessage || // previous list was empty
907+
!oldListBottomMessage || // previous list was empty
908+
!newListTopMessage || // new list is truncated
909+
!newListBottomMessage // new list is truncated
910910
) {
911911
/** Channel was truncated */
912912
channel.state.clearMessages();
@@ -916,26 +916,26 @@ const ChannelWithContext = <
916916
return;
917917
}
918918

919-
const ol_topMessage_createdAt = ol_topMessage.created_at;
920-
const ol_bottomMessage_createdAt = ol_bottomMessage.created_at;
921-
const nl_topMessage_createdAt = nl_topMessage.created_at
922-
? new Date(nl_topMessage.created_at)
919+
const oldListTopMessageCreatedAt = oldListTopMessage.created_at;
920+
const oldListBottomMessageCreatedAt = oldListBottomMessage.created_at;
921+
const newListTopMessageCreatedAt = newListTopMessage.created_at
922+
? new Date(newListTopMessage.created_at)
923923
: new Date();
924-
const nl_bottomMessage_createdAt = nl_bottomMessage?.created_at
925-
? new Date(nl_bottomMessage.created_at)
924+
const newListBottomMessageCreatedAt = newListBottomMessage?.created_at
925+
? new Date(newListBottomMessage.created_at)
926926
: new Date();
927927

928928
let finalMessages = [];
929929

930930
if (
931-
ol_topMessage &&
932-
ol_topMessage_createdAt &&
933-
ol_bottomMessage_createdAt &&
934-
nl_topMessage_createdAt < ol_topMessage_createdAt &&
935-
nl_bottomMessage_createdAt >= ol_bottomMessage_createdAt
931+
oldListTopMessage &&
932+
oldListTopMessageCreatedAt &&
933+
oldListBottomMessageCreatedAt &&
934+
newListTopMessageCreatedAt < oldListTopMessageCreatedAt &&
935+
newListBottomMessageCreatedAt >= oldListBottomMessageCreatedAt
936936
) {
937937
const index = state.messages.findIndex(
938-
(m) => m.id === ol_topMessage_id,
938+
(m) => m.id === oldListTopMessageId,
939939
);
940940
finalMessages = state.messages.slice(index);
941941
} else {

src/components/Chat/hooks/useAppStateListener.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,24 @@ export const useAppStateListener = (
1111
useEffect(() => {
1212
listenerExists && AppState.addEventListener('change', handleAppStateChange);
1313

14-
return () => {
14+
return () =>
1515
listenerExists &&
16-
AppState.removeEventListener('change', handleAppStateChange);
17-
};
16+
AppState.removeEventListener('change', handleAppStateChange);
1817
}, []);
1918

2019
const handleAppStateChange = (nextAppState: AppStateStatus) => {
21-
if (appState.current === 'background' && nextAppState === 'active') {
22-
onForeground?.();
20+
if (
21+
appState.current === 'background' &&
22+
nextAppState === 'active' &&
23+
onForeground
24+
) {
25+
onForeground();
2326
} else if (
2427
appState.current.match(/active|inactive/) &&
25-
nextAppState === 'background'
28+
nextAppState === 'background' &&
29+
onBackground
2630
) {
27-
onBackground?.();
31+
onBackground();
2832
}
2933
appState.current = nextAppState;
3034
};

src/components/Message/Message.tsx

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,6 +1103,7 @@ const MessageWithContext = <
11031103
channel,
11041104
disabled,
11051105
files: attachments.files,
1106+
goToMessage,
11061107
groupStyles,
11071108
handleAction,
11081109
handleDeleteMessage,
@@ -1277,6 +1278,7 @@ const areEqual = <
12771278
) => {
12781279
const {
12791280
channel: prevChannel,
1281+
goToMessage: prevGoToMessage,
12801282
lastReceivedId: prevLastReceivedId,
12811283
message: prevMessage,
12821284
showUnreadUnderlay: prevShowUnreadUnderlay,
@@ -1285,6 +1287,7 @@ const areEqual = <
12851287
} = prevProps;
12861288
const {
12871289
channel: nextChannel,
1290+
goToMessage: nextGoToMessage,
12881291
lastReceivedId: nextLastReceivedId,
12891292
message: nextMessage,
12901293
showUnreadUnderlay: nextShowUnreadUnderlay,
@@ -1295,22 +1298,20 @@ const areEqual = <
12951298
const repliesEqual = prevMessage.reply_count === nextMessage.reply_count;
12961299
if (!repliesEqual) return false;
12971300

1298-
/**
1299-
* We need to allow re-render when lastReceivedId changes, for following cases
1300-
* 1. updating the status (seen status) on latest message in list
1301-
* 2. updating quoted messages. Because when you press the quoted message, it makes a call
1302-
* to `goToMessage` function, which is dependent on message list (length specifically).
1303-
*/
13041301
const lastReceivedIdChangedAndMatters =
13051302
prevLastReceivedId !== nextLastReceivedId &&
13061303
(prevLastReceivedId === prevMessage.id ||
13071304
prevLastReceivedId === nextMessage.id ||
13081305
nextLastReceivedId === prevMessage.id ||
1309-
nextLastReceivedId === nextMessage.id ||
1310-
(prevMessage.quoted_message_id && nextMessage.quoted_message_id));
1306+
nextLastReceivedId === nextMessage.id);
13111307

13121308
if (lastReceivedIdChangedAndMatters) return false;
13131309

1310+
const goToMessageChangedAndMatters =
1311+
nextMessage.quoted_message_id && prevGoToMessage !== nextGoToMessage;
1312+
1313+
if (goToMessageChangedAndMatters) return false;
1314+
13141315
const messageEqual =
13151316
prevMessage.deleted_at === nextMessage.deleted_at &&
13161317
(isMessageWithStylesReadByAndDateSeparator(prevMessage) &&

src/components/Message/MessageSimple/MessageContent.tsx

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,11 @@ export type MessageContentPropsWithContext<
8181
MessageContextValue<At, Ch, Co, Ev, Me, Re, Us>,
8282
| 'alignment'
8383
| 'disabled'
84+
| 'goToMessage'
8485
| 'groupStyles'
8586
| 'hasReactions'
8687
| 'isMyMessage'
8788
| 'lastGroupMessage'
88-
| 'lastReceivedId'
8989
| 'members'
9090
| 'message'
9191
| 'messageContentOrder'
@@ -411,10 +411,10 @@ const areEqual = <
411411
nextProps: MessageContentPropsWithContext<At, Ch, Co, Ev, Me, Re, Us>,
412412
) => {
413413
const {
414+
goToMessage: prevGoToMessage,
414415
groupStyles: prevGroupStyles,
415416
hasReactions: prevHasReactions,
416417
lastGroupMessage: prevLastGroupMessage,
417-
lastReceivedId: prevLastReceivedId,
418418
members: prevMembers,
419419
message: prevMessage,
420420
messageContentOrder: prevMessageContentOrder,
@@ -424,10 +424,10 @@ const areEqual = <
424424
tDateTimeParser: prevTDateTimeParser,
425425
} = prevProps;
426426
const {
427+
goToMessage: nextGoToMessage,
427428
groupStyles: nextGroupStyles,
428429
hasReactions: nextHasReactions,
429430
lastGroupMessage: nextLastGroupMessage,
430-
lastReceivedId: nextLastReceivedId,
431431
members: nextMembers,
432432
message: nextMessage,
433433
messageContentOrder: nextMessageContentOrder,
@@ -443,18 +443,10 @@ const areEqual = <
443443
const lastGroupMessageEqual = prevLastGroupMessage === nextLastGroupMessage;
444444
if (!lastGroupMessageEqual) return false;
445445

446-
/**
447-
* We need to allow re-render when lastReceivedId changes, for following cases
448-
* 1. updating the status (seen status) on latest message in list
449-
* 2. updating quoted messages. Because when you press the quoted message, it makes a call
450-
* to `goToMessage` function, which is dependent on message list (length specifically).
451-
*/
452-
const lastReceivedIdChangedAndMatters =
453-
prevLastReceivedId !== nextLastReceivedId &&
454-
prevMessage.quoted_message_id &&
455-
nextMessage.quoted_message_id;
456-
457-
if (lastReceivedIdChangedAndMatters) return false;
446+
const goToMessageChangedAndMatters =
447+
prevGoToMessage !== nextGoToMessage && nextMessage.quoted_message_id;
448+
449+
if (goToMessageChangedAndMatters) return false;
458450

459451
const onlyEmojisEqual = prevOnlyEmojis === nextOnlyEmojis;
460452
if (!onlyEmojisEqual) return false;

src/components/Message/hooks/useCreateMessageContext.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export const useCreateMessageContext = <
2929
channel,
3030
disabled,
3131
files,
32+
goToMessage,
3233
groupStyles,
3334
handleAction,
3435
handleDeleteMessage,
@@ -92,6 +93,7 @@ export const useCreateMessageContext = <
9293
channel,
9394
disabled,
9495
files,
96+
goToMessage,
9597
groupStyles,
9698
handleAction,
9799
handleDeleteMessage,

src/components/MessageList/MessageList.tsx

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import React, { useEffect, useMemo, useRef, useState } from 'react';
1+
import React, {
2+
useCallback,
3+
useEffect,
4+
useMemo,
5+
useRef,
6+
useState,
7+
} from 'react';
28
import {
39
FlatListProps,
410
FlatList as FlatListType,
@@ -522,13 +528,13 @@ const MessageListWithContext = <
522528
setScrollToBottomButtonVisible(false);
523529
resetPaginationTrackers();
524530

525-
if (flatListRef.current) {
526-
setTimeout(() => {
531+
setTimeout(
532+
() =>
527533
flatListRef.current?.scrollToOffset({
528534
offset: 0,
529-
});
530-
});
531-
}
535+
}),
536+
50,
537+
);
532538
setTimeout(() => {
533539
channelResyncScrollSet.current = true;
534540
if (channel.countUnread() > 0) {
@@ -606,9 +612,7 @@ const MessageListWithContext = <
606612
: []
607613
}
608614
lastReceivedId={
609-
lastReceivedId === message.id || message.quoted_message_id
610-
? lastReceivedId
611-
: undefined
615+
lastReceivedId === message.id ? lastReceivedId : undefined
612616
}
613617
message={message}
614618
onThreadSelect={onThreadSelect}
@@ -842,30 +846,34 @@ const MessageListWithContext = <
842846
markRead();
843847
}
844848
};
845-
const goToMessage = (messageId: string) => {
846-
const indexOfParentInMessageList = messageList.findIndex(
847-
(message) => message?.id === messageId,
848-
);
849849

850-
if (indexOfParentInMessageList > -1) {
851-
try {
852-
if (flatListRef.current) {
853-
flatListRef.current.scrollToIndex({
854-
index: indexOfParentInMessageList,
855-
viewPosition: 0.5,
856-
});
857-
setTargetedMessage(messageId);
850+
const goToMessage = useCallback(
851+
(messageId: string) => {
852+
const indexOfParentInMessageList = messageList.findIndex(
853+
(message) => message?.id === messageId,
854+
);
855+
856+
if (indexOfParentInMessageList > -1) {
857+
try {
858+
if (flatListRef.current) {
859+
flatListRef.current.scrollToIndex({
860+
index: indexOfParentInMessageList,
861+
viewPosition: 0.5,
862+
});
863+
setTargetedMessage(messageId);
858864

859-
return;
865+
return;
866+
}
867+
} catch (_) {
868+
// do nothing;
860869
}
861-
} catch (_) {
862-
// do nothing;
863870
}
864-
}
865871

866-
loadChannelAtMessage({ messageId });
867-
resetPaginationTrackers();
868-
};
872+
loadChannelAtMessage({ messageId });
873+
resetPaginationTrackers();
874+
},
875+
[messageListLengthAfterUpdate, lastReceivedId],
876+
);
869877

870878
const messagesWithImages = messageList.filter((message) => {
871879
if (!message.deleted_at && message.attachments) {

src/contexts/messageContext/MessageContext.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ export type MessageContextValue<
111111
showMessageStatus: boolean;
112112
/** Whether or not the Message is part of a Thread */
113113
threadList: boolean;
114+
goToMessage?: (messageId: string) => void;
114115
/** Latest message id on current channel */
115116
lastReceivedId?: string;
116117
/** Prevent message being pressed for image viewer view */

0 commit comments

Comments
 (0)