From fb35417fe226cd82edeb6d09809aa433bf808089 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Mon, 7 Sep 2020 18:42:59 -0500 Subject: [PATCH] Chats: prevent repeat API calls --- .../chats/components/chat_message_list.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/app/soapbox/features/chats/components/chat_message_list.js b/app/soapbox/features/chats/components/chat_message_list.js index 826f23b6b..48937518d 100644 --- a/app/soapbox/features/chats/components/chat_message_list.js +++ b/app/soapbox/features/chats/components/chat_message_list.js @@ -102,6 +102,11 @@ class ChatMessageList extends ImmutablePureComponent { return scrollHeight - scrollTop; } + restoreScrollPosition = (scrollBottom) => { + this.lastComputedScroll = this.node.scrollHeight - scrollBottom; + this.node.scrollTop = this.lastComputedScroll; + } + componentDidUpdate(prevProps, prevState, scrollBottom) { const { initialLoad } = this.state; const oldCount = prevProps.chatMessages.count(); @@ -110,7 +115,7 @@ class ChatMessageList extends ImmutablePureComponent { const historyAdded = prevProps.chatMessages.getIn([0, 'id']) !== this.props.chatMessages.getIn([0, 'id']); // Retain scroll bar position when loading old messages - this.node.scrollTop = this.node.scrollHeight - scrollBottom; + this.restoreScrollPosition(scrollBottom); if (oldCount !== newCount) { if (isNearBottom || initialLoad) this.scrollToBottom(); @@ -130,7 +135,13 @@ class ChatMessageList extends ImmutablePureComponent { } handleScroll = throttle(() => { - if (this.node.scrollTop < this.node.offsetHeight * 2 && !this.state.isLoading) + const { lastComputedScroll } = this; + const { isLoading, initialLoad } = this.state; + const { scrollTop, offsetHeight } = this.node; + const computedScroll = lastComputedScroll === scrollTop; + const nearTop = scrollTop < offsetHeight * 2; + + if (nearTop && !isLoading && !initialLoad && !computedScroll) this.handleLoadMore(); }, 150, { trailing: true,