Skip to content

Commit

Permalink
fix: Minor bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
tjtanjin committed Oct 21, 2024
1 parent 2220cd6 commit 8d8c8c7
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 36 deletions.
3 changes: 3 additions & 0 deletions __tests__/hooks/internal/useChatHistoryInternal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ describe("useChatHistoryInternal Hook", () => {
expect.any(Object),
initialChatHistory,
expect.any(Function),
expect.any(Object),
expect.any(Number),
expect.any(Function),
);

// checks if history is being loaded
Expand Down
20 changes: 13 additions & 7 deletions src/components/Buttons/VoiceButton/VoiceButton.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, MouseEvent, useRef } from "react";
import { useEffect, MouseEvent, useRef, useState } from "react";

import MediaDisplay from "../../ChatBotBody/MediaDisplay/MediaDisplay";
import { startVoiceRecording, stopVoiceRecording } from "../../../services/VoiceService";
Expand Down Expand Up @@ -42,6 +42,17 @@ const VoiceButton = () => {
// tracks audio chunk (if voice is sent as audio)
const audioChunksRef = useRef<BlobPart[]>([]);

// serves as a workaround (together with useEffect hook) for sending voice input, can consider a better approach
const [voiceInputTrigger, setVoiceInputTrigger] = useState<boolean>(false);
useEffect(() => {
if (settings.voice?.sendAsAudio) {
handleSendAsAudio();
audioChunksRef.current = [];
} else {
handleSubmitText();
}
}, [voiceInputTrigger])

// handles starting and stopping of voice recording on toggle
useEffect(() => {
if (voiceToggledOn) {
Expand Down Expand Up @@ -71,12 +82,7 @@ const VoiceButton = () => {
* Handles submission of user voice input.
*/
const triggerSendVoiceInput = () => {
if (settings.voice?.sendAsAudio) {
handleSendAsAudio();
audioChunksRef.current = [];
} else {
handleSubmitText();
}
setVoiceInputTrigger(prev => !prev);
}

/*
Expand Down
25 changes: 2 additions & 23 deletions src/components/ChatBotBody/ChatBotBody.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Dispatch, SetStateAction, useEffect, CSSProperties, MouseEvent } from "react";
import { Dispatch, SetStateAction, CSSProperties, MouseEvent } from "react";

import ChatMessagePrompt from "./ChatMessagePrompt/ChatMessagePrompt";
import ToastPrompt from "./ToastPrompt/ToastPrompt";
import { getHistoryMessages, loadChatHistory } from "../../services/ChatHistoryService";
import { useChatWindowInternal } from "../../hooks/internal/useChatWindowInternal";
import { useBotStatesContext } from "../../context/BotStatesContext";
import { useBotRefsContext } from "../../context/BotRefsContext";
Expand All @@ -17,14 +16,11 @@ import "./ChatBotBody.css";
/**
* Contains chat messages between the user and bot.
*
* @param chatScrollHeight number representing chat window scroll height
* @param setChatScrollHeight setter for tracking chat window scroll height
*/
const ChatBotBody = ({
chatScrollHeight,
setChatScrollHeight,
}: {
chatScrollHeight: number;
setChatScrollHeight: Dispatch<SetStateAction<number>>;
}) => {
// handles settings
Expand All @@ -34,7 +30,7 @@ const ChatBotBody = ({
const { styles } = useStylesContext();

// handles messages
const { messages, setMessages } = useMessagesContext();
const { messages } = useMessagesContext();

// handles toasts
const { toasts } = useToastsContext();
Expand All @@ -44,9 +40,7 @@ const ChatBotBody = ({

// handles bot states
const {
isLoadingChatHistory,
isBotTyping,
setIsLoadingChatHistory,
setIsScrolling,
setUnreadCount,
} = useBotStatesContext();
Expand Down Expand Up @@ -87,21 +81,6 @@ const ChatBotBody = ({
...styles.toastPromptContainerStyle
};

useEffect(() => {
if (isLoadingChatHistory) {
loadChatHistory(settings, styles, getHistoryMessages(), setMessages);
setTimeout(() => {
if (!chatBodyRef.current) {
return;
}
const { scrollHeight } = chatBodyRef.current;
const scrollDifference = scrollHeight - chatScrollHeight;
chatBodyRef.current.scrollTop = chatBodyRef.current.scrollTop + scrollDifference;
setIsLoadingChatHistory(false);
}, 501)
}
}, [isLoadingChatHistory])

/**
* Checks and updates whether a user is scrolling in chat window.
*/
Expand Down
3 changes: 1 addition & 2 deletions src/components/ChatBotContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ const ChatBotContainer = ({

// handles chat window
const {
chatScrollHeight,
setChatScrollHeight,
viewportHeight,
viewportWidth,
Expand Down Expand Up @@ -147,7 +146,7 @@ const ChatBotContainer = ({
}
<div style={getChatWindowStyle()} className="rcb-chat-window">
{settings.general?.showHeader && <ChatBotHeader buttons={headerButtons}/>}
<ChatBotBody chatScrollHeight={chatScrollHeight} setChatScrollHeight={setChatScrollHeight}/>
<ChatBotBody setChatScrollHeight={setChatScrollHeight}/>
{settings.general?.showInputRow && <ChatBotInput buttons={chatInputButtons}/>}
{settings.general?.showFooter && <ChatBotFooter buttons={footerButtons}/>}
</div>
Expand Down
13 changes: 12 additions & 1 deletion src/hooks/internal/useChatHistoryInternal.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { useCallback } from "react";

import { getHistoryMessages } from "../../services/ChatHistoryService";
import { getHistoryMessages, loadChatHistory } from "../../services/ChatHistoryService";
import { useRcbEventInternal } from "./useRcbEventInternal";
import { useChatWindowInternal } from "./useChatWindowInternal";
import { useBotRefsContext } from "../../context/BotRefsContext";
import { useMessagesContext } from "../../context/MessagesContext";
import { useSettingsContext } from "../../context/SettingsContext";
import { useStylesContext } from "../../context/StylesContext";
Expand All @@ -27,9 +29,15 @@ export const useChatHistoryInternal = () => {
setIsLoadingChatHistory,
} = useBotStatesContext();

// handles bot refs
const { chatBodyRef } = useBotRefsContext();

// handles rcb events
const { callRcbEvent } = useRcbEventInternal();

// handles chat window
const { chatScrollHeight } = useChatWindowInternal();

/**
* Loads and shows chat history in the chat window.
*
Expand All @@ -49,6 +57,9 @@ export const useChatHistoryInternal = () => {
}
}
setIsLoadingChatHistory(true);
loadChatHistory(settings, styles, chatHistory, setMessages,
chatBodyRef, chatScrollHeight, setIsLoadingChatHistory
);
}, [settings, styles, setMessages]);

return { isLoadingChatHistory, setIsLoadingChatHistory, showChatHistory };
Expand Down
19 changes: 16 additions & 3 deletions src/services/ChatHistoryService.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,13 @@ const parseMessageToString = (message: Message) => {
* @param styles styles provided to the bot
* @param chatHistory chat history to show
* @param setMessages setter for updating messages
* @param prevTextAreaDisabled boolean indicating if text area was previously disabled
* @param setTextAreaDisabled setter for enabling/disabling user text area
* @param chatBodyRef reference to the chat body
* @param chatScrollHeight current chat scroll height
* @param setIsLoadingChatHistory setter for whether chat history is loading
*/
const loadChatHistory = (settings: Settings, styles: Styles, chatHistory: Message[],
setMessages: Dispatch<SetStateAction<Message[]>>) => {
setMessages: Dispatch<SetStateAction<Message[]>>, chatBodyRef: React.RefObject<HTMLDivElement | null>,
chatScrollHeight: number, setIsLoadingChatHistory: Dispatch<SetStateAction<boolean>>) => {

historyLoaded = true;
if (chatHistory != null) {
Expand Down Expand Up @@ -160,6 +162,17 @@ const loadChatHistory = (settings: Settings, styles: Styles, chatHistory: Messag
return [...parsedMessages, lineBreakMessage, ...prevMessages];
});
}, 500)

// slight delay afterwards to maintain scroll position
setTimeout(() => {
if (!chatBodyRef.current) {
return;
}
const { scrollHeight } = chatBodyRef.current;
const scrollDifference = scrollHeight - chatScrollHeight;
chatBodyRef.current.scrollTop = chatBodyRef.current.scrollTop + scrollDifference;
setIsLoadingChatHistory(false);
}, 510)
} catch {
// remove chat history on error (to address corrupted storage values)
localStorage.removeItem(settings.chatHistory?.storageKey as string);
Expand Down

0 comments on commit 8d8c8c7

Please sign in to comment.