Skip to content

Commit 5a8c608

Browse files
committed
Merge branch 'feat/flashlist-implementation' of github.com:GetStream/stream-chat-react-native into feat/flashlist-implementation
2 parents a5aef62 + 837bb16 commit 5a8c608

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

package/src/components/MessageList/MessageFlashList.tsx

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';
2-
import { ScrollViewProps, StyleSheet, View, ViewabilityConfig, ViewToken } from 'react-native';
2+
import {
3+
LayoutChangeEvent,
4+
ScrollViewProps,
5+
StyleSheet,
6+
View,
7+
ViewabilityConfig,
8+
ViewToken,
9+
} from 'react-native';
310

411
import { FlashList, FlashListProps, FlashListRef } from '@shopify/flash-list';
512
import type { Channel, Event, LocalMessage, MessageResponse } from 'stream-chat';
@@ -957,10 +964,13 @@ const MessageListFlashListWithContext = (props: MessageListFlashListPropsWithCon
957964
onEndReachedTracker.current = {};
958965
});
959966

967+
const currentScrollOffsetRef = useRef(0);
968+
960969
const handleScroll: ScrollViewProps['onScroll'] = useStableCallback((event) => {
961970
const messageListHasMessages = processedMessageList.length > 0;
962971
const nativeEvent = event.nativeEvent;
963972
const offset = nativeEvent.contentOffset.y;
973+
currentScrollOffsetRef.current = offset;
964974
const visibleLength = nativeEvent.layoutMeasurement.height;
965975
const contentLength = nativeEvent.contentSize.height;
966976

@@ -1066,6 +1076,22 @@ const MessageListFlashListWithContext = (props: MessageListFlashListPropsWithCon
10661076
return client.userID === item.user?.id ? `own-${type}` : type;
10671077
});
10681078

1079+
const currentListHeightRef = useRef<number | undefined>(undefined);
1080+
1081+
const onLayout = useStableCallback((e: LayoutChangeEvent) => {
1082+
const { height } = e.nativeEvent.layout;
1083+
if (!currentListHeightRef.current) {
1084+
currentListHeightRef.current = height;
1085+
return;
1086+
}
1087+
1088+
const changedBy = currentListHeightRef.current - height;
1089+
flashListRef.current?.scrollToOffset({
1090+
offset: currentScrollOffsetRef.current + changedBy,
1091+
});
1092+
currentListHeightRef.current = height;
1093+
});
1094+
10691095
if (loading) {
10701096
return (
10711097
<View style={[styles.container, { backgroundColor: white_snow }, container]}>
@@ -1076,6 +1102,7 @@ const MessageListFlashListWithContext = (props: MessageListFlashListPropsWithCon
10761102

10771103
return (
10781104
<View
1105+
onLayout={onLayout}
10791106
style={[styles.container, { backgroundColor: white_snow }, container]}
10801107
testID='message-flat-list-wrapper'
10811108
>
@@ -1087,7 +1114,7 @@ const MessageListFlashListWithContext = (props: MessageListFlashListPropsWithCon
10871114
<FlashList
10881115
contentContainerStyle={flatListContentContainerStyle}
10891116
data={processedMessageList}
1090-
drawDistance={1500}
1117+
drawDistance={800}
10911118
getItemType={getItemType}
10921119
keyboardShouldPersistTaps='handled'
10931120
keyExtractor={keyExtractor}

0 commit comments

Comments
 (0)