Skip to content

Commit 9ca5148

Browse files
committed
fix: properly calculate minIndexForVisible
1 parent 241d1db commit 9ca5148

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

package/src/components/MessageList/MessageList.tsx

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -335,14 +335,20 @@ const MessageListWithContext = (props: MessageListPropsWithContext) => {
335335

336336
const [autoscrollToRecent, setAutoscrollToRecent] = useState(false);
337337

338+
const minIndexForVisible = Math.min(1, processedMessageList.length);
339+
338340
const maintainVisibleContentPosition = useMemo(
339341
() => ({
340342
autoscrollToTopThreshold: autoscrollToRecent ? 10 : undefined,
341-
minIndexForVisible: 1,
343+
minIndexForVisible,
342344
}),
343-
[autoscrollToRecent],
345+
[autoscrollToRecent, minIndexForVisible],
344346
);
345347

348+
useEffect(() => {
349+
console.log('CHANGED: ', maintainVisibleContentPosition);
350+
}, [maintainVisibleContentPosition]);
351+
346352
/**
347353
* We want to call onEndReached and onStartReached only once, per content length.
348354
* We keep track of calls to these functions per content length, with following trackers.
@@ -620,7 +626,7 @@ const MessageListWithContext = (props: MessageListPropsWithContext) => {
620626
}, [threadList, messageListLengthAfterUpdate, topMessageAfterUpdate?.id]);
621627

622628
useEffect(() => {
623-
if (!rawMessageList.length) {
629+
if (!processedMessageList.length) {
624630
return;
625631
}
626632
if (threadList) {
@@ -637,7 +643,7 @@ const MessageListWithContext = (props: MessageListPropsWithContext) => {
637643
}
638644
const latestNonCurrentMessageBeforeUpdate = latestNonCurrentMessageBeforeUpdateRef.current;
639645
latestNonCurrentMessageBeforeUpdateRef.current = undefined;
640-
const latestCurrentMessageAfterUpdate = rawMessageList[rawMessageList.length - 1];
646+
const latestCurrentMessageAfterUpdate = processedMessageList[0];
641647
if (!latestCurrentMessageAfterUpdate) {
642648
setAutoscrollToRecent(true);
643649
return;
@@ -662,7 +668,7 @@ const MessageListWithContext = (props: MessageListPropsWithContext) => {
662668
}
663669
}
664670
// eslint-disable-next-line react-hooks/exhaustive-deps
665-
}, [channel, rawMessageList, threadList]);
671+
}, [channel, processedMessageList, threadList]);
666672

667673
const goToMessage = useStableCallback(async (messageId: string) => {
668674
const indexOfParentInMessageList = processedMessageList.findIndex(

0 commit comments

Comments
 (0)