@botonic/react 0.23.0 → 0.23.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.
Files changed (282) hide show
  1. package/lib/cjs/botonic-tester.js +2 -2
  2. package/lib/cjs/botonic-tester.js.map +1 -1
  3. package/lib/cjs/components/audio.js +2 -2
  4. package/lib/cjs/components/audio.js.map +1 -1
  5. package/lib/cjs/components/button.js +2 -2
  6. package/lib/cjs/components/button.js.map +1 -1
  7. package/lib/cjs/components/carousel.js +3 -3
  8. package/lib/cjs/components/carousel.js.map +1 -1
  9. package/lib/cjs/components/custom-message.js +1 -1
  10. package/lib/cjs/components/custom-message.js.map +1 -1
  11. package/lib/cjs/components/document.js +2 -2
  12. package/lib/cjs/components/document.js.map +1 -1
  13. package/lib/cjs/components/handoff.js +2 -2
  14. package/lib/cjs/components/handoff.js.map +1 -1
  15. package/lib/cjs/components/image.js +4 -2
  16. package/lib/cjs/components/image.js.map +1 -1
  17. package/lib/cjs/components/index-types.d.ts +8 -1
  18. package/lib/cjs/components/index-types.js.map +1 -1
  19. package/lib/cjs/components/location.js +2 -2
  20. package/lib/cjs/components/location.js.map +1 -1
  21. package/lib/cjs/components/message-template.js +1 -1
  22. package/lib/cjs/components/message-template.js.map +1 -1
  23. package/lib/cjs/components/message.js +3 -3
  24. package/lib/cjs/components/message.js.map +1 -1
  25. package/lib/cjs/components/multichannel/index-types.d.ts +1 -0
  26. package/lib/cjs/components/multichannel/multichannel-carousel.js +1 -1
  27. package/lib/cjs/components/multichannel/multichannel-carousel.js.map +1 -1
  28. package/lib/cjs/components/multichannel/multichannel-text.js +43 -19
  29. package/lib/cjs/components/multichannel/multichannel-text.js.map +1 -1
  30. package/lib/cjs/components/multichannel/multichannel-utils.d.ts +6 -4
  31. package/lib/cjs/components/multichannel/multichannel-utils.js +3 -1
  32. package/lib/cjs/components/multichannel/multichannel-utils.js.map +1 -1
  33. package/lib/cjs/components/multichannel/multichannel.js +2 -2
  34. package/lib/cjs/components/multichannel/multichannel.js.map +1 -1
  35. package/lib/cjs/components/raw.js +2 -2
  36. package/lib/cjs/components/raw.js.map +1 -1
  37. package/lib/cjs/components/reply.js +4 -4
  38. package/lib/cjs/components/reply.js.map +1 -1
  39. package/lib/cjs/components/share-button.js +1 -1
  40. package/lib/cjs/components/share-button.js.map +1 -1
  41. package/lib/cjs/components/subtitle.js +1 -1
  42. package/lib/cjs/components/subtitle.js.map +1 -1
  43. package/lib/cjs/components/text.js +2 -2
  44. package/lib/cjs/components/text.js.map +1 -1
  45. package/lib/cjs/components/timestamps.js +1 -1
  46. package/lib/cjs/components/timestamps.js.map +1 -1
  47. package/lib/cjs/components/title.js +1 -1
  48. package/lib/cjs/components/title.js.map +1 -1
  49. package/lib/cjs/components/video.js +2 -2
  50. package/lib/cjs/components/video.js.map +1 -1
  51. package/lib/cjs/components/webchat-settings.d.ts +3 -10
  52. package/lib/cjs/components/webchat-settings.js +9 -14
  53. package/lib/cjs/components/webchat-settings.js.map +1 -1
  54. package/lib/cjs/components/whatsapp-button-list.d.ts +1 -1
  55. package/lib/cjs/components/whatsapp-button-list.js +1 -1
  56. package/lib/cjs/components/whatsapp-button-list.js.map +1 -1
  57. package/lib/cjs/components/whatsapp-template.js +1 -1
  58. package/lib/cjs/components/whatsapp-template.js.map +1 -1
  59. package/lib/cjs/constants.d.ts +148 -148
  60. package/lib/cjs/contexts.d.ts +4 -62
  61. package/lib/cjs/contexts.js +45 -19
  62. package/lib/cjs/contexts.js.map +1 -1
  63. package/lib/cjs/index-types.d.ts +12 -12
  64. package/lib/cjs/msg-to-botonic.js +2 -2
  65. package/lib/cjs/msg-to-botonic.js.map +1 -1
  66. package/lib/cjs/react-bot.js +1 -1
  67. package/lib/cjs/react-bot.js.map +1 -1
  68. package/lib/cjs/util/environment.js +1 -1
  69. package/lib/cjs/util/environment.js.map +1 -1
  70. package/lib/cjs/webchat/actions.d.ts +25 -23
  71. package/lib/cjs/webchat/actions.js +27 -24
  72. package/lib/cjs/webchat/actions.js.map +1 -1
  73. package/lib/cjs/webchat/components/attachment.js +1 -1
  74. package/lib/cjs/webchat/components/attachment.js.map +1 -1
  75. package/lib/cjs/webchat/components/conditional-animation.js +1 -1
  76. package/lib/cjs/webchat/components/conditional-animation.js.map +1 -1
  77. package/lib/cjs/webchat/components/emoji-picker.js +2 -2
  78. package/lib/cjs/webchat/components/emoji-picker.js.map +1 -1
  79. package/lib/cjs/webchat/components/persistent-menu.js +4 -4
  80. package/lib/cjs/webchat/components/persistent-menu.js.map +1 -1
  81. package/lib/cjs/webchat/components/send-button.js +1 -1
  82. package/lib/cjs/webchat/components/send-button.js.map +1 -1
  83. package/lib/cjs/webchat/components/typing-indicator.js +1 -1
  84. package/lib/cjs/webchat/components/typing-indicator.js.map +1 -1
  85. package/lib/cjs/webchat/header.js +1 -1
  86. package/lib/cjs/webchat/header.js.map +1 -1
  87. package/lib/cjs/webchat/hooks/index.d.ts +5 -0
  88. package/lib/cjs/webchat/hooks/index.js +15 -0
  89. package/lib/cjs/webchat/hooks/index.js.map +1 -0
  90. package/lib/cjs/webchat/hooks/use-component-visible.d.ts +8 -0
  91. package/lib/cjs/webchat/hooks/use-component-visible.js +24 -0
  92. package/lib/cjs/webchat/hooks/use-component-visible.js.map +1 -0
  93. package/lib/cjs/webchat/hooks/use-component-will-mount.d.ts +1 -0
  94. package/lib/cjs/webchat/hooks/use-component-will-mount.js +9 -0
  95. package/lib/cjs/webchat/hooks/use-component-will-mount.js.map +1 -0
  96. package/lib/cjs/webchat/hooks/use-previous.d.ts +1 -0
  97. package/lib/cjs/webchat/hooks/use-previous.js +13 -0
  98. package/lib/cjs/webchat/hooks/use-previous.js.map +1 -0
  99. package/lib/cjs/webchat/hooks/use-typing.d.ts +10 -0
  100. package/lib/cjs/webchat/hooks/use-typing.js +32 -0
  101. package/lib/cjs/webchat/hooks/use-typing.js.map +1 -0
  102. package/lib/cjs/webchat/hooks/use-webchat.d.ts +37 -0
  103. package/lib/cjs/webchat/hooks/use-webchat.js +167 -0
  104. package/lib/cjs/webchat/hooks/use-webchat.js.map +1 -0
  105. package/lib/cjs/webchat/index-types.d.ts +16 -10
  106. package/lib/cjs/webchat/message-list.js +2 -2
  107. package/lib/cjs/webchat/message-list.js.map +1 -1
  108. package/lib/cjs/webchat/messages-reducer.d.ts +6 -1
  109. package/lib/cjs/webchat/messages-reducer.js +8 -8
  110. package/lib/cjs/webchat/messages-reducer.js.map +1 -1
  111. package/lib/cjs/webchat/replies.js +1 -1
  112. package/lib/cjs/webchat/replies.js.map +1 -1
  113. package/lib/cjs/webchat/session-view.js +2 -2
  114. package/lib/cjs/webchat/session-view.js.map +1 -1
  115. package/lib/cjs/webchat/trigger-button/index.js +1 -3
  116. package/lib/cjs/webchat/trigger-button/index.js.map +1 -1
  117. package/lib/cjs/webchat/webchat-reducer.d.ts +6 -1
  118. package/lib/cjs/webchat/webchat-reducer.js +17 -17
  119. package/lib/cjs/webchat/webchat-reducer.js.map +1 -1
  120. package/lib/cjs/webchat/webchat.js +24 -22
  121. package/lib/cjs/webchat/webchat.js.map +1 -1
  122. package/lib/cjs/webchat/webview.js +4 -4
  123. package/lib/cjs/webchat/webview.js.map +1 -1
  124. package/lib/cjs/webview-app.js +1 -1
  125. package/lib/cjs/webview-app.js.map +1 -1
  126. package/lib/esm/botonic-tester.js +2 -2
  127. package/lib/esm/botonic-tester.js.map +1 -1
  128. package/lib/esm/components/audio.js +2 -2
  129. package/lib/esm/components/audio.js.map +1 -1
  130. package/lib/esm/components/button.js +2 -2
  131. package/lib/esm/components/button.js.map +1 -1
  132. package/lib/esm/components/carousel.js +3 -3
  133. package/lib/esm/components/carousel.js.map +1 -1
  134. package/lib/esm/components/custom-message.js +1 -1
  135. package/lib/esm/components/custom-message.js.map +1 -1
  136. package/lib/esm/components/document.js +2 -2
  137. package/lib/esm/components/document.js.map +1 -1
  138. package/lib/esm/components/handoff.js +2 -2
  139. package/lib/esm/components/handoff.js.map +1 -1
  140. package/lib/esm/components/image.js +4 -2
  141. package/lib/esm/components/image.js.map +1 -1
  142. package/lib/esm/components/index-types.d.ts +8 -1
  143. package/lib/esm/components/index-types.js.map +1 -1
  144. package/lib/esm/components/location.js +2 -2
  145. package/lib/esm/components/location.js.map +1 -1
  146. package/lib/esm/components/message-template.js +1 -1
  147. package/lib/esm/components/message-template.js.map +1 -1
  148. package/lib/esm/components/message.js +3 -3
  149. package/lib/esm/components/message.js.map +1 -1
  150. package/lib/esm/components/multichannel/index-types.d.ts +1 -0
  151. package/lib/esm/components/multichannel/multichannel-carousel.js +1 -1
  152. package/lib/esm/components/multichannel/multichannel-carousel.js.map +1 -1
  153. package/lib/esm/components/multichannel/multichannel-text.js +45 -21
  154. package/lib/esm/components/multichannel/multichannel-text.js.map +1 -1
  155. package/lib/esm/components/multichannel/multichannel-utils.d.ts +6 -4
  156. package/lib/esm/components/multichannel/multichannel-utils.js +2 -0
  157. package/lib/esm/components/multichannel/multichannel-utils.js.map +1 -1
  158. package/lib/esm/components/multichannel/multichannel.js +2 -2
  159. package/lib/esm/components/multichannel/multichannel.js.map +1 -1
  160. package/lib/esm/components/raw.js +2 -2
  161. package/lib/esm/components/raw.js.map +1 -1
  162. package/lib/esm/components/reply.js +4 -4
  163. package/lib/esm/components/reply.js.map +1 -1
  164. package/lib/esm/components/share-button.js +1 -1
  165. package/lib/esm/components/share-button.js.map +1 -1
  166. package/lib/esm/components/subtitle.js +1 -1
  167. package/lib/esm/components/subtitle.js.map +1 -1
  168. package/lib/esm/components/text.js +2 -2
  169. package/lib/esm/components/text.js.map +1 -1
  170. package/lib/esm/components/timestamps.js +1 -1
  171. package/lib/esm/components/timestamps.js.map +1 -1
  172. package/lib/esm/components/title.js +1 -1
  173. package/lib/esm/components/title.js.map +1 -1
  174. package/lib/esm/components/video.js +2 -2
  175. package/lib/esm/components/video.js.map +1 -1
  176. package/lib/esm/components/webchat-settings.d.ts +3 -10
  177. package/lib/esm/components/webchat-settings.js +9 -13
  178. package/lib/esm/components/webchat-settings.js.map +1 -1
  179. package/lib/esm/components/whatsapp-button-list.d.ts +1 -1
  180. package/lib/esm/components/whatsapp-button-list.js +1 -1
  181. package/lib/esm/components/whatsapp-button-list.js.map +1 -1
  182. package/lib/esm/components/whatsapp-template.js +1 -1
  183. package/lib/esm/components/whatsapp-template.js.map +1 -1
  184. package/lib/esm/constants.d.ts +148 -148
  185. package/lib/esm/contexts.d.ts +4 -62
  186. package/lib/esm/contexts.js +45 -19
  187. package/lib/esm/contexts.js.map +1 -1
  188. package/lib/esm/index-types.d.ts +12 -12
  189. package/lib/esm/msg-to-botonic.js +2 -2
  190. package/lib/esm/msg-to-botonic.js.map +1 -1
  191. package/lib/esm/react-bot.js +1 -1
  192. package/lib/esm/react-bot.js.map +1 -1
  193. package/lib/esm/util/environment.js +1 -1
  194. package/lib/esm/util/environment.js.map +1 -1
  195. package/lib/esm/webchat/actions.d.ts +25 -23
  196. package/lib/esm/webchat/actions.js +26 -23
  197. package/lib/esm/webchat/actions.js.map +1 -1
  198. package/lib/esm/webchat/components/attachment.js +1 -1
  199. package/lib/esm/webchat/components/attachment.js.map +1 -1
  200. package/lib/esm/webchat/components/conditional-animation.js +1 -1
  201. package/lib/esm/webchat/components/conditional-animation.js.map +1 -1
  202. package/lib/esm/webchat/components/emoji-picker.js +2 -2
  203. package/lib/esm/webchat/components/emoji-picker.js.map +1 -1
  204. package/lib/esm/webchat/components/persistent-menu.js +4 -4
  205. package/lib/esm/webchat/components/persistent-menu.js.map +1 -1
  206. package/lib/esm/webchat/components/send-button.js +1 -1
  207. package/lib/esm/webchat/components/send-button.js.map +1 -1
  208. package/lib/esm/webchat/components/typing-indicator.js +1 -1
  209. package/lib/esm/webchat/components/typing-indicator.js.map +1 -1
  210. package/lib/esm/webchat/header.js +1 -1
  211. package/lib/esm/webchat/header.js.map +1 -1
  212. package/lib/esm/webchat/hooks/index.d.ts +5 -0
  213. package/lib/esm/webchat/hooks/index.js +6 -0
  214. package/lib/esm/webchat/hooks/index.js.map +1 -0
  215. package/lib/esm/webchat/hooks/use-component-visible.d.ts +8 -0
  216. package/lib/esm/webchat/hooks/use-component-visible.js +20 -0
  217. package/lib/esm/webchat/hooks/use-component-visible.js.map +1 -0
  218. package/lib/esm/webchat/hooks/use-component-will-mount.d.ts +1 -0
  219. package/lib/esm/webchat/hooks/use-component-will-mount.js +5 -0
  220. package/lib/esm/webchat/hooks/use-component-will-mount.js.map +1 -0
  221. package/lib/esm/webchat/hooks/use-previous.d.ts +1 -0
  222. package/lib/esm/webchat/hooks/use-previous.js +9 -0
  223. package/lib/esm/webchat/hooks/use-previous.js.map +1 -0
  224. package/lib/esm/webchat/hooks/use-typing.d.ts +10 -0
  225. package/lib/esm/webchat/hooks/use-typing.js +28 -0
  226. package/lib/esm/webchat/hooks/use-typing.js.map +1 -0
  227. package/lib/esm/webchat/hooks/use-webchat.d.ts +37 -0
  228. package/lib/esm/webchat/hooks/use-webchat.js +163 -0
  229. package/lib/esm/webchat/hooks/use-webchat.js.map +1 -0
  230. package/lib/esm/webchat/index-types.d.ts +16 -10
  231. package/lib/esm/webchat/message-list.js +2 -2
  232. package/lib/esm/webchat/message-list.js.map +1 -1
  233. package/lib/esm/webchat/messages-reducer.d.ts +6 -1
  234. package/lib/esm/webchat/messages-reducer.js +9 -9
  235. package/lib/esm/webchat/messages-reducer.js.map +1 -1
  236. package/lib/esm/webchat/replies.js +1 -1
  237. package/lib/esm/webchat/replies.js.map +1 -1
  238. package/lib/esm/webchat/session-view.js +2 -2
  239. package/lib/esm/webchat/session-view.js.map +1 -1
  240. package/lib/esm/webchat/trigger-button/index.js +1 -3
  241. package/lib/esm/webchat/trigger-button/index.js.map +1 -1
  242. package/lib/esm/webchat/webchat-reducer.d.ts +6 -1
  243. package/lib/esm/webchat/webchat-reducer.js +18 -18
  244. package/lib/esm/webchat/webchat-reducer.js.map +1 -1
  245. package/lib/esm/webchat/webchat.js +24 -22
  246. package/lib/esm/webchat/webchat.js.map +1 -1
  247. package/lib/esm/webchat/webview.js +4 -4
  248. package/lib/esm/webchat/webview.js.map +1 -1
  249. package/lib/esm/webview-app.js +1 -1
  250. package/lib/esm/webview-app.js.map +1 -1
  251. package/package.json +2 -2
  252. package/src/components/image.jsx +3 -1
  253. package/src/components/index-types.ts +8 -1
  254. package/src/components/multichannel/index-types.ts +1 -0
  255. package/src/components/multichannel/multichannel-text.jsx +92 -28
  256. package/src/components/multichannel/multichannel-utils.js +2 -0
  257. package/src/components/{webchat-settings.jsx → webchat-settings.tsx} +8 -8
  258. package/src/components/whatsapp-button-list.tsx +1 -1
  259. package/src/contexts.tsx +64 -0
  260. package/src/index-types.ts +12 -12
  261. package/src/util/environment.js +1 -1
  262. package/src/webchat/actions.ts +25 -0
  263. package/src/webchat/hooks/index.ts +5 -0
  264. package/src/webchat/hooks/use-component-visible.ts +29 -0
  265. package/src/webchat/hooks/use-component-will-mount.ts +5 -0
  266. package/src/webchat/hooks/use-previous.ts +9 -0
  267. package/src/webchat/hooks/use-typing.ts +43 -0
  268. package/src/webchat/hooks/use-webchat.ts +213 -0
  269. package/src/webchat/index-types.ts +17 -8
  270. package/src/webchat/{messages-reducer.js → messages-reducer.ts} +25 -19
  271. package/src/webchat/trigger-button/index.tsx +0 -1
  272. package/src/webchat/{webchat-reducer.js → webchat-reducer.ts} +23 -37
  273. package/src/webchat/webchat.jsx +12 -11
  274. package/lib/cjs/webchat/hooks.d.ts +0 -81
  275. package/lib/cjs/webchat/hooks.js +0 -222
  276. package/lib/cjs/webchat/hooks.js.map +0 -1
  277. package/lib/esm/webchat/hooks.d.ts +0 -81
  278. package/lib/esm/webchat/hooks.js +0 -214
  279. package/lib/esm/webchat/hooks.js.map +0 -1
  280. package/src/contexts.jsx +0 -37
  281. package/src/webchat/actions.jsx +0 -23
  282. package/src/webchat/hooks.js +0 -270
