From 5cabb2aeb38dd567de3d2cbdddca115e7500dc27 Mon Sep 17 00:00:00 2001 From: Mike Thicke Date: Thu, 8 May 2025 12:37:34 -0400 Subject: [PATCH] Autoscroll ChatHistory when streaming in responses. --- src/ChatView.tsx | 11 ++++++++--- src/components/ChatHistory.tsx | 23 +++++++++++++++++++++-- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/src/ChatView.tsx b/src/ChatView.tsx index 5e7c51d..e6e62de 100644 --- a/src/ChatView.tsx +++ b/src/ChatView.tsx @@ -33,7 +33,7 @@ export class ChatView extends ItemView { } getDisplayText(): string { - return "Co-Intelligence Chat"; + return this.file?.path || "Co-Intelligence Chat"; } async handleChatChange( @@ -66,7 +66,10 @@ export class ChatView extends ItemView { this.leaf.detach(); return; } - const { messages, linkedNotes } = await deserializeCoiNote(this.file, this.app); + const { messages, linkedNotes } = await deserializeCoiNote( + this.file, + this.app, + ); const rootElement = this.containerEl.children[1]; render( () => ( @@ -74,7 +77,9 @@ export class ChatView extends ItemView { app={this.app} plugin={this.plugin} file={this.file as TFile} - onChange={(newMessages, newTitle) => this.handleChatChange(newMessages, newTitle, linkedNotes)} + onChange={(newMessages, newTitle) => + this.handleChatChange(newMessages, newTitle, linkedNotes) + } initialMessages={messages} initialLinkedNotes={linkedNotes} /> diff --git a/src/components/ChatHistory.tsx b/src/components/ChatHistory.tsx index 04bb7e8..a310af0 100644 --- a/src/components/ChatHistory.tsx +++ b/src/components/ChatHistory.tsx @@ -1,4 +1,4 @@ -import { Component, Accessor } from "solid-js"; +import { Component, Accessor, createEffect, onMount } from "solid-js"; import { CoreMessage } from "ai"; import { BotMessage } from "@/components/BotMessage"; @@ -9,8 +9,27 @@ export interface ChatHistoryProps { } export const ChatHistory: Component = ({ messages }) => { + let chatContainerRef: HTMLDivElement | undefined; + + const scrollToBottom = () => { + if (chatContainerRef) { + chatContainerRef.scrollTop = chatContainerRef.scrollHeight; + } + }; + + // Scroll to bottom whenever messages change + createEffect(() => { + // Access messages to create dependency + messages(); + // Use setTimeout to ensure DOM has updated before scrolling + setTimeout(scrollToBottom, 0); + }); + + // Initial scroll on mount + onMount(scrollToBottom); + return ( -
+
{messages().map((message) => { if (message.role === "assistant") { return ;