Skip to content

Commit 2f94dc8

Browse files
committed
perf: try to improve performance
1 parent 1fc7782 commit 2f94dc8

File tree

1 file changed

+48
-1
lines changed

1 file changed

+48
-1
lines changed

package/src/components/MessageList/MessageFlashList.tsx

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,43 @@ type MessageListFlashListPropsWithContext = Pick<
183183

184184
const WAIT_FOR_SCROLL_TIMEOUT = 200;
185185

186+
const getItemTypeInternal = (message: LocalMessage) => {
187+
if (message.type === 'regular') {
188+
if ((message.attachments?.length ?? 0) > 0) {
189+
return 'message-with-attachments';
190+
}
191+
192+
if (message.poll_id) {
193+
return 'message-with-poll';
194+
}
195+
196+
if (message.quoted_message_id) {
197+
return 'message-with-quote';
198+
}
199+
200+
if (message.shared_location) {
201+
return 'message-with-shared-location';
202+
}
203+
204+
if (message.text) {
205+
const text = message.text;
206+
if (text.length <= 20) {
207+
return 'short-message-with-text';
208+
}
209+
210+
if (text.length <= 100) {
211+
return 'medium-message-with-text';
212+
}
213+
214+
return 'message-with-text';
215+
}
216+
217+
return 'message-with-nothing';
218+
}
219+
220+
return 'unresolvable-type';
221+
};
222+
186223
const MessageListFlashListWithContext = (props: MessageListFlashListPropsWithContext) => {
187224
const LoadingMoreRecentIndicator = props.threadList
188225
? InlineLoadingMoreRecentThreadIndicator
@@ -310,6 +347,7 @@ const MessageListFlashListWithContext = (props: MessageListFlashListPropsWithCon
310347

311348
const maintainVisibleContentPosition = useMemo(() => {
312349
return {
350+
animateAutoscrollToBottom: true,
313351
autoscrollToBottomThreshold: autoScrollToRecent || threadList ? 10 : undefined,
314352
startRenderingFromBottom: true,
315353
};
@@ -460,7 +498,9 @@ const MessageListFlashListWithContext = (props: MessageListFlashListPropsWithCon
460498
// we should scroll to bottom where ever we are now
461499
// as we have sent a new own message
462500
if (shouldScrollToRecentOnNewOwnMessage) {
463-
setAutoScrollToRecent(true);
501+
flashListRef.current?.scrollToEnd({
502+
animated: true,
503+
});
464504
}
465505
}
466506
}, [channel, processedMessageList, shouldScrollToRecentOnNewOwnMessageRef, threadList]);
@@ -1021,6 +1061,11 @@ const MessageListFlashListWithContext = (props: MessageListFlashListPropsWithCon
10211061
[contentContainer],
10221062
);
10231063

1064+
const getItemType = useStableCallback((item: LocalMessage) => {
1065+
const type = getItemTypeInternal(item);
1066+
return client.userID === item.user?.id ? `own-${type}` : type;
1067+
});
1068+
10241069
if (loading) {
10251070
return (
10261071
<View style={[styles.container, { backgroundColor: white_snow }, container]}>
@@ -1042,6 +1087,8 @@ const MessageListFlashListWithContext = (props: MessageListFlashListPropsWithCon
10421087
<FlashList
10431088
contentContainerStyle={flatListContentContainerStyle}
10441089
data={processedMessageList}
1090+
drawDistance={1500}
1091+
getItemType={getItemType}
10451092
keyboardShouldPersistTaps='handled'
10461093
keyExtractor={keyExtractor}
10471094
ListFooterComponent={FooterComponent}

0 commit comments

Comments
 (0)