Skip to content

Commit a0daa89

Browse files
update UIKit version to v6.3.10
1 parent e1b5249 commit a0daa89

7 files changed

Lines changed: 110 additions & 38 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@cometchat/chat-uikit-react",
3-
"version": "6.3.9",
3+
"version": "6.3.10",
44
"description": "Ready-to-use Chat UI Components for React(Javascript/Web)",
55
"author": "CometChat",
66
"exports": {

src/CometChatUIKit/CometChatUIKit.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ class CometChatUIKit {
101101
return new Promise((resolve, reject) => {
102102
window.CometChatUiKit = {
103103
name: "@cometchat/chat-uikit-react",
104-
version: "6.3.9",
104+
version: "6.3.10",
105105
};
106106
CometChat.init(uiKitSettings?.appId, appSettings).then(() => {
107107
CometChat.getLoggedinUser().then((user: CometChat.User | null) => {

src/components/CometChatAIAssistantChat/CometChatAIAssistantChat.tsx

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ interface AIAssistantChatProps {
5555
emptyChatImageView?: React.JSX.Element;
5656
aiAssistantTools?: CometChatAIAssistantTools;
5757
templates?: CometChatMessageTemplate[];
58+
parentMessageId?: number;
59+
loadLastAgentConversation?: boolean;
5860
};
5961

6062
/**
@@ -166,14 +168,17 @@ const CometChatAIAssistantChatComponent = (props: AIAssistantChatProps) => {
166168
emptyChatIntroMessageView,
167169
emptyChatImageView,
168170
aiAssistantTools,
169-
templates
171+
templates,
172+
parentMessageId,
173+
loadLastAgentConversation = false
170174
} = props;
171175

172176
const [startNewChat, setStartNewChat] = useState(false);
173177
const [isSidebarOpen, setIsSidebarOpen] = useState(false);
174-
const [goToMessage, setGoToMessage] = useState<CometChat.BaseMessage | null>(null);
175-
const [parentMessageId, setParentMessageId] = useState<number | null>(null);
176-
const parentMessageIdRef = useRef<number | null>(null);
178+
const [goToMessageId, setGoToMessageId] = useState<number | null>(null);
179+
const [loadLastAgentConversationState, setLoadLastAgentConversationState] = useState<boolean>(loadLastAgentConversation ?? false);
180+
const [activeParentMessageId, setActiveParentMessageId] = useState<number | null>(null);
181+
const activeParentMessageIdRef = useRef<number | null>(null);
177182

178183
// Use suggestions from props if available, otherwise use default suggestions
179184
const displaySuggestions = suggestedMessages.length > 0 ? suggestedMessages : (user.getMetadata() as any)?.suggestedMessages as string[];
@@ -184,6 +189,18 @@ const CometChatAIAssistantChatComponent = (props: AIAssistantChatProps) => {
184189
}
185190
}, [aiAssistantTools])
186191

192+
useEffect(() => {
193+
if (parentMessageId) {
194+
setGoToMessageId(parentMessageId);
195+
setActiveParentMessageId(parentMessageId);
196+
activeParentMessageIdRef.current = parentMessageId;
197+
}
198+
}, [parentMessageId]);
199+
200+
useEffect(() => {
201+
setLoadLastAgentConversationState(loadLastAgentConversation ?? false);
202+
}, [loadLastAgentConversation])
203+
187204

188205
// Function to set auxiliary view in message header.
189206
const setAuxiliaryView = useCallback(() => {
@@ -206,10 +223,11 @@ const CometChatAIAssistantChatComponent = (props: AIAssistantChatProps) => {
206223
const onNewChatButtonClick = () => {
207224
// Force component rerender by updating state
208225
setStartNewChat(prev => !prev);
209-
setGoToMessage(null)
210-
setParentMessageId(null);
211-
parentMessageIdRef.current = null;
226+
setGoToMessageId(null)
227+
setActiveParentMessageId(null);
228+
activeParentMessageIdRef.current = null;
212229
stopStreamingMessage();
230+
setLoadLastAgentConversationState(false);
213231
}
214232

215233
const onHistoryButtonClick = () => {
@@ -262,8 +280,10 @@ const CometChatAIAssistantChatComponent = (props: AIAssistantChatProps) => {
262280
);
263281

264282
function onDeleteChat(id?: number) {
265-
if (id) {
266-
if (parentMessageIdRef.current && id === parentMessageIdRef.current) {
283+
const activeParentId = Number(activeParentMessageIdRef.current)
284+
const msgId = Number(id);
285+
if (msgId) {
286+
if (activeParentId && msgId === activeParentId) {
267287
onNewChatButtonClick();
268288
}
269289
}
@@ -293,7 +313,7 @@ const CometChatAIAssistantChatComponent = (props: AIAssistantChatProps) => {
293313
onError={onError}
294314
/>
295315
<CometChatMessageList
296-
key={`message-list-${startNewChat}-${goToMessage ? goToMessage?.getId() : 'none'}`}
316+
key={`message-list-${startNewChat}-${goToMessageId ?? 'none'}-${loadLastAgentConversationState}`}
297317
user={user}
298318
emptyView={emptyView || defaultEmptyView}
299319
loadingView={loadingView}
@@ -315,15 +335,16 @@ const CometChatAIAssistantChatComponent = (props: AIAssistantChatProps) => {
315335
hideFlagMessageOption={true}
316336
disableSoundForMessages={true}
317337
textFormatters={[]}
318-
parentMessageId={goToMessage ? goToMessage?.getId() : undefined}
338+
parentMessageId={goToMessageId ?? undefined}
319339
templates={templates}
340+
loadLastAgentConversation={loadLastAgentConversationState}
320341
/>
321342
<MessageComposerView
322343
user={user}
323-
parentMessageId={parentMessageId}
344+
parentMessageId={activeParentMessageId}
324345
startNewChat={startNewChat}
325346
onError={onError}
326-
setParentMessageId={setParentMessageId}
347+
setParentMessageId={setActiveParentMessageId}
327348
onSendButtonClick={onSendButtonClick}
328349
/>
329350

@@ -336,15 +357,21 @@ const CometChatAIAssistantChatComponent = (props: AIAssistantChatProps) => {
336357
hideNewChat={hideNewChat}
337358
onNewChatClicked={onDeleteChat}
338359
onMessageClicked={(message: CometChat.BaseMessage) => {
339-
setGoToMessage(message);
340-
setParentMessageId(message.getId());
341-
parentMessageIdRef.current = message.getId();
360+
setGoToMessageId(message.getId());
361+
setActiveParentMessageId(message.getId());
362+
activeParentMessageIdRef.current = message.getId();
342363
setIsSidebarOpen(false);
343364
stopStreamingMessage();
344365
}}
345366
onClose={() => {
346367
setIsSidebarOpen(false)
347-
}} user={user} />
368+
}}
369+
onEmpty={() => {
370+
setLoadLastAgentConversationState(false);
371+
}}
372+
user={user}
373+
loadLastAgentConversation={parentMessageId === undefined ? loadLastAgentConversationState : false}
374+
/>
348375
</div>
349376
</div>
350377

src/components/CometChatAIAssistantChatHistory/CometChatAIAssistantChatHistory.tsx

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,17 @@ interface CometChatAIAssistantChatHistoryProps {
5454
*/
5555
hideNewChat?: boolean;
5656

57+
/**
58+
* Callback function triggered when the chat history is empty.
59+
* Useful for handling UI state when no conversations exist.
60+
*/
61+
onEmpty?: (() => void) | undefined;
62+
63+
/**
64+
* Loads the most recent existing agent conversation if one is available, by clicking it.
65+
* @default false
66+
*/
67+
loadLastAgentConversation?: boolean;
5768
}
5869

5970
const CometChatAIAssistantChatHistory = (props: CometChatAIAssistantChatHistoryProps) => {
@@ -64,7 +75,9 @@ const CometChatAIAssistantChatHistory = (props: CometChatAIAssistantChatHistoryP
6475
onClose,
6576
onMessageClicked,
6677
onNewChatClicked,
67-
hideNewChat
78+
hideNewChat,
79+
loadLastAgentConversation = false,
80+
onEmpty
6881
} = props;
6982

7083
// State variables
@@ -127,7 +140,7 @@ const CometChatAIAssistantChatHistory = (props: CometChatAIAssistantChatHistoryP
127140
/**
128141
* Function to fetch previous messages
129142
*/
130-
const fetchPreviousMessages = useCallback(() => {
143+
const fetchPreviousMessages = useCallback((shouldLoadLastConversation: boolean = false) => {
131144
return new Promise<boolean>(async (resolve, reject) => {
132145
try {
133146
if (isFetchingRef.current) {
@@ -160,10 +173,15 @@ const CometChatAIAssistantChatHistory = (props: CometChatAIAssistantChatHistoryP
160173

161174
if (messages.length > 0) {
162175
await appendMessages(messages.reverse());
176+
177+
if (shouldLoadLastConversation)
178+
onMessageClicked?.(messages[0]);
179+
163180
setListState(States.loaded);
164181
} else {
165182
if (messages.length == 0 && messagesCountRef.current === 0) {
166183
setListState(States.empty);
184+
onEmpty?.();
167185
} else {
168186
setListState(States.loaded);
169187
}
@@ -181,7 +199,7 @@ const CometChatAIAssistantChatHistory = (props: CometChatAIAssistantChatHistoryP
181199
reject(error);
182200
}
183201
});
184-
}, [appendMessages, errorHandler]);
202+
}, [appendMessages, errorHandler, onMessageClicked]);
185203

186204
/**
187205
* Callback to be executed when the list is scrolled to the top
@@ -365,7 +383,7 @@ const CometChatAIAssistantChatHistory = (props: CometChatAIAssistantChatHistoryP
365383
messagesCountRef.current = 0;
366384
setMessageList([]);
367385
setListState(States.loading);
368-
fetchPreviousMessages();
386+
fetchPreviousMessages(loadLastAgentConversation);
369387
}
370388
} catch (error) {
371389
errorHandler(error, "useEffect - initialization");

src/components/CometChatMessageList/CometChatMessageList.tsx

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,12 @@ interface MessageListProps {
351351
* @defaultValue `false`
352352
*/
353353
hideFlagRemarkField?: boolean;
354+
355+
/**
356+
* Loads the most recent existing agent conversation if one is available. Used to set state as loading when enabled.
357+
* @default false
358+
*/
359+
loadLastAgentConversation?: boolean;
354360
}
355361

356362
const defaultProps: MessageListProps = {
@@ -403,7 +409,8 @@ const defaultProps: MessageListProps = {
403409
showScrollbar: false,
404410
isAgentChat: false,
405411
hideFlagMessageOption: false,
406-
hideFlagRemarkField: false
412+
hideFlagRemarkField: false,
413+
loadLastAgentConversation: false,
407414
};
408415

409416
const CometChatMessageList = (props: MessageListProps) => {
@@ -459,7 +466,8 @@ const CometChatMessageList = (props: MessageListProps) => {
459466
isAgentChat,
460467
hideFlagMessageOption,
461468
hideFlagRemarkField,
462-
showMarkAsUnreadOption
469+
showMarkAsUnreadOption,
470+
loadLastAgentConversation,
463471
} = { ...defaultProps, ...props };
464472
/**
465473
* All the useState useCometChatMessageList are declaired here. These trigger a rerender when updated.
@@ -468,7 +476,9 @@ const CometChatMessageList = (props: MessageListProps) => {
468476
const [scrollListToBottom, setScrollListToBottom] = useState<boolean>(true);
469477
const [showToast, setShowToast] = useState<boolean>(false);
470478
const [messageListState, setMessageListState] = useState<States>(
471-
isAgentChat && !parentMessageId ? States.empty : States.loading
479+
isAgentChat && !parentMessageId ?
480+
loadLastAgentConversation ? States.loading :
481+
States.empty : States.loading
472482
);
473483
const [showCallScreen, setShowCallscreen] = useState<boolean>(false);
474484
const [showMessageInfoPopup, setShowMessageInfoPopup] = useState<boolean>(false);
@@ -855,8 +865,11 @@ const CometChatMessageList = (props: MessageListProps) => {
855865
try {
856866
const receiverId = message?.getReceiverId();
857867
const receiverType = message?.getReceiverType();
858-
if (parentMessageIdRef.current) {
859-
if (message.getParentMessageId() === parentMessageIdRef.current || message.getId() === parentMessageIdRef.current) {
868+
const parentMsgId = Number(message.getParentMessageId());
869+
const msgId = Number(message.getId());
870+
const currentParentId = Number(parentMessageIdRef.current);
871+
if (currentParentId) {
872+
if (parentMsgId === currentParentId || msgId === currentParentId) {
860873
return true;
861874
}
862875
} else {
@@ -971,8 +984,11 @@ const CometChatMessageList = (props: MessageListProps) => {
971984
const receiverType = message?.getReceiverType();
972985
const senderId = message?.getSender()?.getUid();
973986
if (parentMessageIdRef.current) {
974-
if (message.getParentMessageId() === parentMessageIdRef.current
975-
|| (isAgentChat && message.getId() === parentMessageIdRef.current)
987+
const parentMsgId = Number(message.getParentMessageId());
988+
const msgId = Number(message.getId());
989+
const currentParentId = Number(parentMessageIdRef.current);
990+
if (parentMsgId === currentParentId
991+
|| (isAgentChat && msgId === currentParentId)
976992
) {
977993
return true;
978994
}
@@ -995,7 +1011,7 @@ const CometChatMessageList = (props: MessageListProps) => {
9951011
} catch (error) {
9961012
errorHandler(error, "isPartOfCurrentChatForSDKEvent")
9971013
}
998-
}, []
1014+
}, [isAgentChat, errorHandler]
9991015
)
10001016

10011017
/*
@@ -2237,7 +2253,7 @@ const CometChatMessageList = (props: MessageListProps) => {
22372253
let shouldStartFromUnreadMessages = startFromUnreadMessages;
22382254

22392255
// If no explicit goToMessageId/quotedMessageId and no lastReadMessageId, try to fetch from conversation
2240-
if ((userRef.current || groupRef.current) && !currentConversationRef.current) {
2256+
if ((userRef.current || groupRef.current) && !currentConversationRef.current && !isAgentChat) {
22412257
conversationLastReadId = await getConversationAndSetLastReadMessage();
22422258
if(currentConversationRef.current) {
22432259
unreadMessageCount = (currentConversationRef.current as CometChat.Conversation).getUnreadMessageCount() || 0;
@@ -4639,6 +4655,9 @@ const getStatusInfoView: (item: CometChat.BaseMessage) => any = useCallback(
46394655
* @returns {States} - Returns the current state of the message list
46404656
*/
46414657
const getCurrentMessageListState: () => States = useCallback(() => {
4658+
if (messageListState === States.loading) {
4659+
return States.loading;
4660+
}
46424661
return messageListState !== States.error && messageList.length === 0 ? States.empty : messageListState;
46434662
}, [messageListState, messageList]);
46444663
/**
@@ -4762,7 +4781,8 @@ const getStatusInfoView: (item: CometChat.BaseMessage) => any = useCallback(
47624781
showSmartReplies,
47634782
goToMessageId,
47644783
isAgentChat,
4765-
messageRepliedTo
4784+
messageRepliedTo,
4785+
loadLastAgentConversation
47664786
);
47674787
return (
47684788
<>

src/components/CometChatMessageList/CometChatMessageListController.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ export class MessageListManager {
6060
builder.withParent(true)
6161
}
6262
}
63+
if (isAgentChat) {
64+
builder.hideDeletedMessages(true)
65+
}
6366
if (user) {
6467
builder.setUID(user.getUid())
6568
builder.guid = undefined;

src/components/CometChatMessageList/useCometChatMessageList.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ function useCometChatMessageList(
3232
showSmartReplies?:boolean,
3333
goToMessageId?:string,
3434
isAgentChat?:boolean,
35-
messageRepliedTo?: string
35+
messageRepliedTo?: string,
36+
loadLastAgentConversation?: boolean
3637

3738
): void {
3839
/**
@@ -94,9 +95,12 @@ function useCometChatMessageList(
9495
setScrollListToBottom(true);
9596
isOnBottomRef.current = true;
9697
}
97-
if(!isAgentChat || (isAgentChat && parentMessageId)){
98-
fetchPreviousMessages();
99-
}
98+
if (
99+
!isAgentChat ||
100+
(isAgentChat && parentMessageId)
101+
) {
102+
fetchPreviousMessages();
103+
}
100104
smartReplyViewRef.current = null;
101105
}
102106
return () => {
@@ -106,7 +110,7 @@ function useCometChatMessageList(
106110
} catch (error) {
107111
errorHandler(error,"useEffect")
108112
}
109-
}, [user, group,isAgentChat, messageRepliedTo]);
113+
}, [user, group, isAgentChat, parentMessageId, loadLastAgentConversation, messageRepliedTo]);
110114

111115

112116
useEffect(() => {

0 commit comments

Comments
 (0)