Skip to content

Commit

Permalink
fix: Fix chat history load and stream message scroll positions
Browse files Browse the repository at this point in the history
  • Loading branch information
tjtanjin committed Mar 15, 2024
1 parent cfaa27b commit 4385c4a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
19 changes: 14 additions & 5 deletions src/components/ChatBotBody/ChatBotBody.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { RefObject, Dispatch, SetStateAction, useEffect, CSSProperties, MouseEvent } from "react";
import { RefObject, Dispatch, SetStateAction, useEffect, useRef, CSSProperties, MouseEvent } from "react";

import { useBotOptions } from "../../context/BotOptionsContext";
import { useMessages } from "../../context/MessagesContext";
Expand All @@ -14,19 +14,22 @@ import "./ChatBotBody.css";
* @param isLoadingChatHistory boolean indicating is chat history is being loaded
* @param prevScrollHeight number representing previously scrolled height
* @param setPrevScrollHeight setter for tracking scroll height
* @param setIsLoadingChatHistory setter for tracking whether is loading history
*/
const ChatBotBody = ({
chatBodyRef,
isBotTyping,
isLoadingChatHistory,
prevScrollHeight,
setPrevScrollHeight
setPrevScrollHeight,
setIsLoadingChatHistory,
}: {
chatBodyRef: RefObject<HTMLDivElement>;
isBotTyping: boolean;
isLoadingChatHistory: boolean;
prevScrollHeight: number;
setPrevScrollHeight: Dispatch<SetStateAction<number>>;
setIsLoadingChatHistory: Dispatch<SetStateAction<boolean>>;
}) => {

// handles options for bot
Expand All @@ -35,6 +38,9 @@ const ChatBotBody = ({
// handles messages between user and the chat bot
const { messages } = useMessages();

// track if is scrolling body
const isScrollingBody = useRef<boolean>(false);

// styles for chat body
const bodyStyle: CSSProperties = {
...botOptions?.bodyStyle
Expand Down Expand Up @@ -63,20 +69,23 @@ const ChatBotBody = ({
const scrollDifference = scrollHeight - prevScrollHeight;
chatBodyRef.current.scrollTop = chatBodyRef.current.scrollTop + scrollDifference;
setPrevScrollHeight(scrollHeight);
setIsLoadingChatHistory(false);
return;
}

if (chatBodyRef.current != null) {
if (chatBodyRef.current != null && !isScrollingBody.current) {
chatBodyRef.current.scrollTop = chatBodyRef.current.scrollHeight;
}
}, [messages.length, isBotTyping]);
}, [chatBodyRef.current?.scrollHeight, messages.length, isBotTyping]);

/**
* Updates scroll height within the chat body.
*/
const updateScrollHeight = () => {
if (chatBodyRef?.current != null) {
setPrevScrollHeight(chatBodyRef.current.scrollHeight);
const { scrollTop, clientHeight, scrollHeight } = chatBodyRef.current;
setPrevScrollHeight(scrollHeight);
isScrollingBody.current = (scrollHeight - scrollTop !== clientHeight)
}
};

Expand Down
8 changes: 6 additions & 2 deletions src/components/ChatBotContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,10 @@ const ChatBotContainer = ({ flow }: { flow: Flow }) => {
await injectMessage(userInput, "user");
}

if (chatBodyRef.current != null) {
chatBodyRef.current.scrollTop = chatBodyRef.current.scrollHeight;
}

// Clear input field
if (inputRef.current) {
inputRef.current.value = "";
Expand Down Expand Up @@ -637,8 +641,8 @@ const ChatBotContainer = ({ flow }: { flow: Flow }) => {
/>
}
<ChatBotBody chatBodyRef={chatBodyRef} isBotTyping={isBotTyping}
isLoadingChatHistory={isLoadingChatHistory}
prevScrollHeight={prevScrollHeight} setPrevScrollHeight={setPrevScrollHeight}
isLoadingChatHistory={isLoadingChatHistory} prevScrollHeight={prevScrollHeight}
setPrevScrollHeight={setPrevScrollHeight} setIsLoadingChatHistory={setIsLoadingChatHistory}
/>
{botOptions.theme?.showInputRow &&
<ChatBotInput handleToggleVoice={handleToggleVoice} handleActionInput={handleActionInput}
Expand Down
1 change: 0 additions & 1 deletion src/services/ChatHistoryService.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ const loadChatHistory = (botOptions: Options, chatHistory: string, setMessages:
}
return [...parsedMessages, lineBreakMessage, ...prevMessages];
});
setIsLoadingChatHistory(false);
setTextAreaDisabled(botOptions.chatInput?.disabled || false);
}, 500)
} catch {
Expand Down

0 comments on commit 4385c4a

Please sign in to comment.