From c34c56fd41cdad0713fe908e7ffbcc37682b539b Mon Sep 17 00:00:00 2001 From: liamcottle Date: Fri, 5 Jul 2024 23:41:30 +1200 Subject: [PATCH] auto load previous messages when scrolling up --- src/public/text-messages-embed.html | 33 +++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/src/public/text-messages-embed.html b/src/public/text-messages-embed.html index 80b1c26..7cd6b30 100644 --- a/src/public/text-messages-embed.html +++ b/src/public/text-messages-embed.html @@ -29,12 +29,10 @@
- - - -
+ +
@@ -46,6 +44,15 @@
+ + + +
@@ -64,6 +71,8 @@ isLoadingPrevious: false, isLoadingMore: false, shouldAutoScroll: true, + loadPreviousObserver: null, + hasMorePrevious: true, messages: [], nodesById: {}, @@ -94,6 +103,14 @@ }); + // setup intersection observer + this.loadPreviousObserver = new IntersectionObserver((entries) => { + const loadMoreElement = entries[0]; + if(loadMoreElement && loadMoreElement.isIntersecting){ + this.loadPrevious(); + } + }); + this.initialLoad(); }, @@ -106,6 +123,9 @@ // scroll to bottom this.scrollToBottom(); + // setup auto loading previous + this.loadPreviousObserver.observe(document.querySelector("#load-previous")); + // load more every few seconds setInterval(async () => { await this.loadMore(); @@ -141,6 +161,11 @@ this.messages.unshift(message); } + // no more previous to load if previous list is empty + if(messages.length === 0){ + this.hasMorePrevious = false; + } + // fetch node info for(const message of messages){ await this.fetchNodeInfo(message.to);