From b01e49bb3c1af8aaabe94b802950b2d47ae63f22 Mon Sep 17 00:00:00 2001 From: Sean Hatfield Date: Mon, 21 Aug 2023 15:46:31 -0700 Subject: [PATCH] Autoscroll to end of chat fix (#201) bug fix for autoscrolling on message send/recieve --- .../ChatHistory/HistoricalMessage/index.jsx | 88 +++++++-------- .../ChatHistory/PromptReply/index.jsx | 106 ++++++++---------- .../ChatContainer/ChatHistory/index.jsx | 74 ++++++------ 3 files changed, 124 insertions(+), 144 deletions(-) diff --git a/frontend/src/components/WorkspaceChat/ChatContainer/ChatHistory/HistoricalMessage/index.jsx b/frontend/src/components/WorkspaceChat/ChatContainer/ChatHistory/HistoricalMessage/index.jsx index 4acda9b8..5869a81f 100644 --- a/frontend/src/components/WorkspaceChat/ChatContainer/ChatHistory/HistoricalMessage/index.jsx +++ b/frontend/src/components/WorkspaceChat/ChatContainer/ChatHistory/HistoricalMessage/index.jsx @@ -1,66 +1,56 @@ -import { useEffect, useRef, memo } from "react"; +import { memo, forwardRef } from "react"; import { AlertTriangle } from "react-feather"; import Jazzicon from "../../../../UserIcon"; import renderMarkdown from "../../../../../utils/chat/markdown"; import { userFromStorage } from "../../../../../utils/request"; import Citations from "../Citation"; -function HistoricalMessage({ - message, - role, - workspace, - sources = [], - error = false, -}) { - const replyRef = useRef(null); - useEffect(() => { - if (replyRef.current) - replyRef.current.scrollIntoView({ behavior: "smooth", block: "end" }); - }, [replyRef.current]); - - if (role === "user") { - return ( -
-
- - {message} - +const HistoricalMessage = forwardRef( + ({ message, role, workspace, sources = [], error = false }, ref) => { + if (role === "user") { + return ( +
+
+ + {message} + +
+
- -
- ); - } + ); + } + + if (error) { + return ( +
+ +
+ + Could not + respond to message. + +
+
+ ); + } - if (error) { return ( -
+
-
+
- Could not - respond to message. - + className="no-scroll whitespace-pre-line text-slate-800 dark:text-slate-200 font-[500] md:font-semibold text-sm md:text-base flex flex-col gap-y-1" + dangerouslySetInnerHTML={{ __html: renderMarkdown(message) }} + /> +
); } - - return ( -
- -
- - -
-
- ); -} +); export default memo(HistoricalMessage); diff --git a/frontend/src/components/WorkspaceChat/ChatContainer/ChatHistory/PromptReply/index.jsx b/frontend/src/components/WorkspaceChat/ChatContainer/ChatHistory/PromptReply/index.jsx index a82c25a4..3ef308e2 100644 --- a/frontend/src/components/WorkspaceChat/ChatContainer/ChatHistory/PromptReply/index.jsx +++ b/frontend/src/components/WorkspaceChat/ChatContainer/ChatHistory/PromptReply/index.jsx @@ -1,71 +1,61 @@ -import { memo, useEffect, useRef } from "react"; +import { forwardRef, memo } from "react"; import { AlertTriangle } from "react-feather"; import Jazzicon from "../../../../UserIcon"; import renderMarkdown from "../../../../../utils/chat/markdown"; import Citations from "../Citation"; -function PromptReply({ - uuid, - reply, - pending, - error, - workspace, - sources = [], - closed = true, -}) { - const replyRef = useRef(null); - useEffect(() => { - if (replyRef.current) - replyRef.current.scrollIntoView({ behavior: "smooth", block: "end" }); - }, [replyRef.current]); - - if (!reply && !sources.length === 0 && !pending && !error) return null; - if (pending) { - return ( -
- -
- -
-
-
-
- ); - } - - if (error) { - return ( -
- -
-
- - Could not - respond to message. +const PromptReply = forwardRef( + ( + { uuid, reply, pending, error, workspace, sources = [], closed = true }, + ref + ) => { + if (!reply && !sources.length === 0 && !pending && !error) return null; + if (pending) { + return ( +
+ +
+ +
- Reason: {error || "unknown"}
+ ); + } + + if (error) { + return ( +
+ +
+
+ + Could + not respond to message. + + Reason: {error || "unknown"} +
+
+
+ ); + } + + return ( +
+ +
+ + +
); } - - return ( -
- -
- - -
-
- ); -} +); export default memo(PromptReply); diff --git a/frontend/src/components/WorkspaceChat/ChatContainer/ChatHistory/index.jsx b/frontend/src/components/WorkspaceChat/ChatContainer/ChatHistory/index.jsx index 6e400c03..201cbc7d 100644 --- a/frontend/src/components/WorkspaceChat/ChatContainer/ChatHistory/index.jsx +++ b/frontend/src/components/WorkspaceChat/ChatContainer/ChatHistory/index.jsx @@ -1,8 +1,19 @@ import { Frown } from "react-feather"; import HistoricalMessage from "./HistoricalMessage"; import PromptReply from "./PromptReply"; +import { useEffect, useRef } from "react"; export default function ChatHistory({ history = [], workspace }) { + const replyRef = useRef(null); + + useEffect(() => { + if (replyRef.current) { + setTimeout(() => { + replyRef.current.scrollIntoView({ behavior: "smooth", block: "end" }); + }, 700); + } + }, [history]); + if (history.length === 0) { return (
@@ -22,48 +33,37 @@ export default function ChatHistory({ history = [], workspace }) { className="h-[89%] pb-[100px] md:pt-[50px] md:pt-0 md:pb-5 mx-2 md:mx-0 overflow-y-scroll flex flex-col justify-start no-scroll" id="chat-history" > - {history.map( - ( - { - uuid = null, - content, - sources = [], - role, - closed = true, - pending = false, - error = false, - animate = false, - }, - index - ) => { - const isLastBotReply = - index === history.length - 1 && role === "assistant"; - if (isLastBotReply && animate) { - return ( - - ); - } + {history.map((props, index) => { + const isLastMessage = index === history.length - 1; + + if (props.role === "assistant" && props.animate) { return ( - ); } - )} + + return ( + + ); + })}
); }