Skip to content

Commit 9dd7b00

Browse files
authored
Bug Fix: double initial fetch message from ChatPopup (#70)
when `openOnLoad` and `showUnreadNotification` prop are both in use, initial message was fetched twice -- once from ChatPopUp component and once from ChatPanel. This is because useFetchInitialMessage doesn't share state between the two component. This PR updates it so ChatPopUp component only fetch when ChatPanel is not render, otherwise ChatPanel should always do the fetching of the initial message J=CLIP-1231 TEST=manual see that it works with both `openOnLoad` and `showUnreadNotification` on https://github.com/yext/chat-ui-react/assets/36055303/12ccb877-8665-4f8e-a5bc-3e883391f857
1 parent df2b8e1 commit 9dd7b00

File tree

6 files changed

+22
-39
lines changed

6 files changed

+22
-39
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@yext/chat-ui-react",
3-
"version": "0.9.1",
3+
"version": "0.9.2",
44
"description": "A library of React Components for powering Yext Chat integrations.",
55
"author": "clippy@yext.com",
66
"main": "./lib/commonjs/src/index.js",

src/components/ChatPopUp.tsx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -147,12 +147,6 @@ export function ChatPopUp(props: ChatPopUpProps) {
147147
const [numReadMessages, setNumReadMessagesLength] = useState<number>(0);
148148
const [numUnreadMessages, setNumUnreadMessagesLength] = useState<number>(0);
149149

150-
useFetchInitialMessage(
151-
showInitialMessagePopUp ? console.error : handleError,
152-
false,
153-
showUnreadNotification || showInitialMessagePopUp
154-
);
155-
156150
const [showInitialMessage, setshowInitialMessage] = useState(
157151
//only show initial message popup (if specified) when CTA label is not provided
158152
!ctaLabel && showInitialMessagePopUp
@@ -168,6 +162,14 @@ export function ChatPopUp(props: ChatPopUpProps) {
168162
// to avoid message requests immediately on load while the popup is still "hidden"
169163
const [renderChat, setRenderChat] = useState(false);
170164

165+
166+
// only fetch initial message when ChatPanel is closed on load (otherwise, it will be fetched in ChatPanel)
167+
useFetchInitialMessage(
168+
showInitialMessagePopUp ? console.error : handleError,
169+
false,
170+
(showUnreadNotification || showInitialMessagePopUp) && !renderChat && !openOnLoad,
171+
);
172+
171173
// update in useEffect, instead of having openOnLoad as initial state for show/renderChat,
172174
// in order to maintain the fade-in CSS animation when opening the panel on load
173175
useEffect(() => {
@@ -192,7 +194,7 @@ export function ChatPopUp(props: ChatPopUpProps) {
192194
}, [customOnClose, messages]);
193195

194196
useEffect(() => {
195-
//update number of unread messages if there are new messages added while the panel is closed
197+
// update number of unread messages if there are new messages added while the panel is closed
196198
setNumUnreadMessagesLength(messages.length - numReadMessages);
197199
}, [messages, numReadMessages]);
198200

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useEffect, useState } from "react";
1+
import { useEffect } from "react";
22
import { useChatState, useChatActions } from "@yext/chat-headless-react";
33
import { useDefaultHandleApiError } from "../hooks/useDefaultHandleApiError";
44

@@ -16,43 +16,20 @@ import { useDefaultHandleApiError } from "../hooks/useDefaultHandleApiError";
1616
export function useFetchInitialMessage(
1717
handleError?: (e: unknown) => void,
1818
stream = false,
19-
customCondition = true
19+
customCondition = true,
2020
) {
2121
const chat = useChatActions();
2222
const defaultHandleApiError = useDefaultHandleApiError();
2323
const messages = useChatState((state) => state.conversation.messages);
24-
const [fetchInitialMessage, setFetchInitialMessage] = useState(
25-
messages.length === 0
26-
);
27-
const [messagesLength, setMessagesLength] = useState(messages.length);
2824
const canSendMessage = useChatState(
2925
(state) => state.conversation.canSendMessage
3026
);
31-
32-
//handle message history resets
33-
useEffect(() => {
34-
const newMessagesLength = messages.length;
35-
// Fetch data only when the conversation messages changes from non-zero to zero
36-
if (messagesLength > 0 && newMessagesLength === 0) {
37-
setFetchInitialMessage(true);
38-
}
39-
setMessagesLength(newMessagesLength);
40-
}, [messages.length, messagesLength]);
41-
27+
4228
useEffect(() => {
43-
if (!fetchInitialMessage || !canSendMessage || !customCondition) {
29+
if (messages.length !== 0 || !canSendMessage || !customCondition) {
4430
return;
4531
}
46-
setFetchInitialMessage(false);
4732
const res = stream ? chat.streamNextMessage() : chat.getNextMessage();
4833
res.catch((e) => (handleError ? handleError(e) : defaultHandleApiError(e)));
49-
}, [
50-
chat,
51-
stream,
52-
handleError,
53-
defaultHandleApiError,
54-
fetchInitialMessage,
55-
canSendMessage,
56-
customCondition,
57-
]);
34+
}, [chat, stream, handleError, defaultHandleApiError, canSendMessage, customCondition, messages.length]);
5835
}

test-site/package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test-site/src/App.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ function App() {
3939
<ChatHeadlessProvider config={config}>
4040
<ChatPopUp
4141
title="Clippy"
42+
openOnLoad={true}
43+
showInitialMessagePopUp={true}
44+
showHeartBeatAnimation={true}
45+
showUnreadNotification={true}
4246
messageSuggestions={[
4347
"hey how are you",
4448
"I'm looking to order a pair of all-mountain skis",

0 commit comments

Comments
 (0)