Skip to content

Commit 1222445

Browse files
authored
Merge pull request #232 from iceljc/features/refine-chat-window
prevent duplicate chat
2 parents ad86b5b + e1503bf commit 1222445

File tree

5 files changed

+53
-39
lines changed

5 files changed

+53
-39
lines changed

src/lib/common/LiveChatEntry.svelte

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import { getSettingDetail } from '$lib/services/setting-service';
66
import { chatBotStore } from '$lib/helpers/store';
77
import { CHAT_FRAME_ID } from '$lib/helpers/constants';
8+
import { ChatAction } from '$lib/helpers/enums';
89
910
let chatUrl = PUBLIC_LIVECHAT_HOST;
1011
@@ -15,9 +16,9 @@
1516
1617
// Handle event from iframe
1718
window.onmessage = async function(e) {
18-
if (e.data.action == 'close') {
19+
if (e.data.action == ChatAction.Close) {
1920
chatBotStore.set({ showChatBox: false });
20-
} else if (e.data.action == 'open') {
21+
} else if (e.data.action == ChatAction.Open) {
2122
chatBotStore.set({ showChatBox: true });
2223
}
2324
};

src/lib/helpers/enums.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ const knowledgeDocSource = {
101101
export const KnowledgeDocSource = Object.freeze(knowledgeDocSource);
102102

103103
const chatAction = {
104+
Open: 'open',
105+
Close: 'close',
104106
Logout: 'logout',
105107
Chat: 'chat',
106108
NewChat: 'new-chat'

src/lib/helpers/utils/chat.js

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,4 @@ export function addChatBoxMountEventListener(func) {
2323
func();
2424
}
2525
});
26-
}
27-
28-
/**
29-
* @param {string} chatFrameId
30-
* @param {string} text
31-
*/
32-
export function loadChatFrame(chatFrameId, text) {
33-
const chatFrame = document.getElementById(chatFrameId);
34-
if (chatFrame) {
35-
// @ts-ignore
36-
chatFrame.contentWindow.postMessage({ action: "chat", text: text }, "*");
37-
}
3826
}