@@ -1,270 +0,0 @@
1
- import { useEffect, useMemo, useReducer, useRef, useState } from 'react'
2
-
3
- import { COLORS, WEBCHAT } from '../constants'
4
- import { scrollToBottom } from '../util/dom'
5
- import {
6
- ADD_MESSAGE,
7
- ADD_MESSAGE_COMPONENT,
8
- CLEAR_MESSAGES,
9
- DO_RENDER_CUSTOM_COMPONENT,
10
- SET_CURRENT_ATTACHMENT,
11
- SET_ERROR,
12
- SET_ONLINE,
13
- TOGGLE_COVER_COMPONENT,
14
- TOGGLE_EMOJI_PICKER,
15
- TOGGLE_PERSISTENT_MENU,
16
- TOGGLE_WEBCHAT,
17
- UPDATE_DEV_SETTINGS,
18
- UPDATE_HANDOFF,
19
- UPDATE_JWT,
20
- UPDATE_LAST_MESSAGE_DATE,
21
- UPDATE_LAST_ROUTE_PATH,
22
- UPDATE_LATEST_INPUT,
23
- UPDATE_MESSAGE,
24
- UPDATE_REPLIES,
25
- UPDATE_SESSION,
26
- UPDATE_THEME,
27
- UPDATE_TYPING,
28
- UPDATE_WEBVIEW,
29
- } from './actions'
30
- import { webchatReducer } from './webchat-reducer'
31
-
32
- export const webchatInitialState = {
33
- width: WEBCHAT.DEFAULTS.WIDTH,
34
- height: WEBCHAT.DEFAULTS.HEIGHT,
35
- messagesJSON: [],
36
- messagesComponents: [],
37
- replies: [],
38
- latestInput: {},
39
- typing: false,
40
- webview: null,
41
- webviewParams: null,
42
- session: { user: null },
43
- lastRoutePath: null,
44
- handoff: false,
45
- theme: {
46
- headerTitle: WEBCHAT.DEFAULTS.TITLE,
47
- brandColor: COLORS.BOTONIC_BLUE,
48
- brandImage: WEBCHAT.DEFAULTS.LOGO,
49
- triggerButtonImage: undefined,
50
- textPlaceholder: WEBCHAT.DEFAULTS.PLACEHOLDER,
51
- style: {
52
- fontFamily: WEBCHAT.DEFAULTS.FONT_FAMILY,
53
- },
54
- },
55
- themeUpdates: {},
56
- error: {},
57
- online: true,
58
- devSettings: { keepSessionOnReload: false },
59
- isWebchatOpen: false,
60
- isEmojiPickerOpen: false,
61
- isPersistentMenuOpen: false,
62
- isCoverComponentOpen: false,
63
- isCustomComponentRendered: false,
64
- lastMessageUpdate: undefined,
65
- currentAttachment: undefined,
66
- jwt: null,
67
- unreadMessages: 0,
68
- }
69
-
70
- export function useWebchat() {
71
- const [webchatState, webchatDispatch] = useReducer(
72
- webchatReducer,
73
- webchatInitialState
74
- )
75
- const addMessage = message =>
76
- webchatDispatch({ type: ADD_MESSAGE, payload: message })
77
- const addMessageComponent = message =>
78
- webchatDispatch({ type: ADD_MESSAGE_COMPONENT, payload: message })
79
- const updateMessage = message =>
80
- webchatDispatch({ type: UPDATE_MESSAGE, payload: message })
81
- const updateReplies = replies =>
82
- webchatDispatch({ type: UPDATE_REPLIES, payload: replies })
83
- const updateLatestInput = input =>
84
- webchatDispatch({ type: UPDATE_LATEST_INPUT, payload: input })
85
- const updateTyping = typing =>
86
- webchatDispatch({ type: UPDATE_TYPING, payload: typing })
87
- const updateWebview = (webview, params) =>
88
- webchatDispatch({
89
- type: UPDATE_WEBVIEW,
90
- payload: { webview, webviewParams: params },
91
- })
92
- const updateSession = session => {
93
- webchatDispatch({
94
- type: UPDATE_SESSION,
95
- payload: session,
96
- })
97
- }
98
-
99
- const updateLastRoutePath = path =>
100
- webchatDispatch({
101
- type: UPDATE_LAST_ROUTE_PATH,
102
- payload: path,
103
- })
104
- const updateHandoff = handoff =>
105
- webchatDispatch({
106
- type: UPDATE_HANDOFF,
107
- payload: handoff,
108
- })
109
- const updateTheme = (theme, themeUpdates = undefined) => {
110
- const payload =
111
- themeUpdates !== undefined ? { theme, themeUpdates } : { theme }
112
- webchatDispatch({
113
- type: UPDATE_THEME,
114
- payload,
115
- })
116
- }
117
- const updateDevSettings = settings =>
118
- webchatDispatch({
119
- type: UPDATE_DEV_SETTINGS,
120
- payload: settings,
121
- })
122
-
123
- const toggleWebchat = toggle => {
124
- webchatDispatch({
125
- type: TOGGLE_WEBCHAT,
126
- payload: toggle,
127
- })
128
- }
129
-
130
- const toggleEmojiPicker = toggle =>
131
- webchatDispatch({
132
- type: TOGGLE_EMOJI_PICKER,
133
- payload: toggle,
134
- })
135
- const togglePersistentMenu = toggle =>
136
- webchatDispatch({
137
- type: TOGGLE_PERSISTENT_MENU,
138
- payload: toggle,
139
- })
140
- const toggleCoverComponent = toggle =>
141
- webchatDispatch({
142
- type: TOGGLE_COVER_COMPONENT,
143
- payload: toggle,
144
- })
145
- const doRenderCustomComponent = toggle =>
146
- webchatDispatch({
147
- type: DO_RENDER_CUSTOM_COMPONENT,
148
- payload: toggle,
149
- })
150
- const setError = error =>
151
- webchatDispatch({
152
- type: SET_ERROR,
153
- payload: error,
154
- })
155
- const setOnline = online =>
156
- webchatDispatch({
157
- type: SET_ONLINE,
158
- payload: online,
159
- })
160
-
161
- const clearMessages = () => {
162
- webchatDispatch({
163
- type: CLEAR_MESSAGES,
164
- })
165
- }
166
- const updateLastMessageDate = date => {
167
- webchatDispatch({
168
- type: UPDATE_LAST_MESSAGE_DATE,
169
- payload: date,
170
- })
171
- }
172
- const setCurrentAttachment = attachment => {
173
- webchatDispatch({
174
- type: SET_CURRENT_ATTACHMENT,
175
- payload: attachment,
176
- })
177
- }
178
-
179
- const updateJwt = jwt => {
180
- webchatDispatch({
181
- type: UPDATE_JWT,
182
- payload: jwt,
183
- })
184
- }
185
-
186
- return {
187
- webchatState,
188
- webchatDispatch,
189
- addMessage,
190
- addMessageComponent,
191
- updateMessage,
192
- updateReplies,
193
- updateLatestInput,
194
- updateTyping,
195
- updateWebview,
196
- updateSession,
197
- updateLastRoutePath,
198
- updateHandoff,
199
- updateTheme,
200
- updateDevSettings,
201
- toggleWebchat,
202
- toggleEmojiPicker,
203
- togglePersistentMenu,
204
- toggleCoverComponent,
205
- doRenderCustomComponent,
206
- setError,
207
- setOnline,
208
- clearMessages,
209
- updateLastMessageDate,
210
- setCurrentAttachment,
211
- updateJwt,
212
- }
213
- }
214
-
215
- export function useTyping({ webchatState, updateTyping, updateMessage, host }) {
216
- useEffect(() => {
217
- let delayTimeout, typingTimeout
218
- scrollToBottom({ host })
219
- try {
220
- const nextMsg = webchatState.messagesJSON.filter(m => !m.display)[0]
221
- if (nextMsg.delay && nextMsg.typing) {
222
- delayTimeout = setTimeout(
223
- () => updateTyping(true),
224
- nextMsg.delay * 1000
225
- )
226
- } else if (nextMsg.typing) updateTyping(true)
227
- const totalDelay = nextMsg.delay + nextMsg.typing
228
- if (totalDelay)
229
- typingTimeout = setTimeout(() => {
230
- updateMessage({ ...nextMsg, display: true })
231
- updateTyping(false)
232
- }, totalDelay * 1000)
233
- } catch (e) {}
234
- return () => {
235
- clearTimeout(delayTimeout)
236
- clearTimeout(typingTimeout)
237
- }
238
- }, [webchatState.messagesJSON, webchatState.typing])
239
- }
240
-
241
- export function usePrevious(value) {
242
- const ref = useRef()
243
- useEffect(() => {
244
- ref.current = value
245
- })
246
- return ref.current
247
- }
248
-
249
- export function useComponentVisible(initialIsVisible, onClickOutside) {
250
- const [isComponentVisible, setIsComponentVisible] = useState(initialIsVisible)
251
- const ref = useRef(null)
252
- const handleClickOutside = event => {
253
- const target = event.path ? event.path[0] : event.target
254
- if (ref.current && !ref.current.contains(target)) {
255
- setIsComponentVisible(false)
256
- onClickOutside()
257
- }
258
- }
259
- useEffect(() => {
260
- document.addEventListener('click', handleClickOutside, false)
261
- return () => {
262
- document.removeEventListener('click', handleClickOutside, false)
263
- }
264
- })
265
- return { ref, isComponentVisible, setIsComponentVisible }
266
- }
267
-
268
- export const useComponentWillMount = func => {
269
- useMemo(func, [])
270
- }