Skip to content

Commit b67a456

Browse files
oliverlazpetyosi
andauthored
fix: edge case of prepend count not being reset when jumping (#1765) (#1768)
* fix: edge case of prepend count not being reset when jumping * fix: reset previously prepended items value Co-authored-by: Petyo Ivanov <[email protected]> (cherry picked from commit 18ba8f8)
1 parent bdf8c32 commit b67a456

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/components/MessageList/hooks/usePrependMessagesCount.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,17 @@ export function usePrependedMessagesCount<
88
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics
99
>(messages: StreamMessage<StreamChatGenerics>[], hasDateSeparator: boolean) {
1010
const firstRealMessageIndex = hasDateSeparator ? 1 : 0;
11-
const currentFirstMessageId = messages?.[firstRealMessageIndex]?.id;
12-
const firstMessageId = useRef(currentFirstMessageId);
13-
const earliestMessageId = useRef(currentFirstMessageId);
11+
const firstMessageId = useRef<string>();
12+
const earliestMessageId = useRef<string>();
1413
const previousNumItemsPrepended = useRef(0);
1514

1615
const numItemsPrepended = useMemo(() => {
1716
if (!messages || !messages.length) {
17+
previousNumItemsPrepended.current = 0;
1818
return 0;
1919
}
20+
21+
const currentFirstMessageId = messages?.[firstRealMessageIndex]?.id;
2022
// if no new messages were prepended, return early (same amount as before)
2123
if (currentFirstMessageId === earliestMessageId.current) {
2224
return previousNumItemsPrepended.current;
@@ -35,9 +37,9 @@ export function usePrependedMessagesCount<
3537
}
3638
}
3739

38-
// if no match has found, we have jumped
40+
// if no match has found, we have jumped - reset the prepend item count.
3941
firstMessageId.current = currentFirstMessageId;
40-
42+
previousNumItemsPrepended.current = 0;
4143
return 0;
4244
// TODO: there's a bug here, the messages prop is the same array instance (something mutates it)
4345
// that's why the second dependency is necessary

0 commit comments

Comments
 (0)