@botonic/react 0.20.8 → 0.21.0

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.
@@ -181,10 +181,10 @@ var Webchat = /*#__PURE__*/(0, _react.forwardRef)(function (props, ref) {
181
181
  customComponent = _useState2[0],
182
182
  setCustomComponent = _useState2[1];
183
183
 
184
- var storage = props.storage === undefined ? localStorage : props.storage;
184
+ var storage = props.storage;
185
185
  var storageKey = typeof props.storageKey === 'function' ? props.storageKey() : props.storageKey;
186
186
 
187
- var _useStorageState = (0, _useStorageStateHook.useStorageState)(storage, storageKey || _constants.WEBCHAT.DEFAULTS.STORAGE_KEY),
187
+ var _useStorageState = (0, _useStorageStateHook.useStorageState)(storage, storageKey),
188
188
  _useStorageState2 = (0, _slicedToArray2["default"])(_useStorageState, 2),
189
189
  botonicState = _useStorageState2[0],
190
190
  saveState = _useStorageState2[1];
@@ -244,6 +244,38 @@ var Webchat = /*#__PURE__*/(0, _react.forwardRef)(function (props, ref) {
244
244
  return function sendUserInput(_x) {
245
245
  return _ref2.apply(this, arguments);
246
246
  };
247
+ }();
248
+
249
+ var sendChatEvent = /*#__PURE__*/function () {
250
+ var _ref3 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2(chatEvent) {
251
+ var chatEventInput;
252
+ return _regenerator["default"].wrap(function _callee2$(_context2) {
253
+ while (1) {
254
+ switch (_context2.prev = _context2.next) {
255
+ case 0:
256
+ chatEventInput = {
257
+ id: (0, _uuid.v4)(),
258
+ type: _core.INPUT.CHAT_EVENT,
259
+ data: chatEvent
260
+ };
261
+ props.onUserInput && props.onUserInput({
262
+ user: webchatState.session.user,
263
+ input: chatEventInput,
264
+ session: webchatState.session,
265
+ lastRoutePath: webchatState.lastRoutePath
266
+ });
267
+
268
+ case 2:
269
+ case "end":
270
+ return _context2.stop();
271
+ }
272
+ }
273
+ }, _callee2);
274
+ }));
275
+
276
+ return function sendChatEvent(_x2) {
277
+ return _ref3.apply(this, arguments);
278
+ };
247
279
  }(); // Load styles stored in window._botonicInsertStyles by Webpack
248
280
 
249
281
 
@@ -291,13 +323,13 @@ var Webchat = /*#__PURE__*/(0, _react.forwardRef)(function (props, ref) {
291
323
  }); // Load initial state from storage
292
324
 
293
325
  (0, _react.useEffect)(function () {
294
- var _ref3 = botonicState || {},
295
- messages = _ref3.messages,
296
- session = _ref3.session,
297
- lastRoutePath = _ref3.lastRoutePath,
298
- devSettings = _ref3.devSettings,
299
- lastMessageUpdate = _ref3.lastMessageUpdate,
300
- themeUpdates = _ref3.themeUpdates;
326
+ var _ref4 = botonicState || {},
327
+ messages = _ref4.messages,
328
+ session = _ref4.session,
329
+ lastRoutePath = _ref4.lastRoutePath,
330
+ devSettings = _ref4.devSettings,
331
+ lastMessageUpdate = _ref4.lastMessageUpdate,
332
+ themeUpdates = _ref4.themeUpdates;
301
333
 
302
334
  session = (0, _webchat.initSession)(session);
303
335
  updateSession(session);
@@ -340,10 +372,10 @@ var Webchat = /*#__PURE__*/(0, _react.forwardRef)(function (props, ref) {
340
372
  if (onStateChange && typeof onStateChange === 'function') onStateChange(webchatState);
341
373
  saveWebchatState(webchatState);
342
374
  }, [webchatState.messagesJSON, webchatState.session, webchatState.lastRoutePath, webchatState.devSettings, webchatState.lastMessageUpdate]);
343
- (0, _useAsyncEffect.useAsyncEffect)( /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2() {
344
- return _regenerator["default"].wrap(function _callee2$(_context2) {
375
+ (0, _useAsyncEffect.useAsyncEffect)( /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee3() {
376
+ return _regenerator["default"].wrap(function _callee3$(_context3) {
345
377
  while (1) {
346
- switch (_context2.prev = _context2.next) {
378
+ switch (_context3.prev = _context3.next) {
347
379
  case 0:
348
380
  if (!webchatState.online) {
349
381
  setError({
@@ -357,10 +389,10 @@ var Webchat = /*#__PURE__*/(0, _react.forwardRef)(function (props, ref) {
357
389
 
358
390
  case 1:
359
391
  case "end":
360
- return _context2.stop();
392
+ return _context3.stop();
361
393
  }
362
394
  }
363
- }, _callee2);
395
+ }, _callee3);
364
396
  })), [webchatState.online]);
365
397
  (0, _hooks.useTyping)({
366
398
  webchatState: webchatState,
@@ -511,34 +543,34 @@ var Webchat = /*#__PURE__*/(0, _react.forwardRef)(function (props, ref) {
511
543
  };
512
544
 
513
545
  var sendInput = /*#__PURE__*/function () {
514
- var _ref5 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee3(input) {
546
+ var _ref6 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee4(input) {
515
547
  var messageComponent;
516
- return _regenerator["default"].wrap(function _callee3$(_context3) {
548
+ return _regenerator["default"].wrap(function _callee4$(_context4) {
517
549
  while (1) {
518
- switch (_context3.prev = _context3.next) {
550
+ switch (_context4.prev = _context4.next) {
519
551
  case 0:
520
552
  if (!(!input || Object.keys(input).length == 0)) {
521
- _context3.next = 2;
553
+ _context4.next = 2;
522
554
  break;
523
555
  }
524
556
 
525
- return _context3.abrupt("return");
557
+ return _context4.abrupt("return");
526
558
 
527
559
  case 2:
528
560
  if (!((0, _messageUtils.isText)(input) && (!input.data || !input.data.trim()))) {
529
- _context3.next = 4;
561
+ _context4.next = 4;
530
562
  break;
531
563
  }
532
564
 
533
- return _context3.abrupt("return");
565
+ return _context4.abrupt("return");
534
566
 
535
567
  case 4:
536
568
  if (!((0, _messageUtils.isText)(input) && checkBlockInput(input))) {
537
- _context3.next = 6;
569
+ _context4.next = 6;
538
570
  break;
539
571
  }
540
572
 
541
- return _context3.abrupt("return");
573
+ return _context4.abrupt("return");
542
574
 
543
575
  case 6:
544
576
  if (!input.id) input.id = (0, _uuid.v4)();
@@ -546,15 +578,15 @@ var Webchat = /*#__PURE__*/(0, _react.forwardRef)(function (props, ref) {
546
578
  if (messageComponent) addMessageComponent(messageComponent);
547
579
 
548
580
  if (!(0, _messageUtils.isMedia)(input)) {
549
- _context3.next = 13;
581
+ _context4.next = 13;
550
582
  break;
551
583
  }
552
584
 
553
- _context3.next = 12;
585
+ _context4.next = 12;
554
586
  return (0, _messageUtils.readDataURL)(input.data);
555
587
 
556
588
  case 12:
557
- input.data = _context3.sent;
589
+ input.data = _context4.sent;
558
590
 
559
591
  case 13:
560
592
  sendUserInput(input);
@@ -566,14 +598,14 @@ var Webchat = /*#__PURE__*/(0, _react.forwardRef)(function (props, ref) {
566
598
 
567
599
  case 19:
568
600
  case "end":
569
- return _context3.stop();
601
+ return _context4.stop();
570
602
  }
571
603
  }
572
- }, _callee3);
604
+ }, _callee4);
573
605
  }));
574
606
 
575
- return function sendInput(_x2) {
576
- return _ref5.apply(this, arguments);
607
+ return function sendInput(_x3) {
608
+ return _ref6.apply(this, arguments);
577
609
  };
578
610
  }();
579
611
  /* This is the public API this component exposes to its parents
@@ -589,10 +621,10 @@ var Webchat = /*#__PURE__*/(0, _react.forwardRef)(function (props, ref) {
589
621
 
590
622
  (0, _react.useImperativeHandle)(ref, function () {
591
623
  return {
592
- addBotResponse: function addBotResponse(_ref6) {
593
- var response = _ref6.response,
594
- session = _ref6.session,
595
- lastRoutePath = _ref6.lastRoutePath;
624
+ addBotResponse: function addBotResponse(_ref7) {
625
+ var response = _ref7.response,
626
+ session = _ref7.session,
627
+ lastRoutePath = _ref7.lastRoutePath;
596
628
  updateTyping(false);
597
629
  if (Array.isArray(response)) response.map(function (r) {
598
630
  return addMessageComponent(r);
@@ -692,18 +724,18 @@ var Webchat = /*#__PURE__*/(0, _react.forwardRef)(function (props, ref) {
692
724
  }, [webchatState.session._botonic_action]);
693
725
 
694
726
  var sendText = /*#__PURE__*/function () {
695
- var _ref7 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee4(text, payload) {
727
+ var _ref8 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee5(text, payload) {
696
728
  var input;
697
- return _regenerator["default"].wrap(function _callee4$(_context4) {
729
+ return _regenerator["default"].wrap(function _callee5$(_context5) {
698
730
  while (1) {
699
- switch (_context4.prev = _context4.next) {
731
+ switch (_context5.prev = _context5.next) {
700
732
  case 0:
701
733
  if (text) {
702
- _context4.next = 2;
734
+ _context5.next = 2;
703
735
  break;
704
736
  }
705
737
 
706
- return _context4.abrupt("return");
738
+ return _context5.abrupt("return");
707
739
 
708
740
  case 2:
709
741
  input = {
@@ -711,84 +743,84 @@ var Webchat = /*#__PURE__*/(0, _react.forwardRef)(function (props, ref) {
711
743
  data: text,
712
744
  payload: payload
713
745
  };
714
- _context4.next = 5;
746
+ _context5.next = 5;
715
747
  return sendInput(input);
716
748
 
717
749
  case 5:
718
750
  case "end":
719
- return _context4.stop();
751
+ return _context5.stop();
720
752
  }
721
753
  }
722
- }, _callee4);
754
+ }, _callee5);
723
755
  }));
724
756
 
725
- return function sendText(_x3, _x4) {
726
- return _ref7.apply(this, arguments);
757
+ return function sendText(_x4, _x5) {
758
+ return _ref8.apply(this, arguments);
727
759
  };
728
760
  }();
729
761
 
730
762
  var sendPayload = /*#__PURE__*/function () {
731
- var _ref8 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee5(payload) {
763
+ var _ref9 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee6(payload) {
732
764
  var input;
733
- return _regenerator["default"].wrap(function _callee5$(_context5) {
765
+ return _regenerator["default"].wrap(function _callee6$(_context6) {
734
766
  while (1) {
735
- switch (_context5.prev = _context5.next) {
767
+ switch (_context6.prev = _context6.next) {
736
768
  case 0:
737
769
  if (payload) {
738
- _context5.next = 2;
770
+ _context6.next = 2;
739
771
  break;
740
772
  }
741
773
 
742
- return _context5.abrupt("return");
774
+ return _context6.abrupt("return");
743
775
 
744
776
  case 2:
745
777
  input = {
746
778
  type: _core.INPUT.POSTBACK,
747
779
  payload: payload
748
780
  };
749
- _context5.next = 5;
781
+ _context6.next = 5;
750
782
  return sendInput(input);
751
783
 
752
784
  case 5:
753
785
  case "end":
754
- return _context5.stop();
786
+ return _context6.stop();
755
787
  }
756
788
  }
757
- }, _callee5);
789
+ }, _callee6);
758
790
  }));
759
791
 
760
- return function sendPayload(_x5) {
761
- return _ref8.apply(this, arguments);
792
+ return function sendPayload(_x6) {
793
+ return _ref9.apply(this, arguments);
762
794
  };
763
795
  }();
764
796
 
765
797
  var sendAttachment = /*#__PURE__*/function () {
766
- var _ref9 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee6(attachment) {
798
+ var _ref10 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee7(attachment) {
767
799
  var attachmentType, input;
768
- return _regenerator["default"].wrap(function _callee6$(_context6) {
800
+ return _regenerator["default"].wrap(function _callee7$(_context7) {
769
801
  while (1) {
770
- switch (_context6.prev = _context6.next) {
802
+ switch (_context7.prev = _context7.next) {
771
803
  case 0:
772
804
  if (!attachment.file) {
773
- _context6.next = 8;
805
+ _context7.next = 8;
774
806
  break;
775
807
  }
776
808
 
777
809
  attachmentType = (0, _messageUtils.getMediaType)(attachment.file.type);
778
810
 
779
811
  if (attachmentType) {
780
- _context6.next = 4;
812
+ _context7.next = 4;
781
813
  break;
782
814
  }
783
815
 
784
- return _context6.abrupt("return");
816
+ return _context7.abrupt("return");
785
817
 
786
818
  case 4:
787
819
  input = {
788
820
  type: attachmentType,
789
821
  data: attachment.file
790
822
  };
791
- _context6.next = 7;
823
+ _context7.next = 7;
792
824
  return sendInput(input);
793
825
 
794
826
  case 7:
@@ -796,14 +828,14 @@ var Webchat = /*#__PURE__*/(0, _react.forwardRef)(function (props, ref) {
796
828
 
797
829
  case 8:
798
830
  case "end":
799
- return _context6.stop();
831
+ return _context7.stop();
800
832
  }
801
833
  }
802
- }, _callee6);
834
+ }, _callee7);
803
835
  }));
804
836
 
805
- return function sendAttachment(_x6) {
806
- return _ref9.apply(this, arguments);
837
+ return function sendAttachment(_x7) {
838
+ return _ref10.apply(this, arguments);
807
839
  };
808
840
  }();
809
841
 
@@ -812,13 +844,47 @@ var Webchat = /*#__PURE__*/(0, _react.forwardRef)(function (props, ref) {
812
844
  textArea.current.value = '';
813
845
  };
814
846
 
847
+ var isTyping = false;
848
+ var typingTimeout = null;
849
+
850
+ function clearTimeoutWithReset(reset) {
851
+ var waitTime = 20 * 1000;
852
+ if (typingTimeout) clearTimeout(typingTimeout);
853
+ if (reset) typingTimeout = setTimeout(stopTyping, waitTime);
854
+ }
855
+
856
+ function startTyping() {
857
+ isTyping = true;
858
+ sendChatEvent('typing_on');
859
+ }
860
+
861
+ function stopTyping() {
862
+ clearTimeoutWithReset(false);
863
+ isTyping = false;
864
+ sendChatEvent('typing_off');
865
+ }
866
+
815
867
  var _onKeyDown = function onKeyDown(event) {
816
- if (event.keyCode == 13 && event.shiftKey == false) {
868
+ if (event.keyCode === 13 && event.shiftKey === false) {
817
869
  event.preventDefault();
818
870
  sendTextAreaText();
871
+ stopTyping();
819
872
  }
820
873
  };
821
874
 
875
+ var onKeyUp = function onKeyUp() {
876
+ if (textArea.current.value === '') {
877
+ stopTyping();
878
+ return;
879
+ }
880
+
881
+ if (!isTyping) {
882
+ startTyping();
883
+ }
884
+
885
+ clearTimeoutWithReset(true);
886
+ };
887
+
822
888
  var webviewRequestContext = {
823
889
  getString: function getString(stringId) {
824
890
  return props.getString(stringId, webchatState.session);
@@ -940,6 +1006,7 @@ var Webchat = /*#__PURE__*/(0, _react.forwardRef)(function (props, ref) {
940
1006
  onKeyDown: function onKeyDown(e) {
941
1007
  return _onKeyDown(e);
942
1008
  },
1009
+ onKeyUp: onKeyUp,
943
1010
  style: _objectSpread({
944
1011
  display: 'flex',
945
1012
  fontSize: deviceAdapter.fontSize(14),
@@ -1000,8 +1067,8 @@ var Webchat = /*#__PURE__*/(0, _react.forwardRef)(function (props, ref) {
1000
1067
  }, [webchatState.messagesJSON]);
1001
1068
  };
1002
1069
 
1003
- var DarkenBackground = function DarkenBackground(_ref10) {
1004
- var component = _ref10.component;
1070
+ var DarkenBackground = function DarkenBackground(_ref11) {
1071
+ var component = _ref11.component;
1005
1072
  return /*#__PURE__*/_react["default"].createElement("div", null, darkBackgroundMenu && /*#__PURE__*/_react["default"].createElement(DarkBackgroundMenu, {
1006
1073
  style: {
1007
1074
  borderRadius: webchatState.theme.style.borderRadius