Skip to content

Commit

Permalink
fix: textArea is disabled after chatHistory is loaded (#180)
Browse files Browse the repository at this point in the history
  • Loading branch information
yaminmomo15 authored Oct 12, 2024
1 parent d3cc373 commit c8631f9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 38 deletions.
55 changes: 26 additions & 29 deletions __tests__/hooks/internal/useChatHistoryInternal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,25 @@ describe("useChatHistoryInternal Hook", () => {

// initial values
const initialIsLoadingChatHistory = false;
const initialChatHistory = [
{
id: generateSecureUUID(),
sender: "user",
content: "Hello",
type: "string",
timestamp: new Date().toUTCString()
},
{
id: generateSecureUUID(),
sender: "bot",
content: "Hi there!",
type: "string",
timestamp: new Date().toUTCString()
},
];
const initialChatHistory = [
{
id: generateSecureUUID(),
sender: "user",
content: "Hello",
type: "string",
timestamp: new Date().toUTCString()
},
{
id: generateSecureUUID(),
sender: "bot",
content: "Hi there!",
type: "string",
timestamp: new Date().toUTCString()
},
];

it("should load chat history correctly, change state and emit rcb-load-chat-history event", async () => {
// mocks rcb event handler
// mocks rcb event handler
const callRcbEventMock = jest.fn().mockReturnValue({ defaultPrevented: false });
mockUseRcbEventInternal.mockReturnValue({
callRcbEvent: callRcbEventMock,
Expand All @@ -56,9 +56,8 @@ describe("useChatHistoryInternal Hook", () => {

// mocks loadChatHistory to resolve successfully
mockLoadChatHistory.mockImplementation(
(settings, styles, chatHistory, setMessages, prevTextAreaDisabled, setTextAreaDisabled) => {
(settings, styles, chatHistory, setMessages) => {
setMessages(chatHistory);
setTextAreaDisabled(false);
return Promise.resolve();
}
);
Expand All @@ -82,20 +81,18 @@ describe("useChatHistoryInternal Hook", () => {
// checks if get history messages was called
expect(mockGetHistoryMessages).toHaveBeenCalledTimes(1);

// checks if callRcbEvent was called with rcb-load-chat-history and correct arguments
// checks if callRcbEvent was called with rcb-load-chat-history and correct arguments
expect(callRcbEventMock).toHaveBeenCalledWith(RcbEvent.LOAD_CHAT_HISTORY, {});
// checks if load chat history was called
expect(mockLoadChatHistory).toHaveBeenCalledWith(

// checks if load chat history was called
expect(mockLoadChatHistory).toHaveBeenCalledWith(
MockDefaultSettings,
expect.any(Object),
initialChatHistory,
expect.any(Function),
expect.any(Boolean),
expect.any(Function)
);

// checks if history is being loaded
// checks if history is being loaded
expect(result.current.isLoadingChatHistory).toBe(true);
});

Expand Down Expand Up @@ -125,13 +122,13 @@ describe("useChatHistoryInternal Hook", () => {
// checks if get history messages was called
expect(mockGetHistoryMessages).toHaveBeenCalledTimes(1);

// checks if callRcbEvent was called with rcb-load-chat-history and correct arguments
// checks if callRcbEvent was called with rcb-load-chat-history and correct arguments
expect(callRcbEventMock).toHaveBeenCalledWith(RcbEvent.LOAD_CHAT_HISTORY, {});

// checks if load chat history was not called
// checks if load chat history was not called
expect(mockLoadChatHistory).not.toHaveBeenCalled();

// checks if history is being loaded
// checks if history is being loaded
expect(result.current.isLoadingChatHistory).toBe(false);
});
});
8 changes: 2 additions & 6 deletions src/hooks/internal/useChatHistoryInternal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ export const useChatHistoryInternal = () => {
const {
isLoadingChatHistory,
setIsLoadingChatHistory,
textAreaDisabled,
setTextAreaDisabled
} = useBotStatesContext();

// handles rcb events
Expand All @@ -51,10 +49,8 @@ export const useChatHistoryInternal = () => {
}
}
setIsLoadingChatHistory(true);
const prevTextAreaDisabled = textAreaDisabled;
setTextAreaDisabled(true);
loadChatHistory(settings, styles, chatHistory, setMessages, prevTextAreaDisabled, setTextAreaDisabled);
}, [settings, styles, setMessages, setTextAreaDisabled]);
loadChatHistory(settings, styles, chatHistory, setMessages);
}, [settings, styles, setMessages]);

return { isLoadingChatHistory, setIsLoadingChatHistory, showChatHistory };
};
4 changes: 1 addition & 3 deletions src/services/ChatHistoryService.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,7 @@ const parseMessageToString = (message: Message) => {
* @param setTextAreaDisabled setter for enabling/disabling user text area
*/
const loadChatHistory = (settings: Settings, styles: Styles, chatHistory: Message[],
setMessages: Dispatch<SetStateAction<Message[]>>, prevTextAreaDisabled: boolean,
setTextAreaDisabled: Dispatch<SetStateAction<boolean>>) => {
setMessages: Dispatch<SetStateAction<Message[]>>) => {

historyLoaded = true;
if (chatHistory != null) {
Expand Down Expand Up @@ -160,7 +159,6 @@ const loadChatHistory = (settings: Settings, styles: Styles, chatHistory: Messag
}
return [...parsedMessages, lineBreakMessage, ...prevMessages];
});
setTextAreaDisabled(prevTextAreaDisabled ?? settings.chatInput?.disabled ?? false);
}, 500)
} catch {
// remove chat history on error (to address corrupted storage values)
Expand Down

0 comments on commit c8631f9

Please sign in to comment.