@microsoft/omnichannel-chat-widget 0.1.0-main.fb426ed → 0.1.0-main.fdf1fdf

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 (117) hide show
  1. package/README.md +32 -0
  2. package/lib/cjs/common/Constants.js +18 -2
  3. package/lib/cjs/common/telemetry/TelemetryConstants.js +21 -3
  4. package/lib/cjs/common/utils.js +75 -2
  5. package/lib/cjs/components/chatbuttonstateful/ChatButtonStateful.js +19 -3
  6. package/lib/cjs/components/confirmationpanestateful/ConfirmationPaneStateful.js +0 -1
  7. package/lib/cjs/components/footerstateful/downloadtranscriptstateful/DownloadTranscriptStateful.js +10 -1
  8. package/lib/cjs/components/headerstateful/HeaderStateful.js +4 -3
  9. package/lib/cjs/components/livechatwidget/common/ActivityStreamHandler.js +44 -0
  10. package/lib/cjs/components/livechatwidget/common/ActivitySubscriber/DefaultActivitySubscriber.js +23 -0
  11. package/lib/cjs/components/livechatwidget/common/ActivitySubscriber/IActivitySubscriber.js +1 -0
  12. package/lib/cjs/components/livechatwidget/common/ActivitySubscriber/PauseActivitySubscriber.js +39 -0
  13. package/lib/cjs/components/livechatwidget/common/ChatAdapterShim.js +70 -0
  14. package/lib/cjs/components/livechatwidget/common/Deferred.js +42 -0
  15. package/lib/cjs/components/livechatwidget/common/authHelper.js +52 -0
  16. package/lib/cjs/components/livechatwidget/common/createAdapter.js +13 -1
  17. package/lib/cjs/components/livechatwidget/common/createMarkdown.js +31 -30
  18. package/lib/cjs/components/livechatwidget/common/defaultProps/dummyDefaultProps.js +13 -3
  19. package/lib/cjs/components/livechatwidget/common/endChat.js +68 -22
  20. package/lib/cjs/components/livechatwidget/common/initWebChatComposer.js +20 -3
  21. package/lib/cjs/components/livechatwidget/common/reconnectChatHelper.js +55 -35
  22. package/lib/cjs/components/livechatwidget/common/setPostChatContextAndLoadSurvey.js +0 -4
  23. package/lib/cjs/components/livechatwidget/common/shareObservable.js +45 -0
  24. package/lib/cjs/components/livechatwidget/common/startChat.js +163 -80
  25. package/lib/cjs/components/livechatwidget/livechatwidgetstateful/LiveChatWidgetStateful.js +231 -91
  26. package/lib/cjs/components/prechatsurveypanestateful/PreChatSurveyPaneStateful.js +4 -7
  27. package/lib/cjs/components/proactivechatpanestateful/ProactiveChatPaneStateful.js +17 -1
  28. package/lib/cjs/components/webchatcontainerstateful/WebChatContainerStateful.js +91 -0
  29. package/lib/cjs/components/webchatcontainerstateful/common/defaultProps/defaultWebChatContainerStatefulProps.js +4 -1
  30. package/lib/cjs/components/webchatcontainerstateful/common/defaultStyles/defaultAdaptiveCardStyles.js +11 -0
  31. package/lib/cjs/components/webchatcontainerstateful/common/mockchatsdk.js +2 -0
  32. package/lib/cjs/components/webchatcontainerstateful/interfaces/IAdaptiveCardStyles.js +1 -0
  33. package/lib/cjs/components/webchatcontainerstateful/interfaces/IBotMagicCodeConfig.js +1 -0
  34. package/lib/cjs/components/webchatcontainerstateful/webchatcontroller/BotMagicCodeStore.js +14 -0
  35. package/lib/cjs/components/webchatcontainerstateful/webchatcontroller/middlewares/renderingmiddlewares/activityMiddleware.js +16 -2
  36. package/lib/cjs/components/webchatcontainerstateful/webchatcontroller/middlewares/renderingmiddlewares/cardActionMiddleware.js +52 -0
  37. package/lib/cjs/components/webchatcontainerstateful/webchatcontroller/middlewares/renderingmiddlewares/cardActionMiddleware.spec.js +98 -0
  38. package/lib/cjs/components/webchatcontainerstateful/webchatcontroller/middlewares/renderingmiddlewares/messageTimestampMiddleware.js +117 -0
  39. package/lib/cjs/contexts/common/ConversationState.js +3 -2
  40. package/lib/cjs/contexts/common/LiveChatWidgetActionType.js +3 -0
  41. package/lib/cjs/contexts/common/LiveChatWidgetContextInitialState.js +3 -2
  42. package/lib/cjs/contexts/createReducer.js +16 -0
  43. package/lib/cjs/controller/componentController.js +3 -3
  44. package/lib/esm/common/Constants.js +18 -2
  45. package/lib/esm/common/telemetry/TelemetryConstants.js +21 -3
  46. package/lib/esm/common/utils.js +52 -0
  47. package/lib/esm/components/chatbuttonstateful/ChatButtonStateful.js +21 -6
  48. package/lib/esm/components/confirmationpanestateful/ConfirmationPaneStateful.js +0 -1
  49. package/lib/esm/components/footerstateful/downloadtranscriptstateful/DownloadTranscriptStateful.js +8 -2
  50. package/lib/esm/components/headerstateful/HeaderStateful.js +4 -3
  51. package/lib/esm/components/livechatwidget/common/ActivityStreamHandler.js +34 -0
  52. package/lib/esm/components/livechatwidget/common/ActivitySubscriber/DefaultActivitySubscriber.js +14 -0
  53. package/lib/esm/components/livechatwidget/common/ActivitySubscriber/IActivitySubscriber.js +1 -0
  54. package/lib/esm/components/livechatwidget/common/ActivitySubscriber/PauseActivitySubscriber.js +29 -0
  55. package/lib/esm/components/livechatwidget/common/ChatAdapterShim.js +59 -0
  56. package/lib/esm/components/livechatwidget/common/Deferred.js +33 -0
  57. package/lib/esm/components/livechatwidget/common/authHelper.js +39 -0
  58. package/lib/esm/components/livechatwidget/common/createAdapter.js +12 -2
  59. package/lib/esm/components/livechatwidget/common/createMarkdown.js +31 -30
  60. package/lib/esm/components/livechatwidget/common/defaultProps/dummyDefaultProps.js +13 -3
  61. package/lib/esm/components/livechatwidget/common/endChat.js +65 -22
  62. package/lib/esm/components/livechatwidget/common/initWebChatComposer.js +19 -5
  63. package/lib/esm/components/livechatwidget/common/reconnectChatHelper.js +56 -37
  64. package/lib/esm/components/livechatwidget/common/setPostChatContextAndLoadSurvey.js +0 -4
  65. package/lib/esm/components/livechatwidget/common/shareObservable.js +38 -0
  66. package/lib/esm/components/livechatwidget/common/startChat.js +157 -82
  67. package/lib/esm/components/livechatwidget/livechatwidgetstateful/LiveChatWidgetStateful.js +223 -94
  68. package/lib/esm/components/prechatsurveypanestateful/PreChatSurveyPaneStateful.js +6 -8
  69. package/lib/esm/components/proactivechatpanestateful/ProactiveChatPaneStateful.js +17 -1
  70. package/lib/esm/components/webchatcontainerstateful/WebChatContainerStateful.js +82 -0
  71. package/lib/esm/components/webchatcontainerstateful/common/defaultProps/defaultWebChatContainerStatefulProps.js +3 -1
  72. package/lib/esm/components/webchatcontainerstateful/common/defaultStyles/defaultAdaptiveCardStyles.js +4 -0
  73. package/lib/esm/components/webchatcontainerstateful/common/mockchatsdk.js +2 -0
  74. package/lib/esm/components/webchatcontainerstateful/interfaces/IAdaptiveCardStyles.js +1 -0
  75. package/lib/esm/components/webchatcontainerstateful/interfaces/IBotMagicCodeConfig.js +1 -0
  76. package/lib/esm/components/webchatcontainerstateful/webchatcontroller/BotMagicCodeStore.js +5 -0
  77. package/lib/esm/components/webchatcontainerstateful/webchatcontroller/middlewares/renderingmiddlewares/activityMiddleware.js +16 -2
  78. package/lib/esm/components/webchatcontainerstateful/webchatcontroller/middlewares/renderingmiddlewares/cardActionMiddleware.js +41 -0
  79. package/lib/esm/components/webchatcontainerstateful/webchatcontroller/middlewares/renderingmiddlewares/cardActionMiddleware.spec.js +94 -0
  80. package/lib/esm/components/webchatcontainerstateful/webchatcontroller/middlewares/renderingmiddlewares/messageTimestampMiddleware.js +107 -0
  81. package/lib/esm/contexts/common/ConversationState.js +3 -2
  82. package/lib/esm/contexts/common/LiveChatWidgetActionType.js +3 -0
  83. package/lib/esm/contexts/common/LiveChatWidgetContextInitialState.js +3 -2
  84. package/lib/esm/contexts/createReducer.js +16 -0
  85. package/lib/esm/controller/componentController.js +3 -3
  86. package/lib/types/common/Constants.d.ts +9 -1
  87. package/lib/types/common/interfaces/IContextDataStore.d.ts +1 -1
  88. package/lib/types/common/telemetry/TelemetryConstants.d.ts +21 -4
  89. package/lib/types/common/utils.d.ts +7 -1
  90. package/lib/types/components/headerstateful/interfaces/IHeaderStatefulParams.d.ts +2 -1
  91. package/lib/types/components/livechatwidget/common/ActivityStreamHandler.d.ts +14 -0
  92. package/lib/types/components/livechatwidget/common/ActivitySubscriber/DefaultActivitySubscriber.d.ts +5 -0
  93. package/lib/types/components/livechatwidget/common/ActivitySubscriber/IActivitySubscriber.d.ts +6 -0
  94. package/lib/types/components/livechatwidget/common/ActivitySubscriber/PauseActivitySubscriber.d.ts +7 -0
  95. package/lib/types/components/livechatwidget/common/ChatAdapterShim.d.ts +7 -0
  96. package/lib/types/components/livechatwidget/common/Deferred.d.ts +9 -0
  97. package/lib/types/components/livechatwidget/common/authHelper.d.ts +4 -0
  98. package/lib/types/components/livechatwidget/common/endChat.d.ts +1 -1
  99. package/lib/types/components/livechatwidget/common/reconnectChatHelper.d.ts +5 -4
  100. package/lib/types/components/livechatwidget/common/shareObservable.d.ts +1 -0
  101. package/lib/types/components/livechatwidget/common/startChat.d.ts +4 -2
  102. package/lib/types/components/livechatwidget/interfaces/ILiveChatWidgetControlProps.d.ts +1 -0
  103. package/lib/types/components/livechatwidget/interfaces/ILiveChatWidgetProps.d.ts +1 -0
  104. package/lib/types/components/reconnectchatpanestateful/interfaces/IReconnectChatPaneStatefulProps.d.ts +0 -1
  105. package/lib/types/components/webchatcontainerstateful/common/defaultStyles/defaultAdaptiveCardStyles.d.ts +2 -0
  106. package/lib/types/components/webchatcontainerstateful/common/mockchatsdk.d.ts +1 -0
  107. package/lib/types/components/webchatcontainerstateful/interfaces/IAdaptiveCardStyles.d.ts +4 -0
  108. package/lib/types/components/webchatcontainerstateful/interfaces/IBotMagicCodeConfig.d.ts +4 -0
  109. package/lib/types/components/webchatcontainerstateful/interfaces/IWebChatContainerStatefulProps.d.ts +4 -0
  110. package/lib/types/components/webchatcontainerstateful/webchatcontroller/BotMagicCodeStore.d.ts +3 -0
  111. package/lib/types/components/webchatcontainerstateful/webchatcontroller/middlewares/renderingmiddlewares/cardActionMiddleware.d.ts +2 -0
  112. package/lib/types/components/webchatcontainerstateful/webchatcontroller/middlewares/renderingmiddlewares/cardActionMiddleware.spec.d.ts +1 -0
  113. package/lib/types/components/webchatcontainerstateful/webchatcontroller/middlewares/renderingmiddlewares/messageTimestampMiddleware.d.ts +5 -0
  114. package/lib/types/contexts/common/ConversationState.d.ts +3 -2
  115. package/lib/types/contexts/common/ILiveChatWidgetContext.d.ts +2 -1
  116. package/lib/types/contexts/common/LiveChatWidgetActionType.d.ts +4 -1
  117. package/package.json +4 -3
