Skip to content

Commit 23a1cad

Browse files
Small fix for loading channel at very first messages
1 parent 38788a3 commit 23a1cad

File tree

3 files changed

+21
-23
lines changed

3 files changed

+21
-23
lines changed

src/components/Channel/Channel.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,7 +1296,7 @@ const ChannelWithContext = <
12961296
Me,
12971297
Re,
12981298
Us
1299-
>['loadMore'] = async () => {
1299+
>['loadMore'] = async (limit = 20) => {
13001300
if (loadingMore || hasMore === false) {
13011301
return;
13021302
}
@@ -1313,7 +1313,6 @@ const ChannelWithContext = <
13131313
}
13141314

13151315
const oldestID = oldestMessage && oldestMessage.id;
1316-
const limit = 20;
13171316

13181317
try {
13191318
if (channel) {
@@ -1340,7 +1339,7 @@ const ChannelWithContext = <
13401339
Me,
13411340
Re,
13421341
Us
1343-
>['loadMoreRecent'] = async () => {
1342+
>['loadMoreRecent'] = async (limit = 5) => {
13441343
if (channel?.state.isUpToDate) {
13451344
return;
13461345
}
@@ -1356,7 +1355,7 @@ const ChannelWithContext = <
13561355

13571356
try {
13581357
if (channel) {
1359-
await queryAfterMessage(recentMessage.id);
1358+
await queryAfterMessage(recentMessage.id, limit);
13601359
loadMoreRecentFinished(channel.state.messages);
13611360
}
13621361
} catch (err) {

src/components/MessageList/MessageList.tsx

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import React, {
88
import {
99
FlatListProps,
1010
FlatList as FlatListType,
11-
Platform,
1211
ScrollViewProps,
1312
StyleSheet,
1413
View,
@@ -425,19 +424,6 @@ const MessageListWithContext = <
425424
setTimeout(() => {
426425
initialScrollSet.current = true;
427426
}, 500);
428-
} else if (
429-
// On android, you can't overscroll. So we are going to scroll up just a little
430-
// which gives user some offset to scroll down and trigger onStartReached.
431-
Platform.OS === 'android' &&
432-
!channel?.state.isUpToDate &&
433-
flatListRef.current
434-
) {
435-
flatListRef.current.scrollToOffset({
436-
offset: 50,
437-
});
438-
setTimeout(() => {
439-
initialScrollSet.current = true;
440-
}, 500);
441427
} else if (!initialScrollSet.current) {
442428
initialScrollSet.current = true;
443429
}
@@ -547,6 +533,19 @@ const MessageListWithContext = <
547533
setScrollToBottomButtonVisible(true);
548534
}
549535

536+
if (
537+
!channel?.state.isUpToDate &&
538+
flatListRef.current &&
539+
messageListLengthBeforeUpdate.current === 0 &&
540+
messageListLengthAfterUpdate < 10
541+
) {
542+
/**
543+
* Trigger onStartReached on first load, if messages are not enough to fill the screen.
544+
* This is important especially for android, where you can't overscroll.
545+
*/
546+
maybeCallOnStartReached(10);
547+
}
548+
550549
/**
551550
* channelLastRead and topMessage are only used for setting initial scroll.
552551
* So lets not set it if `initialScrollToFirstUnreadMessage` prop is false.
@@ -679,7 +678,7 @@ const MessageListWithContext = <
679678
* 2. Ensures that we call `loadMoreRecent`, once per content length
680679
* 3. If the call to `loadMore` is in progress, we wait for it to finish to make sure scroll doesn't jump.
681680
*/
682-
const maybeCallOnStartReached = () => {
681+
const maybeCallOnStartReached = (limit?: number) => {
683682
// If onStartReached has already been called for given data length, then ignore.
684683
if (
685684
messageList?.length &&
@@ -708,12 +707,12 @@ const MessageListWithContext = <
708707
// If onEndReached is in progress, better to wait for it to finish for smooth UX
709708
if (onEndReachedInPromise.current) {
710709
onEndReachedInPromise.current.finally(() => {
711-
onStartReachedInPromise.current = loadMoreRecent()
710+
onStartReachedInPromise.current = loadMoreRecent(limit)
712711
.then(callback)
713712
.catch(onError);
714713
});
715714
} else {
716-
onStartReachedInPromise.current = loadMoreRecent()
715+
onStartReachedInPromise.current = loadMoreRecent(limit)
717716
.then(callback)
718717
.catch(onError);
719718
}

src/contexts/paginatedMessageListContext/PaginatedMessageListContext.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ export type PaginatedMessageListContextValue<
3939
/**
4040
* Load more messages
4141
*/
42-
loadMore: () => Promise<void>;
42+
loadMore: (limit: number) => Promise<void>;
4343
/**
4444
* Load more recent messages
4545
*/
46-
loadMoreRecent: () => Promise<void>;
46+
loadMoreRecent: (limit: number) => Promise<void>;
4747
/**
4848
* Messages from client state
4949
*/

0 commit comments

Comments
 (0)