@xapp/chat-widget 1.36.0-alpha.1 → 1.36.3

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/index.js CHANGED
@@ -2906,7 +2906,7 @@ function responseToMessage(botResponse, now) {
2906
2906
  if (html) {
2907
2907
  responseMessage.msg = __assign(__assign({}, responseMessage.msg), { html: html });
2908
2908
  }
2909
- if ((_e = outputSpeech.suggestions) === null || _e === void 0 ? void 0 : _e.length) {
2909
+ if ((_e = outputSpeech === null || outputSpeech === void 0 ? void 0 : outputSpeech.suggestions) === null || _e === void 0 ? void 0 : _e.length) {
2910
2910
  responseMessage.msg.options = outputSpeech.suggestions.map(function (suggestion) {
2911
2911
  if (typeof suggestion === "string") {
2912
2912
  // Simple chips (strings)
@@ -2970,6 +2970,7 @@ var StentorDirectChat = /** @class */ (function () {
2970
2970
  function StentorDirectChat(config, options) {
2971
2971
  this._userId = "";
2972
2972
  this._sessionId = "";
2973
+ this._accessToken = "";
2973
2974
  this.config = config;
2974
2975
  this.options = options;
2975
2976
  this.isNewSession = false;
@@ -3004,7 +3005,6 @@ var StentorDirectChat = /** @class */ (function () {
3004
3005
  };
3005
3006
  StentorDirectChat.prototype.userJoined = function (data) {
3006
3007
  log("SERVER: user joined: ".concat(JSON.stringify(data)));
3007
- this.visitorInfo.token = data.token;
3008
3008
  this.dispatch({
3009
3009
  type: "chat",
3010
3010
  detail: {
@@ -3117,6 +3117,7 @@ var StentorDirectChat = /** @class */ (function () {
3117
3117
  };
3118
3118
  StentorDirectChat.prototype.setVisitorInfo = function (visitorInfo, cb) {
3119
3119
  this.visitorInfo = visitorInfo;
3120
+ this._accessToken = this.visitorInfo.accessToken;
3120
3121
  this.startSession();
3121
3122
  this.userJoined({
3122
3123
  user: this.getBot(undefined),
@@ -3138,10 +3139,11 @@ var StentorDirectChat = /** @class */ (function () {
3138
3139
  StentorDirectChat.prototype.postMessage = function (message) {
3139
3140
  var _a, _b, _c, _d;
3140
3141
  return __awaiter$1(this, void 0, void 0, function () {
3141
- var request, now, permissionRequest, expired, text, granted, userProfile, isEmail, botResponse, responseMessage;
3142
+ var request, accessToken, now, permissionRequest, expired, text, granted, userProfile, isEmail, botResponse, responseMessage;
3142
3143
  return __generator$1(this, function (_e) {
3143
3144
  switch (_e.label) {
3144
3145
  case 0:
3146
+ accessToken = this._accessToken;
3145
3147
  now = new Date().getTime();
3146
3148
  if (this.isNewSession && !((_a = message === null || message === void 0 ? void 0 : message.msg) === null || _a === void 0 ? void 0 : _a.text)) {
3147
3149
  request = {
@@ -3152,7 +3154,8 @@ var StentorDirectChat = /** @class */ (function () {
3152
3154
  isNewSession: true,
3153
3155
  intentId: "LaunchRequest",
3154
3156
  platform: "stentor-platform",
3155
- channel: "widget"
3157
+ channel: "widget",
3158
+ accessToken: accessToken
3156
3159
  };
3157
3160
  }
3158
3161
  else {
@@ -3191,7 +3194,8 @@ var StentorDirectChat = /** @class */ (function () {
3191
3194
  userProfile: userProfile,
3192
3195
  intentId: "PermissionGrant",
3193
3196
  platform: "stentor-platform",
3194
- channel: "widget"
3197
+ channel: "widget",
3198
+ accessToken: accessToken
3195
3199
  };
3196
3200
  }
3197
3201
  else if (message.type === "custom") {
@@ -3199,7 +3203,7 @@ var StentorDirectChat = /** @class */ (function () {
3199
3203
  // token: message.msg.token,
3200
3204
  sessionId: this._sessionId, userId: this._userId, isNewSession: false,
3201
3205
  // intentId: "OptionSelect",
3202
- platform: "stentor-platform", channel: "widget" });
3206
+ platform: "stentor-platform", channel: "widget", accessToken: accessToken });
3203
3207
  }
3204
3208
  else if (message.msg.token) {
3205
3209
  request = {
@@ -3210,7 +3214,8 @@ var StentorDirectChat = /** @class */ (function () {
3210
3214
  isNewSession: false,
3211
3215
  intentId: "OptionSelect",
3212
3216
  platform: "stentor-platform",
3213
- channel: "widget"
3217
+ channel: "widget",
3218
+ accessToken: accessToken
3214
3219
  };
3215
3220
  }
3216
3221
  else {
@@ -3222,7 +3227,8 @@ var StentorDirectChat = /** @class */ (function () {
3222
3227
  isNewSession: false,
3223
3228
  intentId: "NLU_RESULT_PLACEHOLDER",
3224
3229
  platform: "stentor-platform",
3225
- channel: "widget"
3230
+ channel: "widget",
3231
+ accessToken: accessToken
3226
3232
  };
3227
3233
  }
3228
3234
  }
@@ -7496,11 +7502,15 @@ function getSrcId(url) {
7496
7502
 
7497
7503
  function useChatServerVisitorId() {
7498
7504
  var dispatch = useChatServerDispatch();
7499
- var clientVisitorId = reactRedux.useSelector(function (state) { return state.visitorId; });
7505
+ var clientVisitorId = reactRedux.useSelector(function (state) {
7506
+ return state.visitorId || state.userId;
7507
+ });
7508
+ var visitorAccessToken = reactRedux.useSelector(function (state) { return state.accessToken; });
7500
7509
  return React$1.useMemo(function () { return ({
7501
7510
  dispatch: dispatch,
7502
- visitorId: clientVisitorId
7503
- }); }, [dispatch, clientVisitorId]);
7511
+ visitorId: clientVisitorId,
7512
+ accessToken: visitorAccessToken
7513
+ }); }, [dispatch, clientVisitorId, visitorAccessToken]);
7504
7514
  }
7505
7515
  //send whenever server settings or visitor id changes
7506
7516
  function useGreeting(active) {
@@ -7513,7 +7523,8 @@ function useGreeting(active) {
7513
7523
  if (curr.visitorId) {
7514
7524
  curr.dispatch(sendVisitorInfo({
7515
7525
  name: "Visitor",
7516
- visitorId: curr.visitorId
7526
+ visitorId: curr.visitorId,
7527
+ accessToken: curr.accessToken
7517
7528
  }));
7518
7529
  var timeoutId_1 = setTimeout(function () {
7519
7530
  var greetingAction = sendGreeting();
@@ -7529,11 +7540,6 @@ function useGreeting(active) {
7529
7540
  }, [curr, active]);
7530
7541
  }
7531
7542
 
7532
- function match(item, pattern) {
7533
- var value = item.text;
7534
- return value.toLocaleLowerCase().indexOf(pattern.toLocaleLowerCase()) >= 0;
7535
- }
7536
-
7537
7543
  function useSuggestionsFetch(search, context) {
7538
7544
  var env = useWidgetEnv();
7539
7545
  var baseUrl = env === null || env === void 0 ? void 0 : env.autocompleteSuggestionsUrl;
@@ -7586,27 +7592,18 @@ function useSuggestions(search, context) {
7586
7592
  var suggestionsResponse = useSuggestionsFetch(query || "", contextState);
7587
7593
  var suggestionsData = suggestionsResponse.state === "success" ? suggestionsResponse.data : undefined;
7588
7594
  var raw = React$1.useMemo(function () {
7589
- return suggestionsData ? parseSuggestionsResponse_1(suggestionsData, -1) : undefined;
7595
+ return suggestionsData ?
7596
+ parseSuggestionsResponse_1(suggestionsData, -1).map(function (suggestion) {
7597
+ return {
7598
+ heading: suggestion.heading,
7599
+ items: suggestion.items.slice().reverse()
7600
+ };
7601
+ }) : undefined;
7590
7602
  }, [suggestionsData]);
7591
7603
  React$1.useEffect(function () {
7592
7604
  var prev = suggestionItem;
7593
7605
  setSuggestions(raw);
7594
- if (prev && raw) {
7595
- var prevValue = prev.text;
7596
- var index = 0;
7597
- for (var _i = 0, raw_1 = raw; _i < raw_1.length; _i++) {
7598
- var group = raw_1[_i];
7599
- for (var _a = 0, _b = group.items; _a < _b.length; _a++) {
7600
- var item = _b[_a];
7601
- if (match(item, prevValue)) {
7602
- setSuggestionIndex(index);
7603
- break;
7604
- }
7605
- index++;
7606
- }
7607
- }
7608
- }
7609
- else {
7606
+ if (!prev || !raw) {
7610
7607
  setSuggestionIndex(-1);
7611
7608
  }
7612
7609
  }, [raw, suggestionItem]);
@@ -8612,26 +8609,17 @@ var DEFAULT_VISITOR = {
8612
8609
  nick: "visitor:",
8613
8610
  typing: false
8614
8611
  };
8615
- function createDefaultState() {
8616
- return {
8617
- connection: {
8612
+ function createDefaultState(state) {
8613
+ if (!state) {
8614
+ state = {};
8615
+ }
8616
+ state.userId = state.userId ? state.userId : visitorFingerprint();
8617
+ state.visitorId = state.userId;
8618
+ return __assign({ connection: {
8618
8619
  connectionStatus: "offline",
8619
8620
  token: null,
8620
8621
  greetingRequested: false
8621
- },
8622
- accountStatus: "offline",
8623
- departments: {},
8624
- visitor: DEFAULT_VISITOR,
8625
- agents: {},
8626
- chats: [],
8627
- lastTimestamp: 0,
8628
- lastRatingRequestTimestamp: 0,
8629
- hasRating: false,
8630
- isChatting: false,
8631
- queuePosition: 0,
8632
- visitorId: visitorFingerprint(),
8633
- chips: []
8634
- };
8622
+ }, accountStatus: "offline", departments: {}, visitor: DEFAULT_VISITOR, agents: {}, chats: [], lastTimestamp: 0, lastRatingRequestTimestamp: 0, hasRating: false, isChatting: false, queuePosition: 0, visitorId: visitorFingerprint(), chips: [] }, state);
8635
8623
  }
8636
8624
  var DEFAULT_STATE = createDefaultState();
8637
8625
 
@@ -8807,8 +8795,10 @@ function storeHandler(state, action) {
8807
8795
 
8808
8796
  function createChatStore(config, dataStorage) {
8809
8797
  if (dataStorage === void 0) { dataStorage = sessionStorage; }
8810
- var storage = new BrowserStateStorage(dataStorage, "xappchat.".concat(config.serverUrl, ".").concat(config.accountKey));
8811
- var chatReducer = persistStateReducer(storage, DEFAULT_STATE, storeHandler);
8798
+ var connection = config.connection;
8799
+ var storage = new BrowserStateStorage(dataStorage, "xappchat.".concat(connection.serverUrl, ".").concat(connection.accountKey));
8800
+ var defaultState = createDefaultState({ accessToken: config.accessToken, userId: config.userId });
8801
+ var chatReducer = persistStateReducer(storage, defaultState, storeHandler);
8812
8802
  var middlewares = [
8813
8803
  thunk__default["default"]
8814
8804
  ];
@@ -8817,11 +8807,18 @@ function createChatStore(config, dataStorage) {
8817
8807
  }
8818
8808
 
8819
8809
  var ChatWidgetContainer = function (props) {
8820
- var _a;
8810
+ var _a, _b, _c;
8821
8811
  var messageMiddleware = useStandardMiddleware();
8822
8812
  var connection = useServerConfig(props.config);
8823
- var chatStore = React$1.useMemo(function () { return createChatStore(connection); }, [connection]);
8824
- if ((_a = props.config) === null || _a === void 0 ? void 0 : _a.disabled) {
8813
+ var chatStore = React$1.useMemo(function () {
8814
+ var _a, _b;
8815
+ return createChatStore({
8816
+ connection: connection,
8817
+ userId: (_a = props.config) === null || _a === void 0 ? void 0 : _a.userId,
8818
+ accessToken: (_b = props.config) === null || _b === void 0 ? void 0 : _b.accessToken
8819
+ });
8820
+ }, [connection, (_a = props.config) === null || _a === void 0 ? void 0 : _a.userId, (_b = props.config) === null || _b === void 0 ? void 0 : _b.accessToken]);
8821
+ if ((_c = props.config) === null || _c === void 0 ? void 0 : _c.disabled) {
8825
8822
  return React__default$1["default"].createElement(React__default$1["default"].Fragment, null);
8826
8823
  }
8827
8824
  var widgetProps = __assign(__assign({}, props), { messageMiddleware: messageMiddleware });