@@ -1,7 +1,6 @@
1
- import { ChatSDKError, Constants } from "../../../common/Constants";
2
- import { LogLevel, TelemetryEvent } from "../../../common/telemetry/TelemetryConstants";
1
+ import { ChatSDKError } from "../../../common/Constants";
2
+ import { BroadcastEvent, LogLevel, TelemetryEvent } from "../../../common/telemetry/TelemetryConstants";
3
3
  import { ConversationState } from "../../../contexts/common/ConversationState";
4
- import { DataStoreManager } from "../../../common/contextDataStore/DataStoreManager";
5
4
  import { LiveChatWidgetActionType } from "../../../contexts/common/LiveChatWidgetActionType";
6
5
  import { NotificationHandler } from "../../webchatcontainerstateful/webchatcontroller/notification/NotificationHandler";
7
6
  import { NotificationScenarios } from "../../webchatcontainerstateful/webchatcontroller/enums/NotificationScenarios";
@@ -9,14 +8,24 @@ import { TelemetryHelper } from "../../../common/telemetry/TelemetryHelper";
9
8
  import { TelemetryTimers } from "../../../common/telemetry/TelemetryManager";
10
9
  import { createAdapter } from "./createAdapter";
11
10
  import { createOnNewAdapterActivityHandler } from "../../../plugins/newMessageEventHandler";
