@axos-web-dev/shared-components 1.0.100-dev.62-0 → 1.0.100-dev.62-1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/Chatbot/Chat.js +0 -7
- package/dist/Chatbot/ChatWindow.js +9 -19
- package/dist/Chatbot/Chatbot.js +39 -14
- package/dist/Chatbot/ChatbotMessage.js +3 -1
- package/dist/Chatbot/store/chat.d.ts +3 -0
- package/dist/Chatbot/store/chat.js +10 -2
- package/dist/Chatbot/store/messages.d.ts +1 -0
- package/dist/Chatbot/store/messages.js +5 -2
- package/package.json +1 -1
package/dist/Chatbot/Chat.js
CHANGED
|
@@ -33,7 +33,6 @@ function Chat() {
|
|
|
33
33
|
let chatConnectedHandler;
|
|
34
34
|
async function initChat() {
|
|
35
35
|
const { Client, consoleLoggerHandler, Logger } = await import("@ujet/websdk-headless");
|
|
36
|
-
console.log("this", isMounted);
|
|
37
36
|
Logger.addHandler(consoleLoggerHandler);
|
|
38
37
|
clientRef.current = new Client({
|
|
39
38
|
companyId,
|
|
@@ -71,14 +70,11 @@ function Chat() {
|
|
|
71
70
|
menus?.menus[5].id
|
|
72
71
|
);
|
|
73
72
|
}
|
|
74
|
-
console.log("this");
|
|
75
73
|
identityHandler = (identity) => {
|
|
76
74
|
if (!isMounted.current) return;
|
|
77
75
|
console.log("identity:", identity);
|
|
78
76
|
};
|
|
79
77
|
messageHandler = (msg) => {
|
|
80
|
-
console.log("new message:", msg);
|
|
81
|
-
console.log("addMessage:", msg);
|
|
82
78
|
addMessage(msg);
|
|
83
79
|
};
|
|
84
80
|
chatUpdatedHandler = (chat) => {
|
|
@@ -87,13 +83,11 @@ function Chat() {
|
|
|
87
83
|
chatConnectedHandler = async () => {
|
|
88
84
|
console.log("connected");
|
|
89
85
|
const messagesFetched = await clientRef.current?.fetchMessages();
|
|
90
|
-
console.log("[messages]:", messagesFetched);
|
|
91
86
|
addMessages(messagesFetched || []);
|
|
92
87
|
};
|
|
93
88
|
memberJoinedHandler = (identity) => {
|
|
94
89
|
console.log("member joined:", identity);
|
|
95
90
|
};
|
|
96
|
-
console.log(isMounted.current);
|
|
97
91
|
clientRef.current?.on("authenticated", () => {
|
|
98
92
|
console.log("authenticated");
|
|
99
93
|
});
|
|
@@ -144,7 +138,6 @@ function Chat() {
|
|
|
144
138
|
const form = e.target;
|
|
145
139
|
const input = form.elements[0];
|
|
146
140
|
const message = input.value;
|
|
147
|
-
console.log("Sending message:", message);
|
|
148
141
|
try {
|
|
149
142
|
await clientRef.current?.sendTextMessage(message);
|
|
150
143
|
input.value = "";
|
|
@@ -37,7 +37,12 @@ const ChatWindow = ({
|
|
|
37
37
|
console.log("Preview typing: ", msg);
|
|
38
38
|
}
|
|
39
39
|
}) => {
|
|
40
|
-
const {
|
|
40
|
+
const {
|
|
41
|
+
showThankyouMessage,
|
|
42
|
+
displayThankyouMessage,
|
|
43
|
+
toggleThankyouMessage,
|
|
44
|
+
hasEscalated
|
|
45
|
+
} = useOpenChat();
|
|
41
46
|
const [mounted, setMounted] = React.useState(false);
|
|
42
47
|
const [menuOpen, setMenuOpen] = React.useState(false);
|
|
43
48
|
const [showEndChatDialog, setShowEndChatDialog] = React.useState(false);
|
|
@@ -89,24 +94,6 @@ const ChatWindow = ({
|
|
|
89
94
|
setInput("");
|
|
90
95
|
toggleThankyouMessage?.();
|
|
91
96
|
};
|
|
92
|
-
useEffect(() => {
|
|
93
|
-
const scalationStarted = messages.some(
|
|
94
|
-
(msg) => msg.$userType === "user" || ["escalationAccepted", "escalationStarted"].includes(
|
|
95
|
-
msg.event
|
|
96
|
-
)
|
|
97
|
-
);
|
|
98
|
-
if (!scalationStarted) {
|
|
99
|
-
return;
|
|
100
|
-
}
|
|
101
|
-
const sendPreview = () => {
|
|
102
|
-
const cleaned = cleanInput(input);
|
|
103
|
-
previewTyping(cleaned);
|
|
104
|
-
};
|
|
105
|
-
const intervalId = setInterval(sendPreview, 2e3);
|
|
106
|
-
return () => {
|
|
107
|
-
clearInterval(intervalId);
|
|
108
|
-
};
|
|
109
|
-
}, [input, messages, previewTyping]);
|
|
110
97
|
return /* @__PURE__ */ jsxs("div", { className: clsx(windowStyle, isOpen && windowOpenStyle), children: [
|
|
111
98
|
/* @__PURE__ */ jsxs("div", { className: windowBarStyle, children: [
|
|
112
99
|
/* @__PURE__ */ jsx("div", { className: clsx(left_bar_section) }),
|
|
@@ -475,6 +462,9 @@ const ChatWindow = ({
|
|
|
475
462
|
value: input,
|
|
476
463
|
onChange: (e) => setInput(e.target.value),
|
|
477
464
|
onKeyDown: (e) => {
|
|
465
|
+
if (hasEscalated) {
|
|
466
|
+
previewTyping(e.target?.value);
|
|
467
|
+
}
|
|
478
468
|
if (e.key === "Enter" && !e.shiftKey) {
|
|
479
469
|
e.preventDefault();
|
|
480
470
|
const cleaned = cleanInput(input);
|
package/dist/Chatbot/Chatbot.js
CHANGED
|
@@ -6,7 +6,7 @@ import { chatbotUFB, chatbotAXB } from "./Chatbot.css.js";
|
|
|
6
6
|
import { ChatWindow } from "./ChatWindow.js";
|
|
7
7
|
import { useOpenChat } from "./store/chat.js";
|
|
8
8
|
import { useMessages } from "./store/messages.js";
|
|
9
|
-
import { useRef, useState, useEffect
|
|
9
|
+
import { useRef, useState, useEffect } from "react";
|
|
10
10
|
import { useMount, useUnmount } from "react-use";
|
|
11
11
|
const Chatbot = ({
|
|
12
12
|
project = "axos",
|
|
@@ -14,7 +14,14 @@ const Chatbot = ({
|
|
|
14
14
|
menuOption = "Support Virtual Agent",
|
|
15
15
|
debug = false
|
|
16
16
|
}) => {
|
|
17
|
-
const {
|
|
17
|
+
const {
|
|
18
|
+
hasOpenedOnce,
|
|
19
|
+
toggle,
|
|
20
|
+
reset,
|
|
21
|
+
startEscalation,
|
|
22
|
+
hasEscalated,
|
|
23
|
+
endEscalation
|
|
24
|
+
} = useOpenChat();
|
|
18
25
|
const { addMessage, addMessages, clearMessages, messages } = useMessages();
|
|
19
26
|
const clientRef = useRef(null);
|
|
20
27
|
const menuRef = useRef(null);
|
|
@@ -90,19 +97,26 @@ const Chatbot = ({
|
|
|
90
97
|
};
|
|
91
98
|
const onChatMessageHandler = async (message) => {
|
|
92
99
|
console.log("Received message:", message);
|
|
93
|
-
|
|
100
|
+
if (["system", "virtual_agent", "user"].includes(message.$userType) && message.event === void 0) {
|
|
101
|
+
addMessage(message);
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
94
104
|
const eventName = message?.event;
|
|
95
105
|
switch (eventName) {
|
|
96
106
|
case "escalationAccepted":
|
|
97
107
|
setScalationStarted(true);
|
|
108
|
+
startEscalation?.();
|
|
109
|
+
addMessage(message);
|
|
98
110
|
return;
|
|
99
111
|
case "escalationStarted":
|
|
100
112
|
setScalationStarted(true);
|
|
113
|
+
startEscalation?.();
|
|
114
|
+
addMessage(message);
|
|
101
115
|
return;
|
|
102
116
|
default:
|
|
117
|
+
addMessage(message);
|
|
103
118
|
if (["end_user"].includes(message.$userType)) {
|
|
104
|
-
|
|
105
|
-
if (!scalationStarted) {
|
|
119
|
+
if (!hasEscalated) {
|
|
106
120
|
addMessage(typingMessage);
|
|
107
121
|
}
|
|
108
122
|
}
|
|
@@ -137,19 +151,25 @@ const Chatbot = ({
|
|
|
137
151
|
const onChatMemberLeftHandler = async (identity) => {
|
|
138
152
|
console.log("Chat member left:", identity);
|
|
139
153
|
};
|
|
154
|
+
const onChatMemberJoinedHandler = async (identity) => {
|
|
155
|
+
console.log("Chat member joined:", identity);
|
|
156
|
+
};
|
|
140
157
|
const onChatConnectedHandler = async () => {
|
|
141
158
|
setStatus("connected");
|
|
142
159
|
console.log("connected");
|
|
143
160
|
const messages2 = await clientRef.current?.fetchMessages();
|
|
144
161
|
if (messages2) {
|
|
145
162
|
addMessages(messages2);
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
msg.event
|
|
150
|
-
)
|
|
163
|
+
const hasEscalation = messages2.some(
|
|
164
|
+
(msg) => msg.$userType === "user" || ["escalationAccepted", "escalationStarted"].includes(
|
|
165
|
+
msg.event
|
|
151
166
|
)
|
|
152
167
|
);
|
|
168
|
+
console.log(hasEscalation);
|
|
169
|
+
if (hasEscalation) {
|
|
170
|
+
setScalationStarted(true);
|
|
171
|
+
startEscalation?.();
|
|
172
|
+
}
|
|
153
173
|
}
|
|
154
174
|
};
|
|
155
175
|
const registerEventHandlers = () => {
|
|
@@ -162,6 +182,7 @@ const Chatbot = ({
|
|
|
162
182
|
clientRef.current?.on("chat.timeout", onTimeoutHandler);
|
|
163
183
|
clientRef.current?.on("chat.ended", onEndedHandler);
|
|
164
184
|
clientRef.current?.on("chat.destroyed", onDestroyedHandler);
|
|
185
|
+
clientRef.current?.on("chat.memberJoined", onChatMemberJoinedHandler);
|
|
165
186
|
clientRef.current?.on("chat.memberLeft", onChatMemberLeftHandler);
|
|
166
187
|
clientRef?.current?.on("chat.connected", onChatConnectedHandler);
|
|
167
188
|
};
|
|
@@ -178,6 +199,7 @@ const Chatbot = ({
|
|
|
178
199
|
clientRef.current?.off("chat.ended", onEndedHandler);
|
|
179
200
|
clientRef.current?.off("chat.destroyed", onDestroyedHandler);
|
|
180
201
|
clientRef.current?.off("chat.memberLeft", onChatMemberLeftHandler);
|
|
202
|
+
clientRef.current?.off("chat.memberJoined", onChatMemberJoinedHandler);
|
|
181
203
|
clientRef.current?.off("chat.connected", onChatConnectedHandler);
|
|
182
204
|
};
|
|
183
205
|
const createClient = async () => {
|
|
@@ -256,7 +278,7 @@ const Chatbot = ({
|
|
|
256
278
|
};
|
|
257
279
|
const onSendMessage = async (msg) => {
|
|
258
280
|
console.log("Sending message:", msg);
|
|
259
|
-
clientRef.current?.sendTextMessage(msg);
|
|
281
|
+
await clientRef.current?.sendTextMessage(msg);
|
|
260
282
|
};
|
|
261
283
|
const onEndChat = async () => {
|
|
262
284
|
console.log("Ending chat");
|
|
@@ -267,6 +289,7 @@ const Chatbot = ({
|
|
|
267
289
|
chatRef.current = null;
|
|
268
290
|
console.log("Chat ended");
|
|
269
291
|
setHasStarted(false);
|
|
292
|
+
endEscalation?.();
|
|
270
293
|
}
|
|
271
294
|
};
|
|
272
295
|
const onEndAndStartNewChat = async () => {
|
|
@@ -295,6 +318,7 @@ const Chatbot = ({
|
|
|
295
318
|
setHasStarted(false);
|
|
296
319
|
setStatus("idle");
|
|
297
320
|
clearMessages();
|
|
321
|
+
endEscalation?.();
|
|
298
322
|
clientRef.current?.destroy();
|
|
299
323
|
clientRef.current?.destroyChat();
|
|
300
324
|
clientRef.current = null;
|
|
@@ -306,16 +330,17 @@ const Chatbot = ({
|
|
|
306
330
|
reset();
|
|
307
331
|
setHasStarted(false);
|
|
308
332
|
};
|
|
309
|
-
const onPreviewTyping =
|
|
333
|
+
const onPreviewTyping = async (msg) => {
|
|
310
334
|
console.log("Preview typing message:", msg);
|
|
311
335
|
try {
|
|
312
336
|
if (clientRef.current) {
|
|
313
|
-
|
|
337
|
+
chatRef.current?.startTyping();
|
|
338
|
+
await clientRef.current?.sendPreviewMessage(msg);
|
|
314
339
|
}
|
|
315
340
|
} catch (error) {
|
|
316
341
|
console.error("Error sending preview message:", error);
|
|
317
342
|
}
|
|
318
|
-
}
|
|
343
|
+
};
|
|
319
344
|
return /* @__PURE__ */ jsxs("div", { className: project === "ufb" ? chatbotUFB : chatbotAXB, children: [
|
|
320
345
|
/* @__PURE__ */ jsx(Bubble, { onClick: handleClick }),
|
|
321
346
|
/* @__PURE__ */ jsx(
|
|
@@ -10,6 +10,7 @@ import '../assets/themes/victorie.css';import '../assets/themes/ufb.css';import
|
|
|
10
10
|
/* empty css */
|
|
11
11
|
import clsx from "clsx";
|
|
12
12
|
import { shimmerText, notificationStyle, endChatButtonStyle, messageStyle, user_msg, agent_msg, inline_button_wrapper, inline_button } from "./ChatWindow.css.js";
|
|
13
|
+
import { useOpenChat } from "./store/chat.js";
|
|
13
14
|
function timeAgo(date) {
|
|
14
15
|
const seconds = Math.floor(((/* @__PURE__ */ new Date()).getTime() - date.getTime()) / 1e3);
|
|
15
16
|
if (seconds < 60) return "Just now";
|
|
@@ -27,6 +28,7 @@ const ChatbotMessage = ({
|
|
|
27
28
|
onEndChat,
|
|
28
29
|
onSend
|
|
29
30
|
}) => {
|
|
31
|
+
const { hasEscalated } = useOpenChat();
|
|
30
32
|
const [timeText, setTimeText] = useState(timeAgo(msg.$timestamp));
|
|
31
33
|
useEffect(() => {
|
|
32
34
|
const interval = setInterval(() => {
|
|
@@ -34,7 +36,7 @@ const ChatbotMessage = ({
|
|
|
34
36
|
}, 3e4);
|
|
35
37
|
return () => clearInterval(interval);
|
|
36
38
|
}, [msg.$timestamp]);
|
|
37
|
-
if (msg.$sid === "typing-1") {
|
|
39
|
+
if (msg.$sid === "typing-1" && !hasEscalated) {
|
|
38
40
|
return /* @__PURE__ */ jsx(
|
|
39
41
|
"div",
|
|
40
42
|
{
|
|
@@ -8,6 +8,9 @@ interface OpenChatState {
|
|
|
8
8
|
showThankyouMessage: boolean;
|
|
9
9
|
displayThankyouMessage: () => void;
|
|
10
10
|
toggleThankyouMessage?: () => void;
|
|
11
|
+
hasEscalated?: boolean;
|
|
12
|
+
startEscalation?: () => void;
|
|
13
|
+
endEscalation?: () => void;
|
|
11
14
|
}
|
|
12
15
|
export declare const useOpenChat: import('zustand').UseBoundStore<import('zustand').StoreApi<OpenChatState>>;
|
|
13
16
|
export {};
|
|
@@ -3,6 +3,7 @@ const useOpenChat = create((set, get) => ({
|
|
|
3
3
|
isOpen: false,
|
|
4
4
|
hasOpenedOnce: false,
|
|
5
5
|
showThankyouMessage: false,
|
|
6
|
+
hasEscalated: false,
|
|
6
7
|
toggle: () => set((state) => ({ isOpen: !state.isOpen, hasOpenedOnce: true })),
|
|
7
8
|
open: () => {
|
|
8
9
|
const alreadyOpened = get().hasOpenedOnce;
|
|
@@ -12,9 +13,16 @@ const useOpenChat = create((set, get) => ({
|
|
|
12
13
|
});
|
|
13
14
|
},
|
|
14
15
|
close: () => set({ isOpen: false }),
|
|
15
|
-
reset: () => set({
|
|
16
|
+
reset: () => set({
|
|
17
|
+
hasOpenedOnce: false,
|
|
18
|
+
isOpen: false,
|
|
19
|
+
showThankyouMessage: false,
|
|
20
|
+
hasEscalated: false
|
|
21
|
+
}),
|
|
16
22
|
displayThankyouMessage: () => set({ showThankyouMessage: true }),
|
|
17
|
-
toggleThankyouMessage: () => set((state) => ({ showThankyouMessage: !state.showThankyouMessage }))
|
|
23
|
+
toggleThankyouMessage: () => set((state) => ({ showThankyouMessage: !state.showThankyouMessage })),
|
|
24
|
+
startEscalation: () => set({ hasEscalated: true }),
|
|
25
|
+
endEscalation: () => set({ hasEscalated: false })
|
|
18
26
|
}));
|
|
19
27
|
export {
|
|
20
28
|
useOpenChat
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { create } from "zustand";
|
|
2
|
-
const useMessages = create((set) => ({
|
|
2
|
+
const useMessages = create((set, get) => ({
|
|
3
3
|
messages: [],
|
|
4
4
|
addMessage: (message) => set((state) => {
|
|
5
5
|
const cleaned = state.messages.filter((m) => m.$sid !== "typing-1");
|
|
@@ -11,7 +11,10 @@ const useMessages = create((set) => ({
|
|
|
11
11
|
)
|
|
12
12
|
})),
|
|
13
13
|
addMessages: (newMessages) => set((state) => ({ messages: [...state.messages, ...newMessages] })),
|
|
14
|
-
clearMessages: () => set({ messages: [] })
|
|
14
|
+
clearMessages: () => set({ messages: [] }),
|
|
15
|
+
isEscalated: get()?.messages?.some(
|
|
16
|
+
(msg) => ["escalationAccepted", "escalationStarted"].includes(msg.event)
|
|
17
|
+
)
|
|
15
18
|
}));
|
|
16
19
|
export {
|
|
17
20
|
useMessages
|
package/package.json
CHANGED