Skip to content

Commit

Permalink
prevent duplicate chat
Browse files Browse the repository at this point in the history
  • Loading branch information
iceljc committed Oct 1, 2024
1 parent cf04f53 commit 5ee7678
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 37 deletions.
5 changes: 3 additions & 2 deletions src/lib/common/LiveChatEntry.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import { getSettingDetail } from '$lib/services/setting-service';
import { chatBotStore } from '$lib/helpers/store';
import { CHAT_FRAME_ID } from '$lib/helpers/constants';
import { ChatAction } from '$lib/helpers/enums';
let chatUrl = PUBLIC_LIVECHAT_HOST;
Expand All @@ -15,9 +16,9 @@
// Handle event from iframe
window.onmessage = async function(e) {
if (e.data.action == 'close') {
if (e.data.action == ChatAction.Close) {
chatBotStore.set({ showChatBox: false });
} else if (e.data.action == 'open') {
} else if (e.data.action == ChatAction.Open) {
chatBotStore.set({ showChatBox: true });
}
};
Expand Down
2 changes: 2 additions & 0 deletions src/lib/helpers/enums.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ const knowledgeDocSource = {
export const KnowledgeDocSource = Object.freeze(knowledgeDocSource);

const chatAction = {
Open: 'open',
Close: 'close',
Logout: 'logout',
Chat: 'chat',
NewChat: 'new-chat'
Expand Down
12 changes: 0 additions & 12 deletions src/lib/helpers/utils/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,4 @@ export function addChatBoxMountEventListener(func) {
func();
}
});
}

/**
* @param {string} chatFrameId
* @param {string} text
*/
export function loadChatFrame(chatFrameId, text) {
const chatFrame = document.getElementById(chatFrameId);
if (chatFrame) {
// @ts-ignore
chatFrame.contentWindow.postMessage({ action: "chat", text: text }, "*");
}
}
2 changes: 1 addition & 1 deletion src/lib/services/setting-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export async function getSettings() {
/**
*
* @param {string} id
* @returns {Promise<object>}
* @returns {Promise<any>}
*/
export async function getSettingDetail(id) {
let url = replaceUrl(endpoints.settingDetailUrl, {id: id});
Expand Down
63 changes: 41 additions & 22 deletions src/routes/chat/[agentId]/[conversationId]/chat-box.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@
let loadChatUtils = false;
let disableSpeech = false;
let isLoading = false;
let isCreatingNewConv = false;
$: {
const editor = lastBotMsg?.rich_content?.editor || '';
Expand Down Expand Up @@ -200,35 +200,54 @@
window.addEventListener('message', async e => {
if (e.data.action === ChatAction.Logout) {
resetLocalStorage(true);
return;
handleLogoutAction();
} else if (e.data.action === ChatAction.NewChat) {
await handleNewChatAction(e);
} else if (e.data.action === ChatAction.Chat) {
handleChatAction(e);
}
});
});
if (e.data.action === ChatAction.NewChat) {
const conv = await createNewConversation();
if (!!e.data.text && !isThinking && !isSendingMsg) {
isLoading = true;
sendChatMessage(e.data.text, e.data.data || null, conv.id).then(() => {
redirectToNewConversation(conv);
isLoading = false;
openFrame();
});
}
return;
}
function handleLogoutAction() {
resetLocalStorage(true);
}
if (e.data.action === ChatAction.Chat && !!e.data.text && !isThinking && !isSendingMsg) {
sendChatMessage(e.data.text, e.data.data || null).then(() => {
/** @param {any} e */
async function handleNewChatAction(e) {
if (!isCreatingNewConv && !isThinking && !isSendingMsg) {
isCreatingNewConv = true;
const conv = await createNewConversation();
isCreatingNewConv = false;
if (conv && !!e.data.text) {
isLoading = true;
sendChatMessage(e.data.text, e.data.data || null, conv.id).then(() => {
redirectToNewConversation(conv);
isLoading = false;
openFrame();
}).catch(() => {
isLoading = false;
openFrame();
});
return;
}
});
});
}
}
/** @param {any} e */
function handleChatAction(e) {
if (!!e.data.text && !isThinking && !isSendingMsg) {
sendChatMessage(e.data.text, e.data.data || null).then(() => {
openFrame();
}).catch(() => {
openFrame();
});
}
}
function openFrame() {
if (window.location != window.parent.location) {
window.parent.postMessage({ action: "open" }, "*");
window.parent.postMessage({ action: ChatAction.Open }, "*");
}
}
Expand Down Expand Up @@ -749,7 +768,7 @@
}
});
} else {
window.parent.postMessage({ action: "close" }, "*");
window.parent.postMessage({ action: ChatAction.Close }, "*");
}
}
Expand Down

0 comments on commit 5ee7678

Please sign in to comment.