12
- import { createTimer } from "../../../common/utils";
11
+ import { createTimer, getStateFromCache, isUndefinedOrEmpty } from "../../../common/utils";
13
12
  import { getReconnectIdForAuthenticatedChat, handleRedirectUnauthenticatedReconnectChat } from "./reconnectChatHelper";
14
13
  import { setPostChatContextAndLoadSurvey } from "./setPostChatContextAndLoadSurvey";
15
14
  import { updateSessionDataForTelemetry } from "./updateSessionDataForTelemetry";
15
+ import { BroadcastService } from "@microsoft/omnichannel-chat-components";
16
+ import { ActivityStreamHandler } from "./ActivityStreamHandler";
17
+ import { handleAuthentication } from "./authHelper"; // eslint-disable-next-line @typescript-eslint/no-explicit-any
18
+
16
19
  let optionalParams = {}; // eslint-disable-next-line @typescript-eslint/no-explicit-any
17
20
 
21
+ let widgetInstanceId; // eslint-disable-next-line @typescript-eslint/no-explicit-any
22
+
18
23
  const prepareStartChat = async (props, chatSDK, state, dispatch, setAdapter) => {
19
- var _props$reconnectChatP;
24
+ var _props$controlProps, _props$reconnectChatP;
25
+
26
+ optionalParams = {}; //Resetting to ensure no stale values
27
+
28
+ widgetInstanceId = props === null || props === void 0 ? void 0 : (_props$controlProps = props.controlProps) === null || _props$controlProps === void 0 ? void 0 : _props$controlProps.widgetInstanceId; // Can connect to existing chat session
20
29
 
21
30
  if (await canConnectToExistingChat(props, chatSDK, state, dispatch, setAdapter)) {
22
31
  return;
@@ -24,59 +33,93 @@ const prepareStartChat = async (props, chatSDK, state, dispatch, setAdapter) =>
24
33
 
25
34
 
26
35
  if ((_props$reconnectChatP = props.reconnectChatPaneProps) !== null && _props$reconnectChatP !== void 0 && _props$reconnectChatP.reconnectId) {
27
- var _props$reconnectChatP2, _props$reconnectChatP3;
36
+ var _props$reconnectChatP2, _props$reconnectChatP3, _props$reconnectChatP4;
28
37
 
29
- await handleRedirectUnauthenticatedReconnectChat(chatSDK, dispatch, setAdapter, initStartChat, (_props$reconnectChatP2 = props.reconnectChatPaneProps) === null || _props$reconnectChatP2 === void 0 ? void 0 : _props$reconnectChatP2.reconnectId, (_props$reconnectChatP3 = props.reconnectChatPaneProps) === null || _props$reconnectChatP3 === void 0 ? void 0 : _props$reconnectChatP3.redirectInSameWindow);
30
- } else {
31
- // Getting PreChat Survey Context
32
- const parseToJson = false;
33
- const preChatSurveyResponse = await chatSDK.getPreChatSurvey(parseToJson);
34
- const showPrechat = state.appStates.conversationState === ConversationState.ProactiveChat ? preChatSurveyResponse && state.appStates.proactiveChatStates.proactiveChatEnablePrechat : preChatSurveyResponse; // Getting reconnectId for authenticated chat
38
+ await handleRedirectUnauthenticatedReconnectChat(chatSDK, props.chatConfig, props.getAuthToken, dispatch, setAdapter, initStartChat, (_props$reconnectChatP2 = props.reconnectChatPaneProps) === null || _props$reconnectChatP2 === void 0 ? void 0 : _props$reconnectChatP2.isReconnectEnabled, (_props$reconnectChatP3 = props.reconnectChatPaneProps) === null || _props$reconnectChatP3 === void 0 ? void 0 : _props$reconnectChatP3.reconnectId, (_props$reconnectChatP4 = props.reconnectChatPaneProps) === null || _props$reconnectChatP4 === void 0 ? void 0 : _props$reconnectChatP4.redirectInSameWindow);
39
+ return;
40
+ } // Getting reconnectId for authenticated chat
35
41
 
36
- const reconnectId = await getReconnectIdForAuthenticatedChat(props, chatSDK);
37
42
 
38
- if (reconnectId) {
39
- dispatch({
40
- type: LiveChatWidgetActionType.SET_RECONNECT_ID,
41
- payload: reconnectId
42
- });
43
- dispatch({
44
- type: LiveChatWidgetActionType.SET_CONVERSATION_STATE,
45
- payload: ConversationState.ReconnectChat
46
- });
47
- } else if (showPrechat) {
48
- dispatch({
49
- type: LiveChatWidgetActionType.SET_PRE_CHAT_SURVEY_RESPONSE,
50
- payload: preChatSurveyResponse
51
- });
52
- dispatch({
53
- type: LiveChatWidgetActionType.SET_CONVERSATION_STATE,
54
- payload: ConversationState.Prechat
55
- });
56
- setCustomContextParams(props, state);
57
- } else {
58
- dispatch({
59
- type: LiveChatWidgetActionType.SET_CONVERSATION_STATE,
60
- payload: ConversationState.Loading
61
- });
62
- setCustomContextParams(props, state);
63
- await initStartChat(chatSDK, dispatch, setAdapter);
64
- }
65
- }
43
+ const reconnectId = await getReconnectIdForAuthenticatedChat(props, chatSDK);
44
+
45
+ if (reconnectId) {
46
+ dispatch({
47
+ type: LiveChatWidgetActionType.SET_RECONNECT_ID,
48
+ payload: reconnectId
49
+ });
50
+ dispatch({
51
+ type: LiveChatWidgetActionType.SET_CONVERSATION_STATE,
52
+ payload: ConversationState.ReconnectChat
53
+ });
54
+ return;
55
+ } // Setting Proactive chat settings
56
+
57
+
58
+ const isProactiveChat = state.appStates.conversationState === ConversationState.ProactiveChat;
59
+ const isPreChatEnabledInProactiveChat = state.appStates.proactiveChatStates.proactiveChatEnablePrechat; //Setting PreChat and intiate chat
60
+
61
+ setPreChatAndInitiateChat(chatSDK, props.chatConfig, props.getAuthToken, dispatch, setAdapter, isProactiveChat, isPreChatEnabledInProactiveChat);
62
+ }; // eslint-disable-next-line @typescript-eslint/no-explicit-any
63
+
64
+
65
+ const setPreChatAndInitiateChat = async (chatSDK, chatConfig, getAuthToken, dispatch, setAdapter, isProactiveChat, proactiveChatEnablePrechatState) => {
66
+ // Getting prechat Survey Context
67
+ const parseToJson = false;
68
+ const preChatSurveyResponse = await chatSDK.getPreChatSurvey(parseToJson);
69
+ const showPrechat = isProactiveChat ? preChatSurveyResponse && proactiveChatEnablePrechatState : preChatSurveyResponse;
70
+
71
+ if (showPrechat) {
72
+ dispatch({
73
+ type: LiveChatWidgetActionType.SET_PRE_CHAT_SURVEY_RESPONSE,
74
+ payload: preChatSurveyResponse
75
+ });
76
+ dispatch({
77
+ type: LiveChatWidgetActionType.SET_CONVERSATION_STATE,
78
+ payload: ConversationState.Prechat
79
+ });
80
+ return;
81
+ } //Initiate start chat
82
+
83
+
84
+ dispatch({
85
+ type: LiveChatWidgetActionType.SET_CONVERSATION_STATE,
86
+ payload: ConversationState.Loading
87
+ });
88
+ await initStartChat(chatSDK, chatConfig, getAuthToken, dispatch, setAdapter);
66
89
  }; // eslint-disable-next-line @typescript-eslint/no-explicit-any
