33 import { onMount } from ' svelte' ;
44 import { PUBLIC_LIVECHAT_HOST , PUBLIC_LIVECHAT_ENTRY_ICON } from ' $env/static/public' ;
55 import { getSettingDetail } from ' $lib/services/setting-service' ;
6- import { chatBotStore } from ' $lib/helpers/store' ;
76 import { CHAT_FRAME_ID } from ' $lib/helpers/constants' ;
87 import { ChatAction } from ' $lib/helpers/enums' ;
8+ import BubbleChat from ' ./BubbleChat.svelte' ;
99
1010 let chatUrl = PUBLIC_LIVECHAT_HOST ;
11+ let showChatBox = false ;
12+ let showBubbleMsg = false ;
13+ let receivedMsg = ' ' ;
1114
1215 onMount (async () => {
1316 const agentSettings = await getSettingDetail (" Agent" );
1720 // Handle event from iframe
1821 window .onmessage = async function (e ) {
1922 if (e .data .action == ChatAction .Close ) {
20- chatBotStore . set ({ showChatBox: false }) ;
23+ showChatBox = false ;
2124 } else if (e .data .action == ChatAction .Open ) {
22- chatBotStore .set ({ showChatBox: true });
25+ // showChatBox = true;
26+ } else if (e .data .action == ChatAction .ReceiveMsg && ! showChatBox) {
27+ receivedMsg = e .data ? .data ? .rich_content ? .message ? .text || e .data ? .data ? .text || ' ' ;
28+ showBubbleMsg = true ;
29+ wave ();
2330 }
2431 };
2532
2633 function openChatBox () {
27- chatBotStore .set ({ showChatBox: true });
34+ showChatBox = true ;
35+ receivedMsg = ' ' ;
36+ showBubbleMsg = false ;
37+ }
38+
39+ function closeBubbleMsg () {
40+ receivedMsg = ' ' ;
41+ showBubbleMsg = false ;
42+ }
43+
44+ function wave () {
45+ const elem = document .getElementById (' chatbot-icon' );
46+ if (elem) {
47+ elem .classList .add (' waving' );
48+ setTimeout (() => {
49+ elem .classList .remove (' waving' );
50+ }, 800 );
51+ }
2852 }
2953< / script>
3054
3155< div class = " chatbot-container fixed-bottom float-bottom-right" >
56+ {#if showBubbleMsg}
57+ < div transition: fade= {{ delay: 50 , duration: 200 }}>
58+ < BubbleChat text= {receivedMsg} close= {() => closeBubbleMsg ()} / >
59+ < / div>
60+ {/ if }
61+
3262 < iframe
3363 src= {chatUrl}
34- width ={` ${ $chatBotStore . showChatBox ? ' 380px' : ' 0px ' } ` }
35- height ={` ${ $chatBotStore . showChatBox ? ' 650px' : ' 0px ' } ` }
36- class ={` border border-2 rounded-3 m-3 float-end ${$chatBotStore . showChatBox ? ' chat-iframe' : ' ' } ` }
64+ width= {' 380px' }
65+ height= {' 650px' }
66+ class = {` border border-2 rounded-3 m-3 float-end ${ showChatBox ? ' chat-iframe' : ' hide ' } ` }
3767 title= " live chat"
3868 id= {CHAT_FRAME_ID }
3969 / >
4070
41- {#if ! $chatBotStore . showChatBox }
71+ {#if ! showChatBox}
4272 < div
73+ id= " chatbot-icon"
4374 class = " chatbot-icon mb-3 float-end wave-effect"
4475 transition: fade= {{ delay: 50 , duration: 200 }}
4576 >
46- <button class ="btn btn-transparent" on:click ={() => openChatBox ()}>
77+ < button class = " btn btn-transparent chat-icon-btn " on: click= {() => openChatBox ()}>
4778 < img alt= " live chat" class = " avatar-md rounded-circle" src= {PUBLIC_LIVECHAT_ENTRY_ICON } / >
4879 < / button>
4980 < / div>
5889 perspective: 1000px ;
5990 }
6091
92+ .waving {
93+ animation: wave 0 .82s cubic- bezier (.36 ,.07 ,.19 ,.97 ) both;
94+ transform: translate3d (0 , 0 , 0 );
95+ backface- visibility: hidden;
96+ perspective: 1000px ;
97+ }
98+
6199 @keyframes wave {
62100 10 % , 90 % {
63101 transform: translate3d (- 1px , 0 , 0 );
91129 display: flex;
92130 justify- content: flex- end;
93131 }
132+
133+ .chat - icon- btn {
134+ padding- top: 0px ;
135+ }
94136< / style>
0 commit comments