Skip to content

Build your personal knowledge base with Trilium Notes

refactor(llm): extract chat message list into separate component

Elian Doranee4619e3ae70
diff --git a/apps/client/src/widgets/sidebar/SidebarChat.css b/apps/client/src/widgets/sidebar/SidebarChat.css
index a05281ee41..dcaa3af726 100644
--- a/apps/client/src/widgets/sidebar/SidebarChat.css
+++ b/apps/client/src/widgets/sidebar/SidebarChat.css
@@ -18,10 +18,8 @@
}
}
+/* Scroll-container essentials (flex/min-height/overflow) come from .chat-message-list. */
.sidebar-chat-messages {
- flex: 1;
- min-height: 0; /* Allow flex shrinking for scroll containment */
- overflow-y: auto;
padding: 0.5rem;
display: flex;
flex-direction: column;
diff --git a/apps/client/src/widgets/sidebar/SidebarChat.tsx b/apps/client/src/widgets/sidebar/SidebarChat.tsx
index 96f606bf83..8f16b24133 100644
--- a/apps/client/src/widgets/sidebar/SidebarChat.tsx
+++ b/apps/client/src/widgets/sidebar/SidebarChat.tsx
@@ -11,9 +11,8 @@ import ActionButton from "../react/ActionButton.js";
import Dropdown from "../react/Dropdown.js";
import { FormListItem } from "../react/FormList.js";
import { useActiveNoteContext, useNote, useNoteProperty, useSpacedUpdate } from "../react/hooks.js";
-import NoItems from "../react/NoItems.js";
import ChatInputBar from "../type_widgets/llm_chat/ChatInputBar.js";
-import ChatMessage from "../type_widgets/llm_chat/ChatMessage.js";
+import ChatMessageList from "../type_widgets/llm_chat/ChatMessageList.js";
import type { LlmChatContent } from "../type_widgets/llm_chat/llm_chat_types.js";
import { useLlmChat } from "../type_widgets/llm_chat/useLlmChat.js";
import RightPanelWidget from "./RightPanelWidget.js";
@@ -279,50 +278,11 @@ export default function SidebarChat() {
}
>
<div className="sidebar-chat-container">
- <div className="sidebar-chat-messages" ref={chat.scrollContainerRef}>
- {chat.messages.length === 0 && !chat.isStreaming && (
- <NoItems
- icon="bx bx-conversation"
- text={t("sidebar_chat.empty_state")}
- />
- )}
- {chat.messages.map((msg, idx) => (
- <ChatMessage
- key={msg.id}
- message={msg}
- onRetry={
- idx === chat.messages.length - 1 && msg.type === "error" && !chat.isStreaming
- ? chat.retryLast
- : undefined
- }
- />
- ))}
- {chat.isStreaming && chat.streamingThinking && (
- <ChatMessage
- message={{
- id: "streaming-thinking",
- role: "assistant",
- content: chat.streamingThinking,
- createdAt: new Date().toISOString(),
- type: "thinking"
- }}
- isStreaming
- />
- )}
- {chat.isStreaming && chat.streamingBlocks.length > 0 && (
- <ChatMessage
- message={{
- id: "streaming",
- role: "assistant",
- content: chat.streamingBlocks,
- createdAt: new Date().toISOString(),
- citations: chat.pendingCitations.length > 0 ? chat.pendingCitations : undefined
- }}
- isStreaming
- />
- )}
- <div ref={chat.messagesEndRef} />
- </div>
+ <ChatMessageList
+ chat={chat}
+ className="sidebar-chat-messages"
+ emptyStateText={t("sidebar_chat.empty_state")}
+ />
<ChatInputBar
chat={chat}
rows={2}
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ChatMessageList.css b/apps/client/src/widgets/type_widgets/llm_chat/ChatMessageList.css
new file mode 100644
index 0000000000..a3cb1e9288
--- /dev/null
+++ b/apps/client/src/widgets/type_widgets/llm_chat/ChatMessageList.css
@@ -0,0 +1,8 @@
+/* Shared scroll container for the chat message list (used by both the chat note
+ type widget and the sidebar chat). Widget-specific padding/layout lives in the
+ respective widget CSS, keyed off the extra class passed via `className`. */
+.chat-message-list {
+ flex: 1;
+ min-height: 0;
+ overflow-y: auto;
+}
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ChatMessageList.tsx b/apps/client/src/widgets/type_widgets/llm_chat/ChatMessageList.tsx
new file mode 100644
index 0000000000..f3d0307053
--- /dev/null
+++ b/apps/client/src/widgets/type_widgets/llm_chat/ChatMessageList.tsx
@@ -0,0 +1,65 @@
+import "./ChatMessageList.css";
+
+import NoItems from "../../react/NoItems.js";
+import ChatMessage from "./ChatMessage.js";
+import type { UseLlmChatReturn } from "./useLlmChat.js";
+
+interface ChatMessageListProps {
+ /** The chat hook result. */
+ chat: UseLlmChatReturn;
+ /** Placeholder text shown when there are no messages yet. */
+ emptyStateText: string;
+ /** Extra class on the scroll container for widget-specific styling. */
+ className?: string;
+}
+
+/**
+ * Renders the scrollable chat message timeline: stored messages (with retry
+ * wiring on a trailing error), in-progress streaming placeholders and the
+ * scroll anchor. Shared by the chat note type widget and the sidebar chat.
+ */
+export default function ChatMessageList({ chat, emptyStateText, className }: ChatMessageListProps) {
+ return (
+ <div className={`chat-message-list ${className ?? ""}`} ref={chat.scrollContainerRef}>
+ {chat.messages.length === 0 && !chat.isStreaming && (
+ <NoItems icon="bx bx-conversation" text={emptyStateText} />
+ )}
+ {chat.messages.map((msg, idx) => (
+ <ChatMessage
+ key={msg.id}
+ message={msg}
+ onRetry={
+ idx === chat.messages.length - 1 && msg.type === "error" && !chat.isStreaming
+ ? chat.retryLast
+ : undefined
+ }
+ />
+ ))}
+ {chat.isStreaming && chat.streamingThinking && (
+ <ChatMessage
+ message={{
+ id: "streaming-thinking",
+ role: "assistant",
+ content: chat.streamingThinking,
+ createdAt: new Date().toISOString(),
+ type: "thinking"
+ }}
+ isStreaming
+ />
+ )}
+ {chat.isStreaming && chat.streamingBlocks.length > 0 && (
+ <ChatMessage
+ message={{
+ id: "streaming",
+ role: "assistant",
+ content: chat.streamingBlocks,
+ createdAt: new Date().toISOString(),
+ citations: chat.pendingCitations.length > 0 ? chat.pendingCitations : undefined
+ }}
+ isStreaming
+ />
+ )}
+ <div ref={chat.messagesEndRef} />
+ </div>
+ );
+}
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/LlmChat.css b/apps/client/src/widgets/type_widgets/llm_chat/LlmChat.css
index 671818d934..b429ada78d 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/LlmChat.css
+++ b/apps/client/src/widgets/type_widgets/llm_chat/LlmChat.css
@@ -12,9 +12,3 @@
padding: 0.5em 1em;
}
}
-
-.llm-chat-messages {
- flex: 1;
- overflow-y: auto;
- padding-bottom: 1rem;
-}
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/LlmChat.tsx b/apps/client/src/widgets/type_widgets/llm_chat/LlmChat.tsx
index bde73bc09a..14423ee7a0 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/LlmChat.tsx
+++ b/apps/client/src/widgets/type_widgets/llm_chat/LlmChat.tsx
@@ -4,10 +4,9 @@ import { useCallback, useEffect, useRef } from "preact/hooks";
import { t } from "../../../services/i18n.js";
import { useEditorSpacedUpdate } from "../../react/hooks.js";
-import NoItems from "../../react/NoItems.js";
import { TypeWidgetProps } from "../type_widget.js";
import ChatInputBar from "./ChatInputBar.js";
-import ChatMessage from "./ChatMessage.js";
+import ChatMessageList from "./ChatMessageList.js";
import type { LlmChatContent } from "./llm_chat_types.js";
import { useLlmChat } from "./useLlmChat.js";
@@ -55,50 +54,11 @@ export default function LlmChat({ note, ntxId, noteContext }: TypeWidgetProps) {
return (
<div className="llm-chat-container">
- <div className="llm-chat-messages" ref={chat.scrollContainerRef}>
- {chat.messages.length === 0 && !chat.isStreaming && (
- <NoItems
- icon="bx bx-conversation"
- text={t("llm_chat.empty_state")}
- />
- )}
- {chat.messages.map((msg, idx) => (
- <ChatMessage
- key={msg.id}
- message={msg}
- onRetry={
- idx === chat.messages.length - 1 && msg.type === "error" && !chat.isStreaming
- ? chat.retryLast
- : undefined
- }
- />
- ))}
- {chat.isStreaming && chat.streamingThinking && (
- <ChatMessage
- message={{
- id: "streaming-thinking",
- role: "assistant",
- content: chat.streamingThinking,
- createdAt: new Date().toISOString(),
- type: "thinking"
- }}
- isStreaming
- />
- )}
- {chat.isStreaming && chat.streamingBlocks.length > 0 && (
- <ChatMessage
- message={{
- id: "streaming",
- role: "assistant",
- content: chat.streamingBlocks,
- createdAt: new Date().toISOString(),
- citations: chat.pendingCitations.length > 0 ? chat.pendingCitations : undefined
- }}
- isStreaming
- />
- )}
- <div ref={chat.messagesEndRef} />
- </div>
+ <ChatMessageList
+ chat={chat}
+ className="llm-chat-messages"
+ emptyStateText={t("llm_chat.empty_state")}
+ />
<ChatInputBar
chat={chat}
onWebSearchChange={triggerSave}