67
90
 
68
91
 
69
- const initStartChat = async (chatSDK, dispatch, setAdapter, params, persistedState) => {
92
+ const initStartChat = async (chatSDK, chatConfig, getAuthToken, dispatch, setAdapter, params, persistedState) => {
70
93
  try {
71
94
  var _newAdapter$activity$, _TelemetryTimers$Widg;
72
95
 
96
+ let isStartChatSuccessful = false; //Check if chat retrieved from cache
97
+
98
+ if (persistedState || params !== null && params !== void 0 && params.liveChatContext) {
99
+ var _persistedState$domai, _persistedState$domai2, _persistedState$domai3, _persistedState$domai4, _persistedState$domai5;
100
+
101
+ BroadcastService.postMessage({
102
+ eventName: BroadcastEvent.ChatRetrievedFromCache,
103
+ payload: {
104
+ chatId: persistedState === null || persistedState === void 0 ? void 0 : (_persistedState$domai = persistedState.domainStates) === null || _persistedState$domai === void 0 ? void 0 : (_persistedState$domai2 = _persistedState$domai.liveChatContext) === null || _persistedState$domai2 === void 0 ? void 0 : (_persistedState$domai3 = _persistedState$domai2.chatToken) === null || _persistedState$domai3 === void 0 ? void 0 : _persistedState$domai3.chatId,
105
+ requestId: persistedState === null || persistedState === void 0 ? void 0 : (_persistedState$domai4 = persistedState.domainStates) === null || _persistedState$domai4 === void 0 ? void 0 : (_persistedState$domai5 = _persistedState$domai4.liveChatContext) === null || _persistedState$domai5 === void 0 ? void 0 : _persistedState$domai5.requestId
106
+ }
107
+ });
108
+ }
109
+
73
110
  try {
111
+ //Start widget load timer
74
112
  TelemetryTimers.WidgetLoadTimer = createTimer();
75
113
  TelemetryHelper.logSDKEvent(LogLevel.INFO, {
76
114
  Event: TelemetryEvent.StartChatSDKCall
77
- });
78
- optionalParams = Object.assign({}, params, optionalParams);
115
+ }); // Set custom context params
116
+
117
+ setCustomContextParams(chatSDK);
118
+ optionalParams = Object.assign({}, params, optionalParams); // set auth token to chat sdk before start chat
119
+
120
+ await handleAuthentication(chatSDK, chatConfig, getAuthToken);
79
121
  await chatSDK.startChat(optionalParams);
122
+ isStartChatSuccessful = true;
80
123
  } catch (error) {
81
124
  TelemetryHelper.logSDKEvent(LogLevel.ERROR, {
82
125
  Event: TelemetryEvent.StartChatMethodException,
@@ -84,43 +127,52 @@ const initStartChat = async (chatSDK, dispatch, setAdapter, params, persistedSta
84
127
  exception: `Failed to setup startChat: ${error}`
85
128
  }
86
129
  });
87
- }
130
+ isStartChatSuccessful = false; // Resetting the widget state to Closed, for recent introduction of OC rate limiting(429 Error)
131
+ // TODO : How to diplay a proper UI message to customer to try after sometime at this point - cool down scenario
132
+
133
+ dispatch({
134
+ type: LiveChatWidgetActionType.SET_CONVERSATION_STATE,
135
+ payload: ConversationState.Closed
136
+ });
137
+ return;
138
+ } // New adapter creation
139
+
88
140
 
