-
Notifications
You must be signed in to change notification settings - Fork 314
/
MessageList.tsx
1200 lines (1101 loc) · 41.5 KB
/
MessageList.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import {
FlatListProps,
FlatList as FlatListType,
Platform,
ScrollViewProps,
StyleSheet,
View,
ViewToken,
} from 'react-native';
import {
isMessageWithStylesReadByAndDateSeparator,
MessageType,
useMessageList,
} from './hooks/useMessageList';
import { InlineLoadingMoreIndicator } from './InlineLoadingMoreIndicator';
import { InlineLoadingMoreRecentIndicator } from './InlineLoadingMoreRecentIndicator';
import { InlineLoadingMoreThreadIndicator } from './InlineLoadingMoreThreadIndicator';
import { getLastReceivedMessage } from './utils/getLastReceivedMessage';
import {
AttachmentPickerContextValue,
useAttachmentPickerContext,
} from '../../contexts/attachmentPickerContext/AttachmentPickerContext';
import {
ChannelContextValue,
useChannelContext,
} from '../../contexts/channelContext/ChannelContext';
import { ChatContextValue, useChatContext } from '../../contexts/chatContext/ChatContext';
import { useDebugContext } from '../../contexts/debugContext/DebugContext';
import {
ImageGalleryContextValue,
useImageGalleryContext,
} from '../../contexts/imageGalleryContext/ImageGalleryContext';
import {
MessagesContextValue,
useMessagesContext,
} from '../../contexts/messagesContext/MessagesContext';
import {
OverlayContextValue,
useOverlayContext,
} from '../../contexts/overlayContext/OverlayContext';
import {
PaginatedMessageListContextValue,
usePaginatedMessageListContext,
} from '../../contexts/paginatedMessageListContext/PaginatedMessageListContext';
import { mergeThemes, ThemeProvider, useTheme } from '../../contexts/themeContext/ThemeContext';
import { ThreadContextValue, useThreadContext } from '../../contexts/threadContext/ThreadContext';
import {
isDayOrMoment,
TranslationContextValue,
useTranslationContext,
} from '../../contexts/translationContext/TranslationContext';
import type { DefaultStreamChatGenerics } from '../../types/types';
const styles = StyleSheet.create({
container: {
alignItems: 'center',
flex: 1,
width: '100%',
},
contentContainer: {
flexGrow: 1,
/**
* paddingBottom is set to 4 to account for the default date
* header and inline indicator alignment. The top margin is 8
* on the header but 4 on the inline date, this adjusts the spacing
* to allow the "first" inline date to align with the date header.
*/
paddingBottom: 4,
},
flex: { flex: 1 },
invert: { transform: [{ scaleY: -1 }] },
invertAndroid: {
// Invert the Y AND X axis to prevent a react native issue that can lead to ANRs on android 13
// details: https://github.com/Expensify/App/pull/12820
transform: [{ scaleX: -1 }, { scaleY: -1 }],
},
listContainer: {
flex: 1,
width: '100%',
},
messagePadding: {
paddingHorizontal: 8,
},
stickyHeader: {
position: 'absolute',
top: 0,
},
});
const InvertedCellRendererComponent = (props: React.PropsWithChildren<unknown>) => (
<View {...props} style={styles.invertAndroid} />
);
const keyExtractor = <
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
>(
item: MessageType<StreamChatGenerics>,
) => {
if (item.id) return item.id;
if (item.created_at)
return typeof item.created_at === 'string' ? item.created_at : item.created_at.toISOString();
return Date.now().toString();
};
const flatListViewabilityConfig = {
viewAreaCoveragePercentThreshold: 1,
};
type MessageListPropsWithContext<
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
> = Pick<AttachmentPickerContextValue, 'closePicker' | 'selectedPicker' | 'setSelectedPicker'> &
Pick<
ChannelContextValue<StreamChatGenerics>,
| 'channel'
| 'disabled'
| 'EmptyStateIndicator'
| 'hideStickyDateHeader'
| 'loadChannelAroundMessage'
| 'loading'
| 'LoadingIndicator'
| 'markRead'
| 'NetworkDownIndicator'
| 'reloadChannel'
| 'scrollToFirstUnreadThreshold'
| 'setTargetedMessage'
| 'StickyHeader'
| 'targetedMessage'
> &
Pick<ChatContextValue<StreamChatGenerics>, 'client'> &
Pick<ImageGalleryContextValue<StreamChatGenerics>, 'setMessages'> &
Pick<
PaginatedMessageListContextValue<StreamChatGenerics>,
'hasNoMoreRecentMessagesToLoad' | 'loadMore' | 'loadMoreRecent'
> &
Pick<OverlayContextValue, 'overlay'> &
Pick<
MessagesContextValue<StreamChatGenerics>,
| 'DateHeader'
| 'disableTypingIndicator'
| 'FlatList'
| 'initialScrollToFirstUnreadMessage'
| 'InlineDateSeparator'
| 'InlineUnreadIndicator'
| 'legacyImageViewerSwipeBehaviour'
| 'Message'
| 'ScrollToBottomButton'
| 'MessageSystem'
| 'myMessageTheme'
| 'TypingIndicator'
| 'TypingIndicatorContainer'
> &
Pick<ThreadContextValue<StreamChatGenerics>, 'loadMoreThread' | 'thread'> &
Pick<TranslationContextValue, 't' | 'tDateTimeParser'> & {
/**
* Besides existing (default) UX behavior of underlying FlatList of MessageList component, if you want
* to attach some additional props to underlying FlatList, you can add it to following prop.
*
* You can find list of all the available FlatList props here - https://facebook.github.io/react-native/docs/flatlist#props
*
* **NOTE** Don't use `additionalFlatListProps` to get access to ref of flatlist. Use `setFlatListRef` instead.
*
* e.g.
* ```js
* <MessageList
* additionalFlatListProps={{ bounces: true, keyboardDismissMode: true }} />
* ```
*/
additionalFlatListProps?: Partial<FlatListProps<MessageType<StreamChatGenerics>>>;
/**
* UI component for footer of message list. By default message list will use `InlineLoadingMoreIndicator`
* as FooterComponent. If you want to implement your own inline loading indicator, you can access `loadingMore`
* from context.
*
* This is a [ListHeaderComponent](https://facebook.github.io/react-native/docs/flatlist#listheadercomponent) of FlatList
* used in MessageList. Should be used for header by default if inverted is true or defaulted
*/
FooterComponent?: React.ComponentType;
/**
* UI component for header of message list. By default message list will use `InlineLoadingMoreRecentIndicator`
* as HeaderComponent. If you want to implement your own inline loading indicator, you can access `loadingMoreRecent`
* from context.
*
* This is a [ListFooterComponent](https://facebook.github.io/react-native/docs/flatlist#listheadercomponent) of FlatList
* used in MessageList. Should be used for header if inverted is false
*/
HeaderComponent?: React.ComponentType;
/** Whether or not the FlatList is inverted. Defaults to true */
inverted?: boolean;
isListActive?: boolean;
/** Turn off grouping of messages by user */
noGroupByUser?: boolean;
onListScroll?: ScrollViewProps['onScroll'];
/**
* Handler to open the thread on message. This is callback for touch event for replies button.
*
* @param message A message object to open the thread upon.
*/
onThreadSelect?: (message: ThreadContextValue<StreamChatGenerics>['thread']) => void;
/**
* Use `setFlatListRef` to get access to ref to inner FlatList.
*
* e.g.
* ```js
* <MessageList
* setFlatListRef={(ref) => {
* // Use ref for your own good
* }}
* ```
*/
setFlatListRef?: (ref: FlatListType<MessageType<StreamChatGenerics>> | null) => void;
/**
* Boolean whether or not the Messages in the MessageList are part of a Thread
**/
threadList?: boolean;
};
/**
* The message list component renders a list of messages. It consumes the following contexts:
*
* [ChannelContext](https://getstream.io/chat/docs/sdk/reactnative/contexts/channel-context/)
* [ChatContext](https://getstream.io/chat/docs/sdk/reactnative/contexts/chat-context/)
* [MessagesContext](https://getstream.io/chat/docs/sdk/reactnative/contexts/messages-context/)
* [ThreadContext](https://getstream.io/chat/docs/sdk/reactnative/contexts/thread-context/)
* [TranslationContext](https://getstream.io/chat/docs/sdk/reactnative/contexts/translation-context/)
*/
const MessageListWithContext = <
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
>(
props: MessageListPropsWithContext<StreamChatGenerics>,
) => {
const LoadingMoreIndicator = props.threadList
? InlineLoadingMoreThreadIndicator
: InlineLoadingMoreIndicator;
const {
additionalFlatListProps,
channel,
client,
closePicker,
DateHeader,
disabled,
disableTypingIndicator,
EmptyStateIndicator,
FlatList,
FooterComponent = LoadingMoreIndicator,
hasNoMoreRecentMessagesToLoad,
HeaderComponent = InlineLoadingMoreRecentIndicator,
hideStickyDateHeader,
initialScrollToFirstUnreadMessage,
InlineDateSeparator,
InlineUnreadIndicator,
inverted = true,
isListActive = false,
legacyImageViewerSwipeBehaviour,
loadChannelAroundMessage,
loading,
LoadingIndicator,
loadMore,
loadMoreRecent,
loadMoreThread,
markRead,
Message,
MessageSystem,
myMessageTheme,
NetworkDownIndicator,
noGroupByUser,
onListScroll,
onThreadSelect,
overlay,
reloadChannel,
ScrollToBottomButton,
selectedPicker,
setFlatListRef,
setMessages,
setSelectedPicker,
setTargetedMessage,
StickyHeader,
targetedMessage,
tDateTimeParser,
thread,
threadList = false,
TypingIndicator,
TypingIndicatorContainer,
} = props;
const { theme } = useTheme();
const {
colors: { white_snow },
messageList: { container, contentContainer, listContainer },
} = theme;
const modifiedTheme = useMemo(
() => mergeThemes({ style: myMessageTheme, theme }),
[myMessageTheme, theme],
);
const messageList = useMessageList<StreamChatGenerics>({
noGroupByUser,
threadList,
});
const messageListLengthBeforeUpdate = useRef(0);
const messageListLengthAfterUpdate = messageList.length;
/**
* We need topMessage and channelLastRead values to set the initial scroll position.
* So these values only get used if `initialScrollToFirstUnreadMessage` prop is true.
*/
const topMessageBeforeUpdate = useRef<MessageType<StreamChatGenerics>>();
const topMessageAfterUpdate = messageList[messageList.length - 1];
const [autoscrollToTop, setAutoscrollToTop] = useState(false);
/**
* We want to call onEndReached and onStartReached only once, per content length.
* We keep track of calls to these functions per content length, with following trackers.
*/
const onStartReachedTracker = useRef<Record<number, boolean>>({});
const onEndReachedTracker = useRef<Record<number, boolean>>({});
const onStartReachedInPromise = useRef<Promise<void> | null>(null);
const onEndReachedInPromise = useRef<Promise<void> | null>(null);
const flatListRef = useRef<FlatListType<MessageType<StreamChatGenerics>> | null>(null);
/**
* Flag to track if the initial scroll has been set
* If the prop `initialScrollToFirstUnreadMessage` was enabled, then we scroll to the unread msg and set it to true
* If not, the default offset of 0 for flatList means that it has been set already
*/
const [isInitialScrollDone, setInitialScrollDone] = useState(!initialScrollToFirstUnreadMessage);
const channelResyncScrollSet = useRef<boolean>(true);
/**
* The timeout id used to debounce our scrollToIndex calls on messageList updates
*/
const scrollToDebounceTimeoutRef = useRef<NodeJS.Timeout>();
/**
* The timeout id used to lazier load the initial scroll set flag
*/
const initialScrollSettingTimeoutRef = useRef<NodeJS.Timeout>();
/**
* If a messageId was requested to scroll to but was unloaded,
* this flag keeps track of it to scroll to it after loading the message
*/
const messageIdToScrollToRef = useRef<string>();
/**
* Last messageID that was scrolled to after loading a new message list,
* this flag keeps track of it so that we dont scroll to it again on target message set
*/
const messageIdLastScrolledToRef = useRef<string>();
const [hasMoved, setHasMoved] = useState(false);
const [lastReceivedId, setLastReceivedId] = useState(getLastReceivedMessage(messageList)?.id);
const [scrollToBottomButtonVisible, setScrollToBottomButtonVisible] = useState(false);
const [stickyHeaderDate, setStickyHeaderDate] = useState<Date | undefined>();
const stickyHeaderDateRef = useRef<Date | undefined>();
// ref for channel to use in useEffect without triggering it on channel change
const channelRef = useRef(channel);
channelRef.current = channel;
const updateStickyHeaderDateIfNeeded = (viewableItems: ViewToken[]) => {
if (viewableItems.length) {
const lastItem = viewableItems.pop() as {
item: MessageType<StreamChatGenerics>;
};
const isMessageTypeDeleted = lastItem.item.type === 'deleted';
if (
lastItem?.item?.created_at &&
!isMessageTypeDeleted &&
typeof lastItem.item.created_at !== 'string' &&
lastItem.item.created_at.toDateString() !== stickyHeaderDateRef.current?.toDateString()
) {
stickyHeaderDateRef.current = lastItem.item.created_at;
setStickyHeaderDate(lastItem.item.created_at);
}
}
};
/**
* FlatList doesn't accept changeable function for onViewableItemsChanged prop.
* Thus useRef.
*/
const onViewableItemsChanged = useRef(
({ viewableItems }: { viewableItems: ViewToken[] | undefined }) => {
/**
* When a new message comes in, list scrolls down to the bottom automatically (using prop `maintainVisibleContentPosition`)
* and we mark the channel as read from handleScroll function.
* Although this logic is dependent on the fact that `onScroll` event gets triggered during this process.
* But for Android, this event is not triggered when messages length is lesser than visible screen height.
*
* And thus we need to check if the message list length is lesser than visible screen height and mark the channel as read.
*/
if (
Platform.OS === 'android' &&
viewableItems?.length &&
viewableItems?.length >= messageListLengthBeforeUpdate.current
) {
channel.markRead();
}
if (viewableItems && !hideStickyDateHeader) {
updateStickyHeaderDateIfNeeded(viewableItems);
}
},
);
/**
* Resets the pagination trackers, doing so cancels currently scheduled loading more calls
*/
const resetPaginationTrackersRef = useRef(() => {
onStartReachedTracker.current = {};
onEndReachedTracker.current = {};
});
useEffect(() => {
setScrollToBottomButtonVisible(false);
}, [disabled]);
useEffect(() => {
const getShouldMarkReadAutomatically = (): boolean => {
if (loading || !channel) {
// nothing to do
return false;
} else if (channel.countUnread() > 0) {
if (!initialScrollToFirstUnreadMessage) {
/*
* In this case MessageList won't scroll to first unread message when opened, so we can mark
* the channel as read right after opening.
* */
return true;
} else {
/*
* In this case MessageList will be opened to first unread message.
* But if there are were not enough unread messages, so that scrollToBottom button was not shown
* then MessageList won't need to scroll up. So we can safely mark the channel as read right after opening.
*
* NOTE: we must ensure that initial scroll is done, otherwise we do not wait till the unread scroll is finished
* */
if (scrollToBottomButtonVisible) return false;
/* if scrollToBottom button was not visible, wait till
* - initial scroll is done (indicates that if scrolling to index was needed it was triggered)
* */
return isInitialScrollDone;
}
}
return false;
};
if (getShouldMarkReadAutomatically()) {
markRead();
}
}, [loading, scrollToBottomButtonVisible, isInitialScrollDone]);
useEffect(() => {
const lastReceivedMessage = getLastReceivedMessage(messageList);
const hasNewMessage = lastReceivedId !== lastReceivedMessage?.id;
const isMyMessage = lastReceivedMessage?.user?.id === client.userID;
setLastReceivedId(lastReceivedMessage?.id);
/**
* Scroll down when
* 1. you send a new message to channel
* 2. new message list is small than the one before update - channel has resynced
* 3. created_at timestamp of top message before update is lesser than created_at timestamp of top message after update - channel has resynced
*/
const scrollToBottomIfNeeded = () => {
if (!client || !channel || messageList.length === 0) {
return;
}
if (
(hasNewMessage && isMyMessage) ||
messageListLengthAfterUpdate < messageListLengthBeforeUpdate.current ||
(topMessageBeforeUpdate.current?.created_at &&
topMessageAfterUpdate?.created_at &&
topMessageBeforeUpdate.current.created_at < topMessageAfterUpdate.created_at)
) {
channelResyncScrollSet.current = false;
setScrollToBottomButtonVisible(false);
resetPaginationTrackersRef.current();
setTimeout(() => {
flatListRef.current?.scrollToOffset({
offset: 0,
});
}, 50);
setTimeout(() => {
channelResyncScrollSet.current = true;
if (channel.countUnread() > 0) {
markRead();
}
}, 500);
}
};
if (threadList || hasNoMoreRecentMessagesToLoad) {
scrollToBottomIfNeeded();
} else {
setScrollToBottomButtonVisible(true);
}
if (
!hasNoMoreRecentMessagesToLoad &&
flatListRef.current &&
messageListLengthBeforeUpdate.current === 0 &&
messageListLengthAfterUpdate < 10
) {
/**
* Trigger onStartReached on first load, if messages are not enough to fill the screen.
* This is important especially for android, where you can't overscroll.
*/
maybeCallOnStartReached(10);
}
messageListLengthBeforeUpdate.current = messageListLengthAfterUpdate;
topMessageBeforeUpdate.current = topMessageAfterUpdate;
}, [messageListLengthAfterUpdate, topMessageAfterUpdate?.id]);
useEffect(() => {
setAutoscrollToTop(hasNoMoreRecentMessagesToLoad);
}, [messageList, hasNoMoreRecentMessagesToLoad]);
const shouldApplyAndroidWorkaround = inverted && Platform.OS === 'android';
const renderItem = ({
index,
item: message,
}: {
index: number;
item: MessageType<StreamChatGenerics>;
}) => {
if (!channel || (!channel.initialized && !channel.offlineMode)) return null;
const lastRead = channel.lastRead();
function isMessageUnread(messageArrayIndex: number): boolean {
const msg = messageList?.[messageArrayIndex];
if (lastRead && msg?.created_at) {
return lastRead < msg.created_at;
}
return false;
}
const isCurrentMessageUnread = isMessageUnread(index);
const showUnreadUnderlay = isCurrentMessageUnread && scrollToBottomButtonVisible;
const insertInlineUnreadIndicator = showUnreadUnderlay && !isMessageUnread(index + 1); // show only if previous message is read
if (message.type === 'system') {
return (
<>
<View testID={`message-list-item-${index}`}>
<MessageSystem message={message} style={styles.messagePadding} />
</View>
{insertInlineUnreadIndicator && <InlineUnreadIndicator />}
</>
);
}
const wrapMessageInTheme = client.userID === message.user?.id && !!myMessageTheme;
return wrapMessageInTheme ? (
<>
{shouldApplyAndroidWorkaround &&
isMessageWithStylesReadByAndDateSeparator(message) &&
message.dateSeparator && <InlineDateSeparator date={message.dateSeparator} />}
<ThemeProvider mergedStyle={modifiedTheme}>
<View testID={`message-list-item-${index}`}>
<Message
goToMessage={goToMessage}
groupStyles={
isMessageWithStylesReadByAndDateSeparator(message) ? message.groupStyles : []
}
isTargetedMessage={targetedMessage === message.id}
lastReceivedId={lastReceivedId === message.id ? lastReceivedId : undefined}
message={message}
onThreadSelect={onThreadSelect}
showUnreadUnderlay={showUnreadUnderlay}
style={styles.messagePadding}
threadList={threadList}
/>
</View>
</ThemeProvider>
{!shouldApplyAndroidWorkaround &&
isMessageWithStylesReadByAndDateSeparator(message) &&
message.dateSeparator && <InlineDateSeparator date={message.dateSeparator} />}
{/* Adding indicator below the messages, since the list is inverted */}
{insertInlineUnreadIndicator && <InlineUnreadIndicator />}
</>
) : (
<>
<View testID={`message-list-item-${index}`}>
{shouldApplyAndroidWorkaround &&
isMessageWithStylesReadByAndDateSeparator(message) &&
message.dateSeparator && <InlineDateSeparator date={message.dateSeparator} />}
<Message
goToMessage={goToMessage}
groupStyles={
isMessageWithStylesReadByAndDateSeparator(message) ? message.groupStyles : []
}
isTargetedMessage={targetedMessage === message.id}
lastReceivedId={
lastReceivedId === message.id || message.quoted_message_id
? lastReceivedId
: undefined
}
message={message}
onThreadSelect={onThreadSelect}
showUnreadUnderlay={showUnreadUnderlay}
style={styles.messagePadding}
threadList={threadList}
/>
</View>
{!shouldApplyAndroidWorkaround &&
isMessageWithStylesReadByAndDateSeparator(message) &&
message.dateSeparator && <InlineDateSeparator date={message.dateSeparator} />}
{/* Adding indicator below the messages, since the list is inverted */}
{insertInlineUnreadIndicator && <InlineUnreadIndicator />}
</>
);
};
/**
* We are keeping full control on message pagination, and not relying on react-native for it.
* The reasons being,
* 1. FlatList doesn't support onStartReached prop
* 2. `onEndReached` function prop available on react-native, gets executed
* once per content length (and thats actually a nice optimization strategy).
* But it also means, we always need to prioritize onEndReached above our
* logic for `onStartReached`.
* 3. `onEndReachedThreshold` prop decides - at which scroll position to call `onEndReached`.
* Its a factor of content length (which is necessary for "real" infinite scroll). But on
* the other hand, it also makes calls to `onEndReached` (and this `channel.query`) way
* too early during scroll, which we don't really need. So we are going to instead
* keep some fixed offset distance, to decide when to call `loadMore` or `loadMoreRecent`.
*
* We are still gonna keep the optimization, which react-native does - only call onEndReached
* once per content length.
*/
/**
* 1. Makes a call to `loadMoreRecent` function, which queries more recent messages.
* 2. Ensures that we call `loadMoreRecent`, once per content length
* 3. If the call to `loadMore` is in progress, we wait for it to finish to make sure scroll doesn't jump.
*/
const maybeCallOnStartReached = async (limit?: number) => {
// If onStartReached has already been called for given data length, then ignore.
if (messageList?.length && onStartReachedTracker.current[messageList.length]) {
return;
}
if (messageList?.length) {
onStartReachedTracker.current[messageList.length] = true;
}
const callback = () => {
onStartReachedInPromise.current = null;
return Promise.resolve();
};
const onError = () => {
/** Release the onStartReached trigger after 2 seconds, to try again */
setTimeout(() => {
onStartReachedTracker.current = {};
}, 2000);
};
// If onEndReached is in progress, better to wait for it to finish for smooth UX
if (onEndReachedInPromise.current) {
await onEndReachedInPromise.current;
onStartReachedInPromise.current = loadMoreRecent(limit).then(callback).catch(onError);
} else {
onStartReachedInPromise.current = loadMoreRecent(limit).then(callback).catch(onError);
}
};
/**
* 1. Makes a call to `loadMore` function, which queries more older messages.
* 2. Ensures that we call `loadMore`, once per content length
* 3. If the call to `loadMoreRecent` is in progress, we wait for it to finish to make sure scroll doesn't jump.
*/
const maybeCallOnEndReached = async () => {
// If onEndReached has already been called for given messageList length, then ignore.
if (messageList?.length && onEndReachedTracker.current[messageList.length]) {
return;
}
if (messageList?.length) {
onEndReachedTracker.current[messageList.length] = true;
}
const callback = () => {
onEndReachedInPromise.current = null;
return Promise.resolve();
};
const onError = () => {
/** Release the onEndReachedTracker trigger after 2 seconds, to try again */
setTimeout(() => {
onEndReachedTracker.current = {};
}, 2000);
};
// If onStartReached is in progress, better to wait for it to finish for smooth UX
if (onStartReachedInPromise.current) {
await onStartReachedInPromise.current;
onEndReachedInPromise.current = (threadList ? loadMoreThread() : loadMore())
.then(callback)
.catch(onError);
} else {
onEndReachedInPromise.current = (threadList ? loadMoreThread() : loadMore())
.then(callback)
.catch(onError);
}
};
/**
* Following if condition covers following cases:
* 1. If I scroll up -> show scrollToBottom button.
* 2. If I scroll to bottom of screen
* |-> hide scrollToBottom button.
* |-> if channel is unread, call markRead().
*/
const handleScroll: ScrollViewProps['onScroll'] = (event) => {
if (!channel || !channelResyncScrollSet.current) {
return;
}
const offset = event.nativeEvent.contentOffset.y;
const visibleLength = event.nativeEvent.layoutMeasurement.height;
const contentLength = event.nativeEvent.contentSize.height;
// Check if scroll has reached either start of end of list.
const isScrollAtStart = offset < 100;
const isScrollAtEnd = contentLength - visibleLength - offset < 100;
if (isScrollAtStart) {
maybeCallOnStartReached();
}
if (isScrollAtEnd) {
maybeCallOnEndReached();
}
// Show scrollToBottom button once scroll position goes beyond 150.
const isScrollAtBottom = offset <= 150;
const showScrollToBottomButton = !isScrollAtBottom || !hasNoMoreRecentMessagesToLoad;
const shouldMarkRead =
!threadList && offset <= 0 && hasNoMoreRecentMessagesToLoad && channel.countUnread() > 0;
if (shouldMarkRead) {
markRead();
}
setScrollToBottomButtonVisible(showScrollToBottomButton);
if (onListScroll) {
onListScroll(event);
}
};
const goToNewMessages = async () => {
if (!hasNoMoreRecentMessagesToLoad) {
resetPaginationTrackersRef.current();
await reloadChannel();
} else if (flatListRef.current) {
flatListRef.current.scrollToOffset({
offset: 0,
});
}
setScrollToBottomButtonVisible(false);
if (!threadList) {
markRead();
}
};
const onScrollToIndexFailedRef = useRef<
FlatListProps<MessageType<StreamChatGenerics>>['onScrollToIndexFailed']
>((info) => {
// We got a failure as we tried to scroll to an item that was outside the render length
if (flatListRef.current) {
// we dont know the actual size of all items but we can see the average, so scroll to the closest offset
flatListRef.current.scrollToOffset({
animated: false,
offset: info.averageItemLength * info.index,
});
// since we used only an average offset.. we wont go to the center of the item yet
// with a little delay to wait for scroll to offset to complete, we can then scroll to the index
setTimeout(() => {
flatListRef.current?.scrollToIndex({
animated: false,
index: info.index,
viewPosition: 0.5, // try to place message in the center of the screen
});
if (messageIdLastScrolledToRef.current) {
setTargetedMessage(messageIdLastScrolledToRef.current);
}
}, 150);
}
});
const goToMessage = useCallback(
(messageId: string) => {
const indexOfParentInMessageList = messageList.findIndex(
(message) => message?.id === messageId,
);
if (indexOfParentInMessageList !== -1 && flatListRef.current) {
flatListRef.current.scrollToIndex({
animated: true,
index: indexOfParentInMessageList,
viewPosition: 0.5, // try to place message in the center of the screen
});
// keep track of this messageId, so that we dont scroll to again in useEffect for targeted message change
messageIdLastScrolledToRef.current = messageId;
setTargetedMessage(messageId);
return;
}
messageIdToScrollToRef.current = messageId; // keep track of the id to scroll afterwards
loadChannelAroundMessage({ messageId }); // now try to load the message and whats around it
resetPaginationTrackersRef.current();
},
[messageList],
);
/**
* Check if a messageId needs to be scrolled to after list loads, and scroll to it
* Note: This effect fires on every list change with a small debounce so that scrolling isnt abrupted by an immediate rerender
*/
useEffect(() => {
scrollToDebounceTimeoutRef.current = setTimeout(() => {
if (initialScrollToFirstUnreadMessage) {
initialScrollSettingTimeoutRef.current = setTimeout(() => {
// small timeout to ensure that handleScroll is called after scrollToIndex to set this flag
setInitialScrollDone(true);
}, 500);
}
// goToMessage method might have requested to scroll to a message
let messageIdToScroll: string | undefined = messageIdToScrollToRef.current;
if (targetedMessage && messageIdLastScrolledToRef.current !== targetedMessage) {
// if some messageId was targeted but not scrolledTo yet
// we have scroll to there after loading completes
messageIdToScroll = targetedMessage;
}
if (!messageIdToScroll) return;
const indexOfParentInMessageList = messageList.findIndex(
(message) => message?.id === messageIdToScroll,
);
if (indexOfParentInMessageList !== -1 && flatListRef.current) {
flatListRef.current.scrollToIndex({
animated: false,
index: indexOfParentInMessageList,
viewPosition: 0.5, // try to place message in the center of the screen
});
// reset the messageId tracker to not scroll to that again
messageIdToScrollToRef.current = undefined;
// keep track of this messageId, so that we dont scroll to again for targeted message change
messageIdLastScrolledToRef.current = messageIdToScroll;
}
}, 150);
return () => {
clearTimeout(scrollToDebounceTimeoutRef.current);
clearTimeout(initialScrollSettingTimeoutRef.current);
};
}, [targetedMessage, initialScrollToFirstUnreadMessage, messageList]);
const messagesWithImages =
legacyImageViewerSwipeBehaviour &&
messageList.filter((message) => {
const isMessageTypeDeleted = message.type === 'deleted';
if (!isMessageTypeDeleted && message.attachments) {
return message.attachments.some(
(attachment) =>
attachment.type === 'image' &&
!attachment.title_link &&
!attachment.og_scrape_url &&
(attachment.image_url || attachment.thumb_url),
);
}
return false;
});
/**
* This is for the useEffect to run again in the case that a message
* gets edited with more or the same number of images
*/
const imageString =
legacyImageViewerSwipeBehaviour &&
messagesWithImages &&
messagesWithImages
.map((message) =>
message.attachments
?.map((attachment) => attachment.image_url || attachment.thumb_url || '')
.join(),
)
.join();
const numberOfMessagesWithImages =
legacyImageViewerSwipeBehaviour && messagesWithImages && messagesWithImages.length;
const threadExists = !!thread;
useEffect(() => {
if (
legacyImageViewerSwipeBehaviour &&
isListActive &&
((threadList && thread) || (!threadList && !thread))
) {
setMessages(messagesWithImages as MessageType<StreamChatGenerics>[]);
}
}, [
imageString,
isListActive,
legacyImageViewerSwipeBehaviour,
numberOfMessagesWithImages,
threadExists,
threadList,
]);
const stickyHeaderFormatDate =
stickyHeaderDate?.getFullYear() === new Date().getFullYear() ? 'MMM D' : 'MMM D, YYYY';
const tStickyHeaderDate =
stickyHeaderDate && !hideStickyDateHeader ? tDateTimeParser(stickyHeaderDate) : null;
const stickyHeaderDateString = useMemo(() => {
if (tStickyHeaderDate === null || hideStickyDateHeader) return null;
if (isDayOrMoment(tStickyHeaderDate)) return tStickyHeaderDate.format(stickyHeaderFormatDate);
return new Date(tStickyHeaderDate).toDateString();
}, [tStickyHeaderDate, stickyHeaderFormatDate, hideStickyDateHeader]);
const dismissImagePicker = () => {
if (!hasMoved && selectedPicker) {
setSelectedPicker(undefined);
closePicker();
}
};
const onScrollBeginDrag = () => !hasMoved && selectedPicker && setHasMoved(true);
const onScrollEndDrag = () => hasMoved && selectedPicker && setHasMoved(false);
const refCallback = (ref: FlatListType<MessageType<StreamChatGenerics>>) => {
flatListRef.current = ref;
if (setFlatListRef) {
setFlatListRef(ref);
}
};
const debugRef = useDebugContext();
const isDebugModeEnabled = __DEV__ && debugRef && debugRef.current;
if (isDebugModeEnabled) {
if (debugRef.current.setEventType) debugRef.current.setEventType('send');
if (debugRef.current.setSendEventParams)
debugRef.current.setSendEventParams({
action: thread ? 'ThreadList' : 'Messages',
data: messageList,
});
}
const renderListEmptyComponent = useCallback(
() => (
<View
style={[styles.flex, shouldApplyAndroidWorkaround ? styles.invertAndroid : styles.invert]}
testID='empty-state'
>
<EmptyStateIndicator listType='message' />
</View>
),
[EmptyStateIndicator, shouldApplyAndroidWorkaround],
);
const ListFooterComponent = useCallback(
() => (
<View style={shouldApplyAndroidWorkaround ? styles.invertAndroid : undefined}>
<FooterComponent />
</View>
),
[shouldApplyAndroidWorkaround, FooterComponent],
);
const ListHeaderComponent = useCallback(
() => (
<View style={shouldApplyAndroidWorkaround ? styles.invertAndroid : undefined}>
<HeaderComponent />
</View>
),
[shouldApplyAndroidWorkaround, HeaderComponent],
);
if (!FlatList) return null;