From d7b64ff44779fb8168a3ec27f37f1aa33917653d Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Tue, 24 Sep 2024 00:27:22 +0200 Subject: [PATCH] enh: infinite scroll messages --- src/lib/components/chat/Messages.svelte | 33 ++++++++++++++++++++++--- src/lib/components/common/Loader.svelte | 22 ++++++++++++++--- 2 files changed, 49 insertions(+), 6 deletions(-) diff --git a/src/lib/components/chat/Messages.svelte b/src/lib/components/chat/Messages.svelte index d342abf7f..d6d2b881a 100644 --- a/src/lib/components/chat/Messages.svelte +++ b/src/lib/components/chat/Messages.svelte @@ -9,6 +9,8 @@ import Placeholder from './Messages/Placeholder.svelte'; import Message from './Messages/Message.svelte'; + import Loader from '../common/Loader.svelte'; + import Spinner from '../common/Spinner.svelte'; const i18n = getContext('i18n'); @@ -33,12 +35,21 @@ export let bottomPadding = false; export let autoScroll; + let messagesCount = 20; + let messagesLoading = false; + + const loadMoreMessages = async () => { + messagesLoading = true; + messagesCount += 20; + messagesLoading = false; + }; + $: if (history.currentId) { let _messages = []; let message = history.messages[history.currentId]; - while (message) { - _messages.unshift({ ...message }); + while (message && _messages.length <= messagesCount) { + _messages.push({ ...message }); message = message.parentId !== null ? history.messages[message.parentId] : null; } @@ -330,7 +341,7 @@ {:else}
{#key chatId} -
+
{#each messages as message, messageIdx (message.id)} {/each} + + {#if messages.at(-1).parentId !== null} + { + console.log('visible'); + if (!messagesLoading) { + loadMoreMessages(); + } + }} + > +
+ +
Loading...
+
+
+ {/if}
{#if bottomPadding} diff --git a/src/lib/components/common/Loader.svelte b/src/lib/components/common/Loader.svelte index 86c415ae4..ac7ecaf28 100644 --- a/src/lib/components/common/Loader.svelte +++ b/src/lib/components/common/Loader.svelte @@ -1,16 +1,24 @@