89
141
  const newAdapter = await createAdapter(chatSDK);
90
142
  setAdapter(newAdapter);
91
143
  const chatToken = await chatSDK.getChatToken();
144
+ dispatch({
145
+ type: LiveChatWidgetActionType.SET_CHAT_TOKEN,
146
+ payload: chatToken
147
+ });
92
148
  newAdapter === null || newAdapter === void 0 ? void 0 : (_newAdapter$activity$ = newAdapter.activity$) === null || _newAdapter$activity$ === void 0 ? void 0 : _newAdapter$activity$.subscribe(createOnNewAdapterActivityHandler(chatToken === null || chatToken === void 0 ? void 0 : chatToken.chatId, chatToken === null || chatToken === void 0 ? void 0 : chatToken.visitorId));
93
149
 
94
- if (!persistedState) {
95
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
96
- if (chatSDK !== null && chatSDK !== void 0 && chatSDK.getVoiceVideoCalling) {
97
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
98
- const chatToken = await (chatSDK === null || chatSDK === void 0 ? void 0 : chatSDK.getChatToken());
99
- dispatch({
100
- type: LiveChatWidgetActionType.SET_CHAT_TOKEN,
101
- payload: chatToken
102
- });
103
- } // eslint-disable-next-line @typescript-eslint/no-explicit-any
104
-
105
-
106
- const liveChatContext = await (chatSDK === null || chatSDK === void 0 ? void 0 : chatSDK.getCurrentLiveChatContext());
150
+ if (persistedState) {
107
151
  dispatch({
108
- type: LiveChatWidgetActionType.SET_LIVE_CHAT_CONTEXT,
109
- payload: liveChatContext
152
+ type: LiveChatWidgetActionType.SET_WIDGET_STATE,
153
+ payload: persistedState
110
154
  });
111
- await setPostChatContextAndLoadSurvey(chatSDK, dispatch);
112
- await updateSessionDataForTelemetry(chatSDK, dispatch); // Set app state to Active
155
+ await setPostChatContextAndLoadSurvey(chatSDK, dispatch, true);
156
+ return;
157
+ } // eslint-disable-next-line @typescript-eslint/no-explicit-any
158
+
159
+
160
+ const liveChatContext = await (chatSDK === null || chatSDK === void 0 ? void 0 : chatSDK.getCurrentLiveChatContext());
161
+ dispatch({
162
+ type: LiveChatWidgetActionType.SET_LIVE_CHAT_CONTEXT,
163
+ payload: liveChatContext
164
+ }); // Set post chat context in state, no survey load
165
+
166
+ await setPostChatContextAndLoadSurvey(chatSDK, dispatch); // Updating chat session detail for telemetry
167
+
168
+ await updateSessionDataForTelemetry(chatSDK, dispatch); // Set app state to Active
113
169
 
170
+ if (isStartChatSuccessful) {
171
+ ActivityStreamHandler.uncork();
114
172
  dispatch({
115
173
  type: LiveChatWidgetActionType.SET_CONVERSATION_STATE,
116
174
  payload: ConversationState.Active
117
175
  });
118
- } else {
119
- dispatch({
120
- type: LiveChatWidgetActionType.SET_WIDGET_STATE,
121
- payload: persistedState
122
- });
123
- await setPostChatContextAndLoadSurvey(chatSDK, dispatch, true);
124
176
  }
125
177
 
126
178
  TelemetryHelper.logLoadingEvent(LogLevel.INFO, {
@@ -147,42 +199,65 @@ const initStartChat = async (chatSDK, dispatch, setAdapter, params, persistedSta
147
199
  payload: ConversationState.OutOfOffice
148
200
  });
149
201
  }
202
+ } finally {
203
+ optionalParams = {};
204
+ widgetInstanceId = "";
150
205
  }
