Skip to content

Commit

Permalink
fix duplicate error message
Browse files Browse the repository at this point in the history
  • Loading branch information
Yen Truong committed Oct 17, 2023
1 parent 38947e2 commit 3a588ac
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/components/ChatPanel.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback, useEffect, useRef } from "react";
import React, { useCallback, useEffect, useRef, useState } from "react";
import { useChatState, useChatActions } from "@yext/chat-headless-react";
import {
MessageBubble,
Expand Down Expand Up @@ -65,7 +65,7 @@ export interface ChatPanelProps
* @param props - {@link ChatPanelProps}
*/
export function ChatPanel(props: ChatPanelProps) {
const { header, customCssClasses } = props;
const { header, customCssClasses, stream, handleError } = props;
const chat = useChatActions();
const messages = useChatState((state) => state.conversation.messages);
const loading = useChatState((state) => state.conversation.isLoading);
Expand All @@ -75,22 +75,28 @@ export function ChatPanel(props: ChatPanelProps) {
const cssClasses = useComposedCssClasses(builtInCssClasses, customCssClasses);
const defaultHandleApiError = useDefaultHandleApiError();
const reportAnalyticsEvent = useReportAnalyticsEvent();
const [fetchInitialMessage, setFetchInitialMessage] = useState(false);

useEffect(() => {
reportAnalyticsEvent({
action: "CHAT_IMPRESSION",
});
}, [reportAnalyticsEvent]);

// Fetch the first message on load, if there are no existing messages or a request being processed
// Request initial message only if there are no existing messages and no ongoing request.
useEffect(() => {
if (messages.length !== 0 || !canSendMessage) {
setFetchInitialMessage(messages.length === 0 && canSendMessage)
}, [messages.length, canSendMessage])

useEffect(() => {
if (!fetchInitialMessage) {
return;
}
const { stream = false, handleError } = props;
// Ensures that the fetch for the initial message occurs only once
setFetchInitialMessage(false)
const res = stream ? chat.streamNextMessage() : chat.getNextMessage();
res.catch((e) => (handleError ? handleError(e) : defaultHandleApiError(e)));
}, [chat, props, messages, defaultHandleApiError, canSendMessage]);
}, [chat, stream, handleError, defaultHandleApiError, fetchInitialMessage]);

const messagesRef = useRef<Array<HTMLDivElement | null>>([]);
const messagesContainer = useRef<HTMLDivElement>(null);
Expand Down

0 comments on commit 3a588ac

Please sign in to comment.