pager-widget 1.0.3 → 1.0.4

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.
Files changed (71) hide show
  1. package/dist/index.css +1 -1
  2. package/dist/index.js +4 -4
  3. package/dist/react-parcel.9f367d21.css +2 -18
  4. package/dist/react-parcel.9f367d21.css.map +1 -1
  5. package/dist/react-parcel.js +119 -214
  6. package/dist/react-parcel.js.map +1 -1
  7. package/out/AgentFeedbackButtonWrapper.js +15 -0
  8. package/out/App.js +28 -0
  9. package/out/BotMain.js +91 -0
  10. package/out/ButtonsWrapper.js +29 -0
  11. package/out/ChatPage.js +9 -0
  12. package/out/ChatPageContent.js +131 -0
  13. package/out/ChatPageHeader.js +267 -0
  14. package/out/ChatPageInput.js +29 -0
  15. package/out/ChatWidget.js +17 -0
  16. package/out/Consent.js +66 -0
  17. package/out/CountryDropdown.js +31 -0
  18. package/out/Form.js +238 -0
  19. package/out/HomePage.js +11 -0
  20. package/out/HomePageContent.js +32 -0
  21. package/out/HomePageFooter.js +87 -0
  22. package/out/HomePageHeader.js +66 -0
  23. package/out/InputField.js +80 -0
  24. package/out/Markdown.js +126 -0
  25. package/out/Message.js +197 -0
  26. package/out/MessagesT.js +2 -0
  27. package/out/NewRecentMessagePage.js +525 -0
  28. package/out/NewRecentMessageTab.js +23 -0
  29. package/out/ReceivedMessage.js +211 -0
  30. package/out/RecentMessageDetail.js +178 -0
  31. package/out/RecentMessageList.js +112 -0
  32. package/out/RecentMessageListCard.js +20 -0
  33. package/out/RecentMessageListHeader.js +156 -0
  34. package/out/RecentMessagePage.js +183 -0
  35. package/out/RecentMessageTab.js +35 -0
  36. package/out/WidgetMessage.js +14 -0
  37. package/out/WidgetToggleButton.js +191 -0
  38. package/out/WidgetType.js +2 -0
  39. package/out/assets/icons/pikaicons-react.js +17475 -0
  40. package/out/components/ConsentForm.js +7 -0
  41. package/out/components/CustomTooltip.js +34 -0
  42. package/out/hooks/useChatHistoryExists.js +32 -0
  43. package/out/hooks/useChatHistoryListExist.js +81 -0
  44. package/out/hooks/useChatScroll.js +14 -0
  45. package/out/hooks/useCurrentConversationExists.js +88 -0
  46. package/out/hooks/useInitialMessage.js +33 -0
  47. package/out/hooks/useMessage.js +230 -0
  48. package/out/hooks/useMessageReceiver.js +183 -0
  49. package/out/hooks/usePopupAnimation.js +20 -0
  50. package/out/hooks/useSocket.js +19 -0
  51. package/out/hooks/useSocketContext.js +81 -0
  52. package/out/hooks/useWidgetDimension.js +41 -0
  53. package/out/hooks/useWidgetToggle.js +22 -0
  54. package/out/index.js +31 -0
  55. package/out/markdown/CodeCopy.js +22 -0
  56. package/out/markdown/CodeHighlight.js +58 -0
  57. package/out/markdown/PagerMarkdown.js +36 -0
  58. package/out/markdown/pager_md.js +152 -0
  59. package/out/onscreenNotificationPopup.js +23 -0
  60. package/out/socket.js +11 -0
  61. package/out/utils/checkConversationExit.js +24 -0
  62. package/out/utils/convertTime.js +43 -0
  63. package/out/utils/sendInitialMessage.js +82 -0
  64. package/out/widget.js +21 -0
  65. package/out/widgetConfigStore.js +205 -0
  66. package/out/widgetConfigStore2.js +569 -0
  67. package/out/widgetControlstore.js +23 -0
  68. package/out/widgetStateHandler.js +106 -0
  69. package/out/widgetStore.js +171 -0
  70. package/out/widgetUserStore.js +47 -0
  71. package/package.json +1 -1
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const jsx_runtime_1 = require("react/jsx-runtime");
4
+ const ConsentForm = () => {
5
+ return ((0, jsx_runtime_1.jsx)("div", { className: "p-6", children: (0, jsx_runtime_1.jsx)("div", { className: "max-w-md mx-auto bg-zinc-100 rounded-lg p-6", children: (0, jsx_runtime_1.jsxs)("form", { className: "space-y-4", children: [(0, jsx_runtime_1.jsxs)("div", { children: [(0, jsx_runtime_1.jsx)("label", { htmlFor: "firstName", className: "block text-sm font-medium text-gray-700 mb-1", children: "First name" }), (0, jsx_runtime_1.jsx)("input", { type: "text", id: "firstName", className: "w-full px-4 py-2 h-9 bg-white text-sm rounded-lg border border-gray-300 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent", placeholder: "Enter first name" })] }), (0, jsx_runtime_1.jsxs)("div", { children: [(0, jsx_runtime_1.jsx)("label", { htmlFor: "lastName", className: "block text-sm font-medium text-gray-700 mb-1", children: "Last name" }), (0, jsx_runtime_1.jsx)("input", { type: "text", id: "lastName", className: "w-full px-4 py-2 h-9 bg-white text-sm rounded-lg border border-gray-300 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent", placeholder: "Enter last name" })] }), (0, jsx_runtime_1.jsxs)("div", { children: [(0, jsx_runtime_1.jsx)("label", { htmlFor: "email", className: "block text-sm font-medium text-gray-700 mb-1", children: "Email" }), (0, jsx_runtime_1.jsxs)("div", { className: "relative cursor-pointer", children: [(0, jsx_runtime_1.jsx)("input", { type: "email", id: "email", className: "w-full pl-10 h-9 bg-white text-sm pr-4 py-2 rounded-lg border border-gray-300 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent", placeholder: "Enter email" }), (0, jsx_runtime_1.jsx)("svg", { className: "absolute left-3 top-2.5 h-5 w-5 text-gray-400", fill: "none", stroke: "currentColor", viewBox: "0 0 24 24", xmlns: "http://www.w3.org/2000/svg", children: (0, jsx_runtime_1.jsx)("path", { strokeLinecap: "round", strokeLinejoin: "round", strokeWidth: "2", d: "M3 8l7.89 5.26a2 2 0 002.22 0L21 8M5 19h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v10a2 2 0 002 2z" }) })] })] }), (0, jsx_runtime_1.jsxs)("div", { children: [(0, jsx_runtime_1.jsx)("label", { htmlFor: "phone", className: "block text-sm font-medium text-gray-700 mb-1", children: "Phone number" }), (0, jsx_runtime_1.jsxs)("div", { className: "flex focus:ring-blue-500 focus:outline-none focus:ring-2 focus:border-transparent", children: [(0, jsx_runtime_1.jsxs)("div", { className: "flex items-center px-4 border border-r-0 border-gray-300 rounded-l-lg bg-white h-9 text-sm whitespace-nowrap", children: [(0, jsx_runtime_1.jsx)("span", { className: "text-sm text-gray-600", children: "US +1" }), (0, jsx_runtime_1.jsx)("svg", { className: "w-4 h-4 ml-2 text-gray-400", fill: "none", stroke: "currentColor", viewBox: "0 0 24 24", xmlns: "http://www.w3.org/2000/svg", children: (0, jsx_runtime_1.jsx)("path", { strokeLinecap: "round", strokeLinejoin: "round", strokeWidth: "2", d: "M19 9l-7 7-7-7" }) })] }), (0, jsx_runtime_1.jsx)("input", { type: "tel", id: "phone", className: "flex-1 px-4 py-2 h-9 text-sm rounded-r-lg border bg-white border-gray-300", placeholder: "Enter phone number" })] })] }), (0, jsx_runtime_1.jsxs)("div", { children: [(0, jsx_runtime_1.jsx)("label", { htmlFor: "country", className: "block text-sm font-medium text-gray-700 mb-1", children: "Country" }), (0, jsx_runtime_1.jsxs)("div", { className: "relative cursor-pointer", children: [(0, jsx_runtime_1.jsxs)("select", { id: "country", className: "w-full px-4 py-2 h-9 rounded-lg border text-sm border-gray-300 bg-white focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent appearance-none", children: [(0, jsx_runtime_1.jsx)("option", { value: "usa", children: "USA" }), (0, jsx_runtime_1.jsx)("option", { value: "canada", children: "Canada" }), (0, jsx_runtime_1.jsx)("option", { value: "uk", children: "United Kingdom" })] }), (0, jsx_runtime_1.jsx)("svg", { className: "absolute right-3 top-2.5 h-5 w-5 text-gray-400 pointer-events-none", fill: "none", stroke: "currentColor", viewBox: "0 0 24 24", xmlns: "http://www.w3.org/2000/svg", children: (0, jsx_runtime_1.jsx)("path", { strokeLinecap: "round", strokeLinejoin: "round", strokeWidth: "2", d: "M19 9l-7 7-7-7" }) })] })] }), (0, jsx_runtime_1.jsx)("button", { type: "submit", className: "w-full cursor-pointer text-sm bg-[#4b2fff] hover:bg-[#4b2fff]/90 text-white font-medium py-2 px-4 rounded-lg transition-colors", children: "Submit" })] }) }) }));
6
+ };
7
+ exports.default = ConsentForm;
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CustomTooltip = void 0;
4
+ const jsx_runtime_1 = require("react/jsx-runtime");
5
+ const react_1 = require("react");
6
+ require("./CustomTooltip.css");
7
+ const CustomTooltip = ({ children, content, position = 'top', className = '', }) => {
8
+ const [visible, setVisible] = (0, react_1.useState)(false);
9
+ const positionClasses = {
10
+ top: 'bottom-full left-1/2 -translate-x-1/2 mb-2',
11
+ bottom: 'top-full left-1/2 -translate-x-1/2 mt-2',
12
+ left: 'right-full top-1/2 -translate-y-1/2 mr-2',
13
+ right: 'left-full top-1/2 -translate-y-1/2 ml-2',
14
+ };
15
+ const arrowPosition = {
16
+ top: 'left-1/2 -bottom-1 translate-x-[-50%]',
17
+ bottom: 'left-1/2 -top-1 translate-x-[-50%]',
18
+ left: '-right-1 top-1/2 translate-y-[-50%]',
19
+ right: '-left-1 top-1/2 translate-y-[-50%]',
20
+ };
21
+ const transformOrigin = position === 'top'
22
+ ? 'bottom center'
23
+ : position === 'bottom'
24
+ ? 'top center'
25
+ : position === 'left'
26
+ ? 'center right'
27
+ : 'center left';
28
+ return ((0, jsx_runtime_1.jsxs)("div", { className: `relative inline-block ${className}`, children: [(0, jsx_runtime_1.jsx)("div", { onMouseEnter: () => setVisible(true), onMouseLeave: () => setVisible(false), onFocus: () => setVisible(true), onBlur: () => setVisible(false), className: "inline-block", children: children }), (0, jsx_runtime_1.jsxs)("div", { role: "tooltip", "aria-hidden": !visible, className: `custom-tooltip absolute px-2 py-1 leading-[18px] z-50 text-xs text-white bg-black rounded-md shadow-md whitespace-nowrap
29
+ ${positionClasses[position]}
30
+ ${visible ? 'custom-tooltip--visible' : ''}
31
+ `, style: { transformOrigin }, children: [content, (0, jsx_runtime_1.jsx)("div", { className: `absolute w-2 h-2 bg-black rotate-45 ${arrowPosition[position]}` })] })] }));
32
+ };
33
+ exports.CustomTooltip = CustomTooltip;
34
+ exports.default = exports.CustomTooltip;
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useChatHistoryExists = void 0;
4
+ const widgetConfigStore2_1 = require("../widgetConfigStore2");
5
+ const useChatHistoryExists = () => {
6
+ const widget = (0, widgetConfigStore2_1.useWidgetData)();
7
+ const { status } = widget;
8
+ let hasRecentChat = false;
9
+ let recentChat = null;
10
+ if (status !== 'NOT_STARTED' &&
11
+ status !== 'WIDGET_LOADING' &&
12
+ status !== 'INVALID_STATE') {
13
+ const stored = localStorage.getItem(`${widget?.data?.bot_id}::${widget?.data?.id}`);
14
+ if (stored) {
15
+ try {
16
+ const parsed = JSON.parse(stored);
17
+ if (parsed &&
18
+ typeof parsed === 'object' &&
19
+ parsed[widget?.data?.id]) {
20
+ hasRecentChat = true;
21
+ recentChat = parsed;
22
+ console.log(recentChat, 'recent chats');
23
+ }
24
+ }
25
+ catch (e) {
26
+ console.error('Failed to parse recent chat from localStorage', e);
27
+ }
28
+ }
29
+ }
30
+ return { hasRecentChat, recentChat };
31
+ };
32
+ exports.useChatHistoryExists = useChatHistoryExists;
@@ -0,0 +1,81 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useGetSingleConversation = exports.useChatHistoryListExists = void 0;
4
+ const widgetConfigStore2_1 = require("../widgetConfigStore2");
5
+ const useChatHistoryListExists = () => {
6
+ const widget = (0, widgetConfigStore2_1.useWidgetData)();
7
+ const { status } = widget;
8
+ let hasRecentChatList = false;
9
+ let recentChatList = null;
10
+ if (status !== 'NOT_STARTED' &&
11
+ status !== 'WIDGET_LOADING' &&
12
+ status !== 'INVALID_STATE') {
13
+ const stored = localStorage.getItem(`CONVERSATION_LIST::${widget.data.id}`);
14
+ if (stored) {
15
+ try {
16
+ const parsed = JSON.parse(stored);
17
+ if (parsed) {
18
+ hasRecentChatList = true;
19
+ recentChatList = parsed;
20
+ }
21
+ }
22
+ catch (e) {
23
+ console.error('Failed to parse recent chat list from localStorage', e);
24
+ }
25
+ }
26
+ }
27
+ return { hasRecentChatList, recentChatList };
28
+ };
29
+ exports.useChatHistoryListExists = useChatHistoryListExists;
30
+ // export const useChatHistoryListExists = () => {
31
+ // const widget = useWidgetData()
32
+ // const { status } = widget
33
+ // let hasRecentChatList = false
34
+ // let recentChatList = null
35
+ // if (
36
+ // status !== 'NOT_STARTED' &&
37
+ // status !== 'WIDGET_LOADING' &&
38
+ // status !== 'INVALID_STATE'
39
+ // ) {
40
+ // const stored = localStorage.getItem(
41
+ // `RecentList::${widget?.data?.bot_id}::${widget?.data?.id}`
42
+ // )
43
+ // if (stored) {
44
+ // try {
45
+ // const parsed = JSON.parse(stored)
46
+ // if (parsed) {
47
+ // hasRecentChatList = true
48
+ // recentChatList = parsed
49
+ // }
50
+ // } catch (e) {
51
+ // console.error(
52
+ // 'Failed to parse recent chat list from localStorage',
53
+ // e
54
+ // )
55
+ // }
56
+ // }
57
+ // }
58
+ // return { hasRecentChatList, recentChatList }
59
+ // }
60
+ const useGetSingleConversation = (conversation_id) => {
61
+ const widget = (0, widgetConfigStore2_1.useWidgetData)();
62
+ let chatDetail;
63
+ const { status } = widget;
64
+ if (status !== 'NOT_STARTED' &&
65
+ status !== 'WIDGET_LOADING' &&
66
+ status !== 'INVALID_STATE') {
67
+ const stored = localStorage.getItem(`RecentList::${widget?.data?.bot_id}::${widget?.data?.id}`);
68
+ if (stored) {
69
+ try {
70
+ const parsed = JSON.parse(stored);
71
+ console.log('parsed::', parsed[conversation_id]);
72
+ chatDetail = parsed[conversation_id];
73
+ }
74
+ catch (e) {
75
+ console.error('Failed to parse recent chat list from localStorage', e);
76
+ }
77
+ }
78
+ }
79
+ return { chatDetail };
80
+ };
81
+ exports.useGetSingleConversation = useGetSingleConversation;
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useChatScroll = void 0;
4
+ const react_1 = require("react");
5
+ const useChatScroll = (dep) => {
6
+ const ref = (0, react_1.useRef)(null);
7
+ (0, react_1.useEffect)(() => {
8
+ if (ref.current) {
9
+ ref.current.scrollTop = ref.current.scrollHeight;
10
+ }
11
+ }, [dep]);
12
+ return ref;
13
+ };
14
+ exports.useChatScroll = useChatScroll;
@@ -0,0 +1,88 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useSelectedConversation = exports.useCurrentConversationExist = void 0;
4
+ const widgetConfigStore2_js_1 = require("../widgetConfigStore2.js");
5
+ const useCurrentConversationExist = (widget_id) => {
6
+ let hasRecentConversation = false;
7
+ let recentConversation = null;
8
+ // ✅ Guard clause to prevent undefined access
9
+ // if (!widget?.data?.id || !widget?.data?.bot_id) {
10
+ // console.warn('Widget data not available yet')
11
+ // return { hasRecentConversation, recentConversation }
12
+ // }
13
+ const stored = localStorage.getItem(`CONVERSATION::${widget_id}`);
14
+ if (stored) {
15
+ try {
16
+ const parsed = JSON.parse(stored);
17
+ if (parsed && typeof parsed === 'object') {
18
+ console.log(parsed, 'recent message here');
19
+ hasRecentConversation = parsed.events.length > 1;
20
+ recentConversation = parsed;
21
+ console.log(recentConversation, 'recent conversation from hook');
22
+ }
23
+ }
24
+ catch (e) {
25
+ console.error('Failed to parse recent chat from localStorage', e);
26
+ }
27
+ }
28
+ return { hasRecentConversation, recentConversation };
29
+ };
30
+ exports.useCurrentConversationExist = useCurrentConversationExist;
31
+ const useSelectedConversation = () => {
32
+ let selectedConversation = null;
33
+ let selectedIndex = null;
34
+ const widget = (0, widgetConfigStore2_js_1.useWidgetData)();
35
+ const widgetData = (0, widgetConfigStore2_js_1.widgetHasData)(widget) ? widget.data : null;
36
+ if (!widgetData?.id) {
37
+ console.warn('Widget data not available yet');
38
+ return {
39
+ selectedIndex,
40
+ recentConversation: selectedConversation,
41
+ };
42
+ }
43
+ const stored = localStorage.getItem(`CONVERSATION_LIST::${widgetData.id}`);
44
+ const selectedIndexRaw = localStorage.getItem('SELECTED_INDEX');
45
+ if (stored && selectedIndexRaw !== null) {
46
+ try {
47
+ const parsed = JSON.parse(stored);
48
+ const index = parseInt(selectedIndexRaw, 10);
49
+ if (!isNaN(index) && parsed[index]) {
50
+ selectedIndex = index;
51
+ selectedConversation = parsed[index];
52
+ console.log('Selected conversation:', selectedConversation);
53
+ }
54
+ }
55
+ catch (e) {
56
+ console.error('Failed to parse CONVERSATION_LIST from localStorage:', e);
57
+ }
58
+ }
59
+ return {
60
+ selectedIndex,
61
+ recentConversation: selectedConversation,
62
+ };
63
+ };
64
+ exports.useSelectedConversation = useSelectedConversation;
65
+ // export const useSelectedConversation = () => {
66
+ // let selectedConversation: LocalEventStore | null = null
67
+ // let selectedIndex: number | null = null
68
+ // const widget = useWidgetData()
69
+ // const stored = localStorage.getItem(`CONVERSATION_LIST::${widget?.data.id}`)
70
+ // const selectedIndexRaw = localStorage.getItem('SELECTED_INDEX')
71
+ // if (stored && selectedIndexRaw !== null) {
72
+ // try {
73
+ // const parsed: LocalEventStore[] = JSON.parse(stored)
74
+ // const index = parseInt(selectedIndexRaw, 10)
75
+ // if (!isNaN(index) && parsed[index]) {
76
+ // selectedIndex = index
77
+ // selectedConversation = parsed[index]
78
+ // console.log('Selected conversation:', selectedConversation)
79
+ // }
80
+ // } catch (e) {
81
+ // console.error('Failed to parse CONVERSATION_LIST from localStorage:', e)
82
+ // }
83
+ // }
84
+ // return {
85
+ // selectedIndex,
86
+ // recentConversation: selectedConversation,
87
+ // }
88
+ // }
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useInitialMessage = void 0;
4
+ /* eslint-disable @typescript-eslint/no-explicit-any */
5
+ const react_1 = require("react");
6
+ const sendInitialMessage_1 = require("../utils/sendInitialMessage");
7
+ const widget_1 = require("../widget");
8
+ const useInitialMessage = ({ socket, widget_id, connected, context, continuseConversation, setIsTyping, setMessages,
9
+ // userDetails
10
+ }) => {
11
+ const initialMessage = (0, react_1.useContext)(widget_1.MyContext);
12
+ console.log(initialMessage, 'initialMessage');
13
+ const msg = initialMessage?.initialUserMessage || '';
14
+ const [sent,
15
+ // setSent
16
+ ] = (0, react_1.useState)(false);
17
+ console.log(continuseConversation, 'continuesConversation');
18
+ (0, react_1.useEffect)(() => {
19
+ if (!sent && connected && !continuseConversation) {
20
+ setIsTyping(true);
21
+ (0, sendInitialMessage_1.sendInitialMessage)({
22
+ socket,
23
+ widget_id,
24
+ context,
25
+ msg,
26
+ setMessages,
27
+ });
28
+ // widgetStore.send({ type: 'handleExitChatState', value: false })
29
+ initialMessage?.setInitialUserMessage('');
30
+ }
31
+ }, [connected]);
32
+ };
33
+ exports.useInitialMessage = useInitialMessage;
@@ -0,0 +1,230 @@
1
+ "use strict";
2
+ /* eslint-disable @typescript-eslint/no-explicit-any */
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.useMessages = void 0;
5
+ const react_1 = require("react");
6
+ const useInitialMessage_1 = require("./useInitialMessage");
7
+ // import { useSocket } from './useSocket'
8
+ const useMessageReceiver_1 = require("./useMessageReceiver");
9
+ const widgetConfigStore2_1 = require("../widgetConfigStore2");
10
+ const widgetStateHandler_1 = require("../widgetStateHandler");
11
+ const useSocketContext_1 = require("./useSocketContext");
12
+ const useMessages = ({ widget_id,
13
+ // endpoint,
14
+ context, userDetails, recentConversation, }) => {
15
+ const widget = (0, widgetConfigStore2_1.useWidgetData)();
16
+ console.log('DEBUG :: recentConversation in widget', widget);
17
+ const [messages, setMessages] = (0, react_1.useState)(() => recentConversation);
18
+ const [typing, setIsTyping] = (0, react_1.useState)(false);
19
+ const [agentStatus, setAgentStatus] = (0, react_1.useState)(null);
20
+ const authToken = undefined;
21
+ const [currentToken] = (0, react_1.useState)(authToken);
22
+ // const { socket, connected } = useSocket({ endpoint })
23
+ const { socket, connected } = (0, useSocketContext_1.useSocketV2)();
24
+ console.log('DEBUG :: recentConversation in useMessages', recentConversation, messages);
25
+ const user1Details = {
26
+ SYS_FirstName: 'sample',
27
+ SYS_LastName: 'B',
28
+ SYS_UserEmail: 'example@gmail.com',
29
+ SYS_Location: 'India',
30
+ SYS_Timezone: 'Asia/Kolkata',
31
+ };
32
+ const sendMessageToLS = (widget, messages) => {
33
+ console.log('send message to ls called');
34
+ const widget_id = widget.data.id;
35
+ const bot_id = widget.data.bot_id;
36
+ const conversationId = messages.find((msg) => msg.message.system.conversationId)?.message.system.conversationId;
37
+ if (!conversationId)
38
+ return;
39
+ // Parse existing recent list data from localStorage
40
+ const recentListKey = `RecentList::${bot_id}::${widget_id}`;
41
+ const existingData = localStorage.getItem(recentListKey);
42
+ let parsedData = {};
43
+ if (existingData) {
44
+ try {
45
+ parsedData = JSON.parse(existingData);
46
+ }
47
+ catch (error) {
48
+ console.error(`Failed to parse RecentList for widget_id: ${widget_id}`, error);
49
+ }
50
+ }
51
+ // Add or update conversation
52
+ parsedData[conversationId] = messages;
53
+ // Save the updated RecentList
54
+ localStorage.setItem(recentListKey, JSON.stringify(parsedData));
55
+ // Optional: Save a separate payload per widget (if needed elsewhere)
56
+ const payload = { [widget_id]: messages };
57
+ localStorage.setItem(`${bot_id}::${widget_id}`, JSON.stringify(payload));
58
+ console.log('Updated RecentList in localStorage:', recentListKey, parsedData);
59
+ // console.log(
60
+ // 'to local storage',
61
+ // widget.data.bot_id,
62
+ // widget.data.id,
63
+ // messages
64
+ // )
65
+ // const widget_id = widget.data.id // You might be missing this variable declaration
66
+ // const payload = { [widget_id]: messages } // use computed property name
67
+ // const conversationId = messages.find(
68
+ // (messageResponse) => messageResponse.message.system.conversationId
69
+ // )?.message.system.conversationId as string
70
+ // if (!conversationId) return
71
+ // const existingData = localStorage.getItem(widget_id)
72
+ // let parsedData: Record<string, MessagResponseT[]> = {}
73
+ // if (existingData) {
74
+ // try {
75
+ // parsedData = JSON.parse(existingData)
76
+ // } catch (error) {
77
+ // console.error(
78
+ // `Failed to parse data for widget_id: ${widget_id}`,
79
+ // error
80
+ // )
81
+ // }
82
+ // }
83
+ // // Update/add the conversation with new messages
84
+ // parsedData[conversationId] = messages
85
+ // // Save back to localStorage
86
+ // localStorage.setItem(
87
+ // `${widget.data.bot_id}::${widget.data.id}`,
88
+ // JSON.stringify(payload)
89
+ // )
90
+ // // localStorage.setItem(widget_id, JSON.stringify(parsedData))
91
+ // localStorage.setItem(
92
+ // `RecentList::${widget?.data?.bot_id}::${widget?.data?.id}`,
93
+ // JSON.stringify(parsedData)
94
+ // )
95
+ // // localStorage.setItem(`${widget_id}`, JSON.stringify(recentMessagePayload))
96
+ };
97
+ const getUpdatedContext = (messages, currentContext, { SYS_FirstName, SYS_LastName, SYS_UserEmail }, file = null) => {
98
+ if (!messages.length) {
99
+ const userDetails = {
100
+ first_name: SYS_FirstName,
101
+ last_name: SYS_LastName,
102
+ email: SYS_UserEmail,
103
+ };
104
+ currentContext.context = {
105
+ ...currentContext.context,
106
+ SYS_FirstName,
107
+ SYS_LastName,
108
+ SYS_UserEmail,
109
+ };
110
+ currentContext.system = {
111
+ user: { ...currentContext.system?.user, ...userDetails },
112
+ };
113
+ return currentContext;
114
+ }
115
+ const message = messages[messages.length - 1].message;
116
+ return {
117
+ context: {
118
+ ...message.context,
119
+ SYS_UPLOADED_FILE: file,
120
+ SYS_FirstName,
121
+ SYS_LastName,
122
+ SYS_UserEmail,
123
+ },
124
+ system: message.system,
125
+ };
126
+ };
127
+ const message = (input, currentContext, messages, widget_id, authToken, widget, userDetails, for_event, file = null) => {
128
+ console.log('inside message', messages);
129
+ return {
130
+ authToken,
131
+ widget_id,
132
+ context: {
133
+ ...getUpdatedContext([...messages], currentContext, userDetails, file),
134
+ input: { type: 'text', value: input },
135
+ for_event: for_event ? for_event : null, // will use it for feedback
136
+ SYS_UPLOADED_FILE: file,
137
+ output: [],
138
+ },
139
+ };
140
+ };
141
+ const sendEvent = (event) => {
142
+ console.log('event', event);
143
+ const msg = message('', context, messages, widget_id, currentToken, widget, user1Details);
144
+ console.log('event la message', message);
145
+ if (!socket) {
146
+ return;
147
+ }
148
+ socket.emit(event, msg);
149
+ };
150
+ const sendMessage = (input) => {
151
+ console.log('DEBUG :: inside send message', input, context, messages);
152
+ setIsTyping(true);
153
+ // console.log("my message function",message(input,context,messages,widget_id,authToken,widget.data,user1Details))
154
+ const msg = message(input, context, messages, widget_id, authToken, widget, user1Details);
155
+ if (!socket) {
156
+ return;
157
+ }
158
+ socket.emit('message', msg);
159
+ const sentMessage = {
160
+ type: 'sent',
161
+ message: msg.context,
162
+ time: new Date(),
163
+ };
164
+ console.log('message array before sentMessage', messages);
165
+ console.log('sentMessage', sentMessage, [...messages, sentMessage]);
166
+ // setMessages(prevMessages => [...prevMessages, sentMessage])
167
+ setMessages([...messages, sentMessage]);
168
+ sendMessageToLS(widget, messages);
169
+ console.log('before set your local', messages);
170
+ console.log('DDD::', [...messages, sentMessage]);
171
+ (0, widgetStateHandler_1.setWidgetLocalState)({
172
+ widget_id: widget?.data.id,
173
+ user_state: 'ALLOW_MESSAGE',
174
+ events: [sentMessage],
175
+ });
176
+ console.log('after sentMessages messages', messages);
177
+ };
178
+ const sendFeedback = (input, for_event) => {
179
+ console.log('send feed back', input, for_event);
180
+ setIsTyping(true);
181
+ const msg = message(input, context, messages, widget_id, authToken, widget, user1Details, for_event);
182
+ if (!socket) {
183
+ return;
184
+ }
185
+ socket.emit('feedback', msg);
186
+ };
187
+ const restart = () => {
188
+ setMessages([]);
189
+ localStorage.setItem('messages', JSON.stringify([]));
190
+ sendEvent('restart');
191
+ setIsTyping(true);
192
+ };
193
+ const exit = () => {
194
+ sendEvent('exit');
195
+ };
196
+ // if (widget.status === 'ALLOW_MESSAGE') {
197
+ (0, useInitialMessage_1.useInitialMessage)({
198
+ widget_id,
199
+ socket,
200
+ connected,
201
+ context: context,
202
+ userDetails,
203
+ // continuseConversation: false,
204
+ continuseConversation: recentConversation.length > 0,
205
+ setIsTyping,
206
+ messages,
207
+ setMessages,
208
+ });
209
+ (0, useMessageReceiver_1.useMessageReceiver)({
210
+ widget_id,
211
+ socket,
212
+ messages,
213
+ setMessages,
214
+ setIsTyping,
215
+ setAgentStatus,
216
+ });
217
+ // }
218
+ return {
219
+ typing,
220
+ connected,
221
+ messages,
222
+ setMessages,
223
+ sendMessage,
224
+ sendFeedback,
225
+ restart,
226
+ exit,
227
+ agentStatus,
228
+ };
229
+ };
230
+ exports.useMessages = useMessages;