151
206
  }; // eslint-disable-next-line @typescript-eslint/no-explicit-any
152
207
 
153
208
 
154
209
  const canConnectToExistingChat = async (props, chatSDK, state, dispatch, setAdapter) => {
155
- var _DataStoreManager$cli, _persistedState$domai;
210
+ var _chatSDK$omnichannelC, _chatSDK$omnichannelC2, _props$controlProps2, _persistedState$domai6, _persistedState$appSt;
211
+
212
+ // By pass this function in case of popout chat
213
+ if (state.appStates.skipChatButtonRendering === true) {
214
+ return false;
215
+ }
156
216
 
157
- const widgetStateFromCache = (_DataStoreManager$cli = DataStoreManager.clientDataStore) === null || _DataStoreManager$cli === void 0 ? void 0 : _DataStoreManager$cli.getData(Constants.widgetStateDataKey, "localStorage");
158
- const persistedState = widgetStateFromCache ? JSON.parse(widgetStateFromCache) : undefined;
217
+ const persistedState = getStateFromCache(chatSDK === null || chatSDK === void 0 ? void 0 : (_chatSDK$omnichannelC = chatSDK.omnichannelConfig) === null || _chatSDK$omnichannelC === void 0 ? void 0 : _chatSDK$omnichannelC.orgId, chatSDK === null || chatSDK === void 0 ? void 0 : (_chatSDK$omnichannelC2 = chatSDK.omnichannelConfig) === null || _chatSDK$omnichannelC2 === void 0 ? void 0 : _chatSDK$omnichannelC2.widgetId, (props === null || props === void 0 ? void 0 : (_props$controlProps2 = props.controlProps) === null || _props$controlProps2 === void 0 ? void 0 : _props$controlProps2.widgetInstanceId) ?? ""); //Connect to only active chat session
159
218
 
160
- if (persistedState !== null && persistedState !== void 0 && (_persistedState$domai = persistedState.domainStates) !== null && _persistedState$domai !== void 0 && _persistedState$domai.liveChatContext) {
161
- var _persistedState$domai2;
219
+ if (persistedState && !isUndefinedOrEmpty(persistedState === null || persistedState === void 0 ? void 0 : (_persistedState$domai6 = persistedState.domainStates) === null || _persistedState$domai6 === void 0 ? void 0 : _persistedState$domai6.liveChatContext) && (persistedState === null || persistedState === void 0 ? void 0 : (_persistedState$appSt = persistedState.appStates) === null || _persistedState$appSt === void 0 ? void 0 : _persistedState$appSt.conversationState) === ConversationState.Active) {
220
+ var _persistedState$domai7;
162
221
 
163
222
  dispatch({
164
223
  type: LiveChatWidgetActionType.SET_CONVERSATION_STATE,
165
224
  payload: ConversationState.Loading
166
225
  });
167
226
  const optionalParams = {
168
- liveChatContext: persistedState === null || persistedState === void 0 ? void 0 : (_persistedState$domai2 = persistedState.domainStates) === null || _persistedState$domai2 === void 0 ? void 0 : _persistedState$domai2.liveChatContext
227
+ liveChatContext: persistedState === null || persistedState === void 0 ? void 0 : (_persistedState$domai7 = persistedState.domainStates) === null || _persistedState$domai7 === void 0 ? void 0 : _persistedState$domai7.liveChatContext
169
228
  };
170
- await initStartChat(chatSDK, dispatch, setAdapter, optionalParams, persistedState);
229
+ await initStartChat(chatSDK, props.chatConfig, props.getAuthToken, dispatch, setAdapter, optionalParams, persistedState);
171
230
  return true;
172
231
  } else {
173
232
  return false;
174
233
  }
175
- };
234
+ }; // eslint-disable-next-line @typescript-eslint/no-explicit-any
235
+
236
+
237
+ const setCustomContextParams = chatSDK => {
238
+ var _chatSDK$omnichannelC3, _chatSDK$omnichannelC4, _persistedState$domai8;
239
+
240
+ // Add custom context only for unauthenticated chat
241
+ const persistedState = getStateFromCache(chatSDK === null || chatSDK === void 0 ? void 0 : (_chatSDK$omnichannelC3 = chatSDK.omnichannelConfig) === null || _chatSDK$omnichannelC3 === void 0 ? void 0 : _chatSDK$omnichannelC3.orgId, chatSDK === null || chatSDK === void 0 ? void 0 : (_chatSDK$omnichannelC4 = chatSDK.omnichannelConfig) === null || _chatSDK$omnichannelC4 === void 0 ? void 0 : _chatSDK$omnichannelC4.widgetId, widgetInstanceId ?? "");
242
+
243
+ if (!isUndefinedOrEmpty(persistedState === null || persistedState === void 0 ? void 0 : (_persistedState$domai8 = persistedState.domainStates) === null || _persistedState$domai8 === void 0 ? void 0 : _persistedState$domai8.customContext)) {
244
+ var _persistedState$domai9, _persistedState$domai10;
176
245
 
177
- const setCustomContextParams = (props, state) => {
178
- var _props$chatConfig, _state$domainStates;
246
+ if (persistedState !== null && persistedState !== void 0 && (_persistedState$domai9 = persistedState.domainStates.liveChatConfig) !== null && _persistedState$domai9 !== void 0 && _persistedState$domai9.LiveChatConfigAuthSettings) {
247
+ const errorMessage = "Use of custom context with authenticated chat is deprecated. The chat would not go through.";
248
+ TelemetryHelper.logSDKEvent(LogLevel.WARN, {
249
+ Event: TelemetryEvent.StartChatMethodException,
250
+ ExceptionDetails: {
251
+ exception: errorMessage
252
+ }
253
+ });
254
+ throw new Error(errorMessage);
255
+ }
179
256
 
180
- // Add custom context if any only for unauthenticated chat
181
- if (!((_props$chatConfig = props.chatConfig) !== null && _props$chatConfig !== void 0 && _props$chatConfig.LiveChatConfigAuthSettings) && (_state$domainStates = state.domainStates) !== null && _state$domainStates !== void 0 && _state$domainStates.customContext) {
182
257
  optionalParams = Object.assign({}, optionalParams, {
183
- customContext: state.domainStates.customContext
258
+ customContext: persistedState === null || persistedState === void 0 ? void 0 : (_persistedState$domai10 = persistedState.domainStates) === null || _persistedState$domai10 === void 0 ? void 0 : _persistedState$domai10.customContext
184
259
  });
185
260
  }
186
261
  };
187
262
 
188
- export { prepareStartChat, initStartChat };
263
+ export { prepareStartChat, initStartChat, setPreChatAndInitiateChat };