src/lib/services/setting-service.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export async function getSettings() {
1515
/**
1616
*
1717
* @param {string} id
18-
* @returns {Promise<object>}
18+
* @returns {Promise<any>}
1919
*/
2020
export async function getSettingDetail(id) {
2121
let url = replaceUrl(endpoints.settingDetailUrl, {id: id});

src/routes/chat/[agentId]/[conversationId]/chat-box.svelte

Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@
157157
let loadChatUtils = false;
158158
let disableSpeech = false;
159159
let isLoading = false;
160-
160+
let isCreatingNewConv = false;
161161
162162
$: {
163163
const editor = lastBotMsg?.rich_content?.editor || '';
@@ -198,37 +198,58 @@
198198
refresh();
199199
autoScrollLog = false;
200200
201-
window.addEventListener('message', async e => {
201+
window.addEventListener('message', e => {
202202
if (e.data.action === ChatAction.Logout) {
203-
resetLocalStorage(true);
204-
return;
203+
handleLogoutAction();
204+
} else if (e.data.action === ChatAction.NewChat) {
205+
handleNewChatAction(e);
206+
} else if (e.data.action === ChatAction.Chat) {
207+
handleChatAction(e);
205208
}
209+
});
210+
});
206211
207-
if (e.data.action === ChatAction.NewChat) {
208-
const conv = await createNewConversation();
209-
if (!!e.data.text && !isThinking && !isSendingMsg) {
212+
function handleLogoutAction() {
213+
resetLocalStorage(true);
214+
}
215+
216+
/** @param {any} e */
217+
function handleNewChatAction(e) {
218+
if (!isCreatingNewConv && !isThinking && !isSendingMsg) {
219+
isCreatingNewConv = true;
220+
createNewConversation().then(conv => {
221+
isCreatingNewConv = false;
222+
if (conv && !!e.data.text) {
210223
isLoading = true;
211224
sendChatMessage(e.data.text, e.data.data || null, conv.id).then(() => {
212225
redirectToNewConversation(conv);
213226
isLoading = false;
214227
openFrame();
228+
}).catch(() => {
229+
isLoading = false;
230+
openFrame();
215231
});
216232
}
217-
return;
218-
}
233+
}).catch(() => {
234+
isCreatingNewConv = false;
235+
});
236+
}
237+
}
219238
220-
if (e.data.action === ChatAction.Chat && !!e.data.text && !isThinking && !isSendingMsg) {
221-
sendChatMessage(e.data.text, e.data.data || null).then(() => {
222-
openFrame();
223-
});
224-
return;
225-
}
226-
});
227-
});
239+
/** @param {any} e */
240+
function handleChatAction(e) {
241+
if (!!e.data.text && !isThinking && !isSendingMsg) {
242+
sendChatMessage(e.data.text, e.data.data || null).then(() => {
243+
openFrame();
244+
}).catch(() => {
245+
openFrame();
246+
});
247+
}
248+
}
228249
229250
function openFrame() {
230251
if (window.location != window.parent.location) {
231-
window.parent.postMessage({ action: "open" }, "*");
252+
window.parent.postMessage({ action: ChatAction.Open }, "*");
232253
}
233254
}
234255
@@ -489,6 +510,8 @@
489510
autoScrollLog = true;
490511
clearInstantLogs();
491512
renewUserSentMessages(msgText);
513+
const agentId = params.agentId;
514+
const convId = conversationId || params.conversationId;
492515
493516
let postback = data?.postback;
494517
if (!postback) {
@@ -511,7 +534,7 @@
511534
if (files?.length > 0 && !!!messageData.inputMessageId) {
512535
const filePayload = buildFilePayload(files);
513536
return new Promise((resolve, reject) => {
514-
uploadConversationFiles(params.agentId, conversationId || params.conversationId, files).then(resMessageId => {
537+
uploadConversationFiles(agentId, convId, files).then(resMessageId => {
515538
messageData = { ...messageData, inputMessageId: resMessageId };
516539
if (!!filePayload) {
517540
messageData = {
@@ -524,7 +547,7 @@
524547
};
525548
}
526549
527-
sendMessageToHub(params.agentId, conversationId || params.conversationId, msgText, messageData).then(res => {
550+
sendMessageToHub(agentId, convId, msgText, messageData).then(res => {
528551
resolve(res);
529552
}).catch(err => {
530553
reject(err);
@@ -537,7 +560,7 @@
537560
} else {
538561
return new Promise((resolve, reject) => {
539562
if (!!messageData?.inputMessageId) {
540-
getConversationFiles(params.conversationId, messageData.inputMessageId, FileSourceType.User).then(retFiles => {
563+
getConversationFiles(convId, messageData.inputMessageId, FileSourceType.User).then(retFiles => {
541564
const filePayload = buildFilePayload(retFiles);
542565
if (!!filePayload) {
543566
messageData = {
@@ -550,7 +573,7 @@
550573
};
551574
}
552575
553-
sendMessageToHub(params.agentId, conversationId || params.conversationId, msgText, messageData).then(res => {
576+
sendMessageToHub(agentId, convId, msgText, messageData).then(res => {
554577
resolve(res);
555578
}).catch(err => {
556579
reject(err);
@@ -560,7 +583,7 @@
560583
});
561584
});
562585
} else {
563-
sendMessageToHub(params.agentId, conversationId || params.conversationId, msgText, messageData).then(res => {
586+
sendMessageToHub(agentId, convId, msgText, messageData).then(res => {
564587
resolve(res);
565588
}).catch(err => {
566589
reject(err);
@@ -749,7 +772,7 @@
749772
}
750773
});
751774
} else {
752-
window.parent.postMessage({ action: "close" }, "*");
775+
window.parent.postMessage({ action: ChatAction.Close }, "*");
753776
}
754777
}
755778

0 commit comments

Comments
 (0)