@botonic/react 0.23.1-alpha.0 → 0.23.2-alpha.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.
Files changed (261) 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 +1 -1
  16. package/lib/cjs/components/image.js.map +1 -1
  17. package/lib/cjs/components/index-types.d.ts +1 -1
  18. package/lib/cjs/components/location.js +2 -2
  19. package/lib/cjs/components/location.js.map +1 -1
  20. package/lib/cjs/components/message-template.js +1 -1
  21. package/lib/cjs/components/message-template.js.map +1 -1
  22. package/lib/cjs/components/message.js +3 -3
  23. package/lib/cjs/components/message.js.map +1 -1
  24. package/lib/cjs/components/multichannel/multichannel-carousel.js +1 -1
  25. package/lib/cjs/components/multichannel/multichannel-carousel.js.map +1 -1
  26. package/lib/cjs/components/multichannel/multichannel-text.js +6 -17
  27. package/lib/cjs/components/multichannel/multichannel-text.js.map +1 -1
  28. package/lib/cjs/components/multichannel/multichannel-utils.d.ts +5 -5
  29. package/lib/cjs/components/multichannel/multichannel-utils.js +2 -2
  30. package/lib/cjs/components/multichannel/multichannel-utils.js.map +1 -1
  31. package/lib/cjs/components/multichannel/multichannel.js +2 -2
  32. package/lib/cjs/components/multichannel/multichannel.js.map +1 -1
  33. package/lib/cjs/components/raw.js +2 -2
  34. package/lib/cjs/components/raw.js.map +1 -1
  35. package/lib/cjs/components/reply.js +4 -4
  36. package/lib/cjs/components/reply.js.map +1 -1
  37. package/lib/cjs/components/share-button.js +1 -1
  38. package/lib/cjs/components/share-button.js.map +1 -1
  39. package/lib/cjs/components/subtitle.js +1 -1
  40. package/lib/cjs/components/subtitle.js.map +1 -1
  41. package/lib/cjs/components/text.js +2 -2
  42. package/lib/cjs/components/text.js.map +1 -1
  43. package/lib/cjs/components/timestamps.js +1 -1
  44. package/lib/cjs/components/timestamps.js.map +1 -1
  45. package/lib/cjs/components/title.js +1 -1
  46. package/lib/cjs/components/title.js.map +1 -1
  47. package/lib/cjs/components/video.js +2 -2
  48. package/lib/cjs/components/video.js.map +1 -1
  49. package/lib/cjs/components/whatsapp-button-list.js +1 -1
  50. package/lib/cjs/components/whatsapp-button-list.js.map +1 -1
  51. package/lib/cjs/components/whatsapp-template.js +1 -1
  52. package/lib/cjs/components/whatsapp-template.js.map +1 -1
  53. package/lib/cjs/constants.d.ts +148 -148
  54. package/lib/cjs/contexts.d.ts +4 -62
  55. package/lib/cjs/contexts.js +45 -19
  56. package/lib/cjs/contexts.js.map +1 -1
  57. package/lib/cjs/index-types.d.ts +12 -12
  58. package/lib/cjs/msg-to-botonic.js +2 -2
  59. package/lib/cjs/msg-to-botonic.js.map +1 -1
  60. package/lib/cjs/react-bot.js +1 -1
  61. package/lib/cjs/react-bot.js.map +1 -1
  62. package/lib/cjs/webchat/actions.d.ts +25 -23
  63. package/lib/cjs/webchat/actions.js +27 -24
  64. package/lib/cjs/webchat/actions.js.map +1 -1
  65. package/lib/cjs/webchat/components/attachment.js +1 -1
  66. package/lib/cjs/webchat/components/attachment.js.map +1 -1
  67. package/lib/cjs/webchat/components/conditional-animation.js +1 -1
  68. package/lib/cjs/webchat/components/conditional-animation.js.map +1 -1
  69. package/lib/cjs/webchat/components/emoji-picker.js +2 -2
  70. package/lib/cjs/webchat/components/emoji-picker.js.map +1 -1
  71. package/lib/cjs/webchat/components/persistent-menu.js +4 -4
  72. package/lib/cjs/webchat/components/persistent-menu.js.map +1 -1
  73. package/lib/cjs/webchat/components/send-button.js +1 -1
  74. package/lib/cjs/webchat/components/send-button.js.map +1 -1
  75. package/lib/cjs/webchat/components/typing-indicator.js +1 -1
  76. package/lib/cjs/webchat/components/typing-indicator.js.map +1 -1
  77. package/lib/cjs/webchat/header.js +1 -1
  78. package/lib/cjs/webchat/header.js.map +1 -1
  79. package/lib/cjs/webchat/hooks/index.d.ts +5 -0
  80. package/lib/cjs/webchat/hooks/index.js +15 -0
  81. package/lib/cjs/webchat/hooks/index.js.map +1 -0
  82. package/lib/cjs/webchat/hooks/use-component-visible.d.ts +8 -0
  83. package/lib/cjs/webchat/hooks/use-component-visible.js +24 -0
  84. package/lib/cjs/webchat/hooks/use-component-visible.js.map +1 -0
  85. package/lib/cjs/webchat/hooks/use-component-will-mount.d.ts +1 -0
  86. package/lib/cjs/webchat/hooks/use-component-will-mount.js +9 -0
  87. package/lib/cjs/webchat/hooks/use-component-will-mount.js.map +1 -0
  88. package/lib/cjs/webchat/hooks/use-previous.d.ts +1 -0
  89. package/lib/cjs/webchat/hooks/use-previous.js +13 -0
  90. package/lib/cjs/webchat/hooks/use-previous.js.map +1 -0
  91. package/lib/cjs/webchat/hooks/use-typing.d.ts +10 -0
  92. package/lib/cjs/webchat/hooks/use-typing.js +32 -0
  93. package/lib/cjs/webchat/hooks/use-typing.js.map +1 -0
  94. package/lib/cjs/webchat/hooks/use-webchat.d.ts +37 -0
  95. package/lib/cjs/webchat/hooks/use-webchat.js +167 -0
  96. package/lib/cjs/webchat/hooks/use-webchat.js.map +1 -0
  97. package/lib/cjs/webchat/index-types.d.ts +16 -10
  98. package/lib/cjs/webchat/message-list.js +2 -2
  99. package/lib/cjs/webchat/message-list.js.map +1 -1
  100. package/lib/cjs/webchat/messages-reducer.d.ts +6 -1
  101. package/lib/cjs/webchat/messages-reducer.js +8 -8
  102. package/lib/cjs/webchat/messages-reducer.js.map +1 -1
  103. package/lib/cjs/webchat/replies.js +1 -1
  104. package/lib/cjs/webchat/replies.js.map +1 -1
  105. package/lib/cjs/webchat/session-view.js +2 -2
  106. package/lib/cjs/webchat/session-view.js.map +1 -1
  107. package/lib/cjs/webchat/trigger-button/index.js +1 -3
  108. package/lib/cjs/webchat/trigger-button/index.js.map +1 -1
  109. package/lib/cjs/webchat/webchat-reducer.d.ts +6 -1
  110. package/lib/cjs/webchat/webchat-reducer.js +17 -17
  111. package/lib/cjs/webchat/webchat-reducer.js.map +1 -1
  112. package/lib/cjs/webchat/webchat.js +21 -21
  113. package/lib/cjs/webchat/webchat.js.map +1 -1
  114. package/lib/cjs/webchat/webview.js +4 -4
  115. package/lib/cjs/webchat/webview.js.map +1 -1
  116. package/lib/cjs/webview-app.js +1 -1
  117. package/lib/cjs/webview-app.js.map +1 -1
  118. package/lib/esm/botonic-tester.js +2 -2
  119. package/lib/esm/botonic-tester.js.map +1 -1
  120. package/lib/esm/components/audio.js +2 -2
  121. package/lib/esm/components/audio.js.map +1 -1
  122. package/lib/esm/components/button.js +2 -2
  123. package/lib/esm/components/button.js.map +1 -1
  124. package/lib/esm/components/carousel.js +3 -3
  125. package/lib/esm/components/carousel.js.map +1 -1
  126. package/lib/esm/components/custom-message.js +1 -1
  127. package/lib/esm/components/custom-message.js.map +1 -1
  128. package/lib/esm/components/document.js +2 -2
  129. package/lib/esm/components/document.js.map +1 -1
  130. package/lib/esm/components/handoff.js +2 -2
  131. package/lib/esm/components/handoff.js.map +1 -1
  132. package/lib/esm/components/image.js +1 -1
  133. package/lib/esm/components/image.js.map +1 -1
  134. package/lib/esm/components/index-types.d.ts +1 -1
  135. package/lib/esm/components/location.js +2 -2
  136. package/lib/esm/components/location.js.map +1 -1
  137. package/lib/esm/components/message-template.js +1 -1
  138. package/lib/esm/components/message-template.js.map +1 -1
  139. package/lib/esm/components/message.js +3 -3
  140. package/lib/esm/components/message.js.map +1 -1
  141. package/lib/esm/components/multichannel/multichannel-carousel.js +1 -1
  142. package/lib/esm/components/multichannel/multichannel-carousel.js.map +1 -1
  143. package/lib/esm/components/multichannel/multichannel-text.js +7 -18
  144. package/lib/esm/components/multichannel/multichannel-text.js.map +1 -1
  145. package/lib/esm/components/multichannel/multichannel-utils.d.ts +5 -5
  146. package/lib/esm/components/multichannel/multichannel-utils.js +1 -1
  147. package/lib/esm/components/multichannel/multichannel-utils.js.map +1 -1
  148. package/lib/esm/components/multichannel/multichannel.js +2 -2
  149. package/lib/esm/components/multichannel/multichannel.js.map +1 -1
  150. package/lib/esm/components/raw.js +2 -2
  151. package/lib/esm/components/raw.js.map +1 -1
  152. package/lib/esm/components/reply.js +4 -4
  153. package/lib/esm/components/reply.js.map +1 -1
  154. package/lib/esm/components/share-button.js +1 -1
  155. package/lib/esm/components/share-button.js.map +1 -1
  156. package/lib/esm/components/subtitle.js +1 -1
  157. package/lib/esm/components/subtitle.js.map +1 -1
  158. package/lib/esm/components/text.js +2 -2
  159. package/lib/esm/components/text.js.map +1 -1
  160. package/lib/esm/components/timestamps.js +1 -1
  161. package/lib/esm/components/timestamps.js.map +1 -1
  162. package/lib/esm/components/title.js +1 -1
  163. package/lib/esm/components/title.js.map +1 -1
  164. package/lib/esm/components/video.js +2 -2
  165. package/lib/esm/components/video.js.map +1 -1
  166. package/lib/esm/components/whatsapp-button-list.js +1 -1
  167. package/lib/esm/components/whatsapp-button-list.js.map +1 -1
  168. package/lib/esm/components/whatsapp-template.js +1 -1
  169. package/lib/esm/components/whatsapp-template.js.map +1 -1
  170. package/lib/esm/constants.d.ts +148 -148
  171. package/lib/esm/contexts.d.ts +4 -62
  172. package/lib/esm/contexts.js +45 -19
  173. package/lib/esm/contexts.js.map +1 -1
  174. package/lib/esm/index-types.d.ts +12 -12
  175. package/lib/esm/msg-to-botonic.js +2 -2
  176. package/lib/esm/msg-to-botonic.js.map +1 -1
  177. package/lib/esm/react-bot.js +1 -1
  178. package/lib/esm/react-bot.js.map +1 -1
  179. package/lib/esm/webchat/actions.d.ts +25 -23
  180. package/lib/esm/webchat/actions.js +26 -23
  181. package/lib/esm/webchat/actions.js.map +1 -1
  182. package/lib/esm/webchat/components/attachment.js +1 -1
  183. package/lib/esm/webchat/components/attachment.js.map +1 -1
  184. package/lib/esm/webchat/components/conditional-animation.js +1 -1
  185. package/lib/esm/webchat/components/conditional-animation.js.map +1 -1
  186. package/lib/esm/webchat/components/emoji-picker.js +2 -2
  187. package/lib/esm/webchat/components/emoji-picker.js.map +1 -1
  188. package/lib/esm/webchat/components/persistent-menu.js +4 -4
  189. package/lib/esm/webchat/components/persistent-menu.js.map +1 -1
  190. package/lib/esm/webchat/components/send-button.js +1 -1
  191. package/lib/esm/webchat/components/send-button.js.map +1 -1
  192. package/lib/esm/webchat/components/typing-indicator.js +1 -1
  193. package/lib/esm/webchat/components/typing-indicator.js.map +1 -1
  194. package/lib/esm/webchat/header.js +1 -1
  195. package/lib/esm/webchat/header.js.map +1 -1
  196. package/lib/esm/webchat/hooks/index.d.ts +5 -0
  197. package/lib/esm/webchat/hooks/index.js +6 -0
  198. package/lib/esm/webchat/hooks/index.js.map +1 -0
  199. package/lib/esm/webchat/hooks/use-component-visible.d.ts +8 -0
  200. package/lib/esm/webchat/hooks/use-component-visible.js +20 -0
  201. package/lib/esm/webchat/hooks/use-component-visible.js.map +1 -0
  202. package/lib/esm/webchat/hooks/use-component-will-mount.d.ts +1 -0
  203. package/lib/esm/webchat/hooks/use-component-will-mount.js +5 -0
  204. package/lib/esm/webchat/hooks/use-component-will-mount.js.map +1 -0
  205. package/lib/esm/webchat/hooks/use-previous.d.ts +1 -0
  206. package/lib/esm/webchat/hooks/use-previous.js +9 -0
  207. package/lib/esm/webchat/hooks/use-previous.js.map +1 -0
  208. package/lib/esm/webchat/hooks/use-typing.d.ts +10 -0
  209. package/lib/esm/webchat/hooks/use-typing.js +28 -0
  210. package/lib/esm/webchat/hooks/use-typing.js.map +1 -0
  211. package/lib/esm/webchat/hooks/use-webchat.d.ts +37 -0
  212. package/lib/esm/webchat/hooks/use-webchat.js +163 -0
  213. package/lib/esm/webchat/hooks/use-webchat.js.map +1 -0
  214. package/lib/esm/webchat/index-types.d.ts +16 -10
  215. package/lib/esm/webchat/message-list.js +2 -2
  216. package/lib/esm/webchat/message-list.js.map +1 -1
  217. package/lib/esm/webchat/messages-reducer.d.ts +6 -1
  218. package/lib/esm/webchat/messages-reducer.js +9 -9
  219. package/lib/esm/webchat/messages-reducer.js.map +1 -1
  220. package/lib/esm/webchat/replies.js +1 -1
  221. package/lib/esm/webchat/replies.js.map +1 -1
  222. package/lib/esm/webchat/session-view.js +2 -2
  223. package/lib/esm/webchat/session-view.js.map +1 -1
  224. package/lib/esm/webchat/trigger-button/index.js +1 -3
  225. package/lib/esm/webchat/trigger-button/index.js.map +1 -1
  226. package/lib/esm/webchat/webchat-reducer.d.ts +6 -1
  227. package/lib/esm/webchat/webchat-reducer.js +18 -18
  228. package/lib/esm/webchat/webchat-reducer.js.map +1 -1
  229. package/lib/esm/webchat/webchat.js +21 -21
  230. package/lib/esm/webchat/webchat.js.map +1 -1
  231. package/lib/esm/webchat/webview.js +4 -4
  232. package/lib/esm/webchat/webview.js.map +1 -1
  233. package/lib/esm/webview-app.js +1 -1
  234. package/lib/esm/webview-app.js.map +1 -1
  235. package/package.json +4 -4
  236. package/src/components/index-types.ts +1 -1
  237. package/src/components/multichannel/multichannel-text.jsx +11 -21
  238. package/src/components/multichannel/multichannel-utils.js +1 -1
  239. package/src/contexts.tsx +64 -0
  240. package/src/index-types.ts +12 -12
  241. package/src/webchat/actions.ts +25 -0
  242. package/src/webchat/hooks/index.ts +5 -0
  243. package/src/webchat/hooks/use-component-visible.ts +29 -0
  244. package/src/webchat/hooks/use-component-will-mount.ts +5 -0
  245. package/src/webchat/hooks/use-previous.ts +9 -0
  246. package/src/webchat/hooks/use-typing.ts +43 -0
  247. package/src/webchat/hooks/use-webchat.ts +213 -0
  248. package/src/webchat/index-types.ts +17 -8
  249. package/src/webchat/{messages-reducer.js → messages-reducer.ts} +25 -19
  250. package/src/webchat/trigger-button/index.tsx +0 -1
  251. package/src/webchat/{webchat-reducer.js → webchat-reducer.ts} +23 -37
  252. package/src/webchat/webchat.jsx +8 -9
  253. package/lib/cjs/webchat/hooks.d.ts +0 -81
  254. package/lib/cjs/webchat/hooks.js +0 -222
  255. package/lib/cjs/webchat/hooks.js.map +0 -1
  256. package/lib/esm/webchat/hooks.d.ts +0 -81
  257. package/lib/esm/webchat/hooks.js +0 -214
  258. package/lib/esm/webchat/hooks.js.map +0 -1
  259. package/src/contexts.jsx +0 -37
  260. package/src/webchat/actions.jsx +0 -23
  261. package/src/webchat/hooks.js +0 -270
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@botonic/react",
3
- "version": "0.23.1-alpha.0",
3
+ "version": "0.23.2-alpha.0",
4
4
  "license": "MIT",
5
5
  "description": "Build Chatbots using React",
6
6
  "main": "./lib/cjs",
@@ -35,7 +35,7 @@
35
35
  "README.md"
36
36
  ],
37
37
  "dependencies": {
38
- "@botonic/core": "^0.23.0",
38
+ "@botonic/core": "0.23.1-alpha.0",
39
39
  "axios": "^1.4.0",
40
40
  "emoji-picker-react": "^4.4.9",
41
41
  "framer-motion": "^3.1.1",
@@ -78,10 +78,10 @@
78
78
  "jest-environment-jsdom": "^29.5.0",
79
79
  "react-test-renderer": "^16.14.0",
80
80
  "ts-jest": "^29.1.0",
81
- "typescript": "^5.0.4"
81
+ "typescript": "5.0.4"
82
82
  },
83
83
  "engines": {
84
- "node": ">=10.0.0"
84
+ "node": ">=14.0.0"
85
85
  },
86
86
  "keywords": [
87
87
  "bot-framework",
@@ -128,7 +128,7 @@ export interface ThemeProps extends StyleProp {
128
128
  } & StyleProp & {
129
129
  timestamps?: {
130
130
  enable?: boolean
131
- format(): string
131
+ format: () => string
132
132
  } & StyleProp
133
133
  }
134
134
  button?: {
@@ -18,7 +18,7 @@ import {
18
18
  getMultichannelReplies,
19
19
  isFacebook,
20
20
  isWhatsapp,
21
- MENU_BUTOON_WHATSAPP_BUTTON_LIST,
21
+ MENU_BUTTON_WHATSAPP_BUTTON_LIST,
22
22
  MULTICHANNEL_WHATSAPP_PROPS,
23
23
  WHATSAPP_LIST_MAX_BUTTONS,
24
24
  WHATSAPP_MAX_BUTTONS,
@@ -81,14 +81,12 @@ export const MultichannelText = props => {
81
81
  const asText =
82
82
  type === buttonTypes.POSTBACK ? postbackButtonsAsText : true
83
83
  const newline =
84
- multichannelContext.messageSeparator === null &&
84
+ multichannelContext.messageSeparator == null &&
85
85
  !newLineFirstButton &&
86
86
  i === 0
87
87
  ? ''
88
88
  : '\n'
89
- if (type !== buttonTypes.POSTBACK && multichannelButton.props.payload) {
90
- delete multichannelButton.props.payload
91
- }
89
+
92
90
  return (
93
91
  <MultichannelButton
94
92
  key={`${type}${i}`}
@@ -115,7 +113,6 @@ export const MultichannelText = props => {
115
113
  return messages
116
114
  }
117
115
 
118
- // START WHATSAPP LOGIC
119
116
  if (isWhatsapp(requestContext)) {
120
117
  const texts = getText(props.children)
121
118
  const { postbackButtons, urlButtons, webviewButtons } = getWhatsappButtons()
@@ -132,13 +129,12 @@ export const MultichannelText = props => {
132
129
  const buttonsTextSeparator =
133
130
  props.buttonsTextSeparator || DEFAULT_WHATSAPP_MAX_BUTTON_SEPARATOR
134
131
 
135
- //MORE THAN 3 BUTTONS
136
- if (
137
- !postbackButtonsAsText &&
138
- postbackButtons.length > WHATSAPP_MAX_BUTTONS
139
- ) {
132
+ const exceedWhatsAppMaxButtonNumber =
133
+ !postbackButtonsAsText && postbackButtons.length > WHATSAPP_MAX_BUTTONS
134
+
135
+ if (exceedWhatsAppMaxButtonNumber) {
140
136
  const menuButtonTextWhatsappList =
141
- props.menuButtonTextWhatsappList || MENU_BUTOON_WHATSAPP_BUTTON_LIST
137
+ props.menuButtonTextWhatsappList || MENU_BUTTON_WHATSAPP_BUTTON_LIST
142
138
 
143
139
  const urlButtonElements = urlButtons.map(
144
140
  regenerateMultichannelButtons(!!texts.length)
@@ -153,7 +149,7 @@ export const MultichannelText = props => {
153
149
 
154
150
  const messages = messagesPostbackButtonList.map(
155
151
  (postbackButtons, index) => {
156
- if (postbackButtons.length < 4) {
152
+ if (postbackButtons.length <= WHATSAPP_MAX_BUTTONS) {
157
153
  return {
158
154
  type: INPUT.TEXT,
159
155
  children: [...buttonsTextSeparator, ...postbackButtons],
@@ -166,7 +162,6 @@ export const MultichannelText = props => {
166
162
  : postbackButton.props.payload,
167
163
  title: postbackButton.props.children,
168
164
  }
169
- console.log({ row })
170
165
  return row
171
166
  })
172
167
  const whatsbuttonlistProps = {
@@ -223,14 +218,11 @@ export const MultichannelText = props => {
223
218
  </Text>
224
219
  )
225
220
  })}
226
- {urlButtonElements.length ? messageWithUrlButtonElements : null}
227
- {webviewButtonElements.length
228
- ? messageWithWebviewButtonElements
229
- : null}
221
+ {urlButtonElements.length > 0 && messageWithUrlButtonElements}
222
+ {webviewButtonElements.length > 0 && messageWithWebviewButtonElements}
230
223
  </>
231
224
  )
232
225
  }
233
- // END LOGIC WHATSAPP WITH MORE THAN 3 BUTTONS
234
226
 
235
227
  multichannelContext.currentIndex = getDefaultIndex()
236
228
  const postbackButtonElements = postbackButtons.map(
@@ -264,9 +256,7 @@ export const MultichannelText = props => {
264
256
 
265
257
  return <>{messages}</>
266
258
  }
267
- // END WHATSAPP LOGIC
268
259
 
269
- // START FACEBOOK LOGIC
270
260
  if (isFacebook(requestContext)) {
271
261
  const text = getText(props.children)
272
262
  const multichannelFacebook = new MultichannelFacebook()
@@ -11,7 +11,7 @@ export const WHATSAPP_MAX_BUTTONS = 3
11
11
  export const WHATSAPP_LIST_MAX_BUTTONS = 10
12
12
  export const WHATSAPP_MAX_BUTTON_CHARS = 20
13
13
  export const DEFAULT_WHATSAPP_MAX_BUTTON_SEPARATOR = 'More options:'
14
- export const MENU_BUTOON_WHATSAPP_BUTTON_LIST = 'Menu'
14
+ export const MENU_BUTTON_WHATSAPP_BUTTON_LIST = 'Show options'
15
15
 
16
16
  export function isMultichannelButton(node) {
17
17
  return isNodeKind(node, 'MultichannelButton')
@@ -0,0 +1,64 @@
1
+ import { createContext } from 'react'
2
+
3
+ import { WebchatContextProps } from './index-types'
4
+ import { webchatInitialState } from './webchat/hooks'
5
+
6
+ export const RequestContext = createContext({
7
+ getString: () => '',
8
+ setLocale: () => '',
9
+ session: {},
10
+ params: {},
11
+ input: {},
12
+ defaultDelay: 0,
13
+ defaultTyping: 0,
14
+ })
15
+
16
+ export const WebchatContext = createContext<WebchatContextProps>({
17
+ addMessage: () => {
18
+ return
19
+ },
20
+ closeWebview: () => {
21
+ return
22
+ },
23
+ getThemeProperty: () => {
24
+ return
25
+ }, // used to retrieve a specific property of the theme defined by the developer in his 'webchat/index.js'
26
+ openWebview: () => {
27
+ return
28
+ },
29
+ resolveCase: () => {
30
+ return
31
+ },
32
+ sendAttachment: () => {
33
+ return
34
+ },
35
+ sendInput: () => {
36
+ return
37
+ },
38
+ sendPayload: () => {
39
+ return
40
+ },
41
+ sendText: () => {
42
+ return
43
+ },
44
+ theme: {},
45
+ toggleWebchat: () => {
46
+ return
47
+ },
48
+ updateLatestInput: () => {
49
+ return
50
+ },
51
+ updateMessage: () => {
52
+ return
53
+ },
54
+ updateReplies: () => {
55
+ return
56
+ },
57
+ updateUser: () => {
58
+ return
59
+ },
60
+ updateWebchatDevSettings: () => {
61
+ return
62
+ },
63
+ webchatState: webchatInitialState,
64
+ })
@@ -169,23 +169,23 @@ export interface Event {
169
169
  }
170
170
 
171
171
  export interface WebchatContextProps {
172
- sendText: (text: string, payload?: string) => void
173
- sendAttachment: (attachment: File) => void
174
- sendPayload: (payload: string) => void
175
- sendInput: (input: CoreInput) => void
176
- openWebview: (webviewComponent: Webview) => void
177
172
  addMessage: (message: WebchatMessage) => void
178
- updateMessage: (message: WebchatMessage) => void
179
- updateReplies: (replies: boolean) => void
180
- updateLatestInput: (input: CoreInput) => void
181
173
  closeWebview: () => void
182
- toggleWebchat: (toggle: boolean) => void
183
- getThemeProperty: (property: string, defaultValue?: string) => any
174
+ getThemeProperty: (property: string, defaultValue?: string | boolean) => any
175
+ openWebview: (webviewComponent: Webview) => void
184
176
  resolveCase: () => void
177
+ sendAttachment: (attachment: File) => void
178
+ sendInput: (input: CoreInput) => void
179
+ sendPayload: (payload: string) => void
180
+ sendText: (text: string, payload?: string) => void
185
181
  theme: ThemeProps
186
- webchatState: WebchatState
187
- updateWebchatDevSettings: (settings: WebchatSettingsProps) => void
182
+ toggleWebchat: (toggle: boolean) => void
183
+ updateLatestInput: (input: CoreInput) => void
184
+ updateMessage: (message: WebchatMessage) => void
185
+ updateReplies: (replies: boolean) => void
188
186
  updateUser: (user: Partial<CoreSessionUser>) => void
187
+ updateWebchatDevSettings: (settings: WebchatSettingsProps) => void
188
+ webchatState: WebchatState
189
189
  }
190
190
 
191
191
  // export class DevApp extends WebchatApp {
@@ -0,0 +1,25 @@
1
+ export enum WebchatAction {
2
+ ADD_MESSAGE = 'addMessage',
3
+ ADD_MESSAGE_COMPONENT = 'addMessageComponent',
4
+ UPDATE_MESSAGE = 'updateMessage',
5
+ UPDATE_REPLIES = 'updateReplies',
6
+ UPDATE_LATEST_INPUT = 'updateLatestInput',
7
+ UPDATE_TYPING = 'updateTyping',
8
+ UPDATE_WEBVIEW = 'updateWebview',
9
+ UPDATE_SESSION = 'updateSession',
10
+ UPDATE_LAST_ROUTE_PATH = 'updateLastRoutePath',
11
+ UPDATE_HANDOFF = 'updateHandoff',
12
+ UPDATE_THEME = 'updateTheme',
13
+ UPDATE_DEV_SETTINGS = 'updateDevSettings',
14
+ TOGGLE_WEBCHAT = 'toggleWebchat',
15
+ TOGGLE_EMOJI_PICKER = 'toggleEmojiPicker',
16
+ TOGGLE_PERSISTENT_MENU = 'togglePersistentMenu',
17
+ TOGGLE_COVER_COMPONENT = 'toggleCoverComponent',
18
+ DO_RENDER_CUSTOM_COMPONENT = 'doRenderCustomComponent',
19
+ SET_ERROR = 'setError',
20
+ CLEAR_MESSAGES = 'clearMessages',
21
+ UPDATE_LAST_MESSAGE_DATE = 'updateLastMessageDate',
22
+ SET_CURRENT_ATTACHMENT = 'setCurrentAttachment',
23
+ SET_ONLINE = 'setOnline',
24
+ UPDATE_JWT = 'updateJwt',
25
+ }
@@ -0,0 +1,5 @@
1
+ export { useComponentVisible } from './use-component-visible'
2
+ export { useComponentWillMount } from './use-component-will-mount'
3
+ export { usePrevious } from './use-previous'
4
+ export { useTyping } from './use-typing'
5
+ export { useWebchat, webchatInitialState } from './use-webchat'
@@ -0,0 +1,29 @@
1
+ import { useEffect, useRef, useState } from 'react'
2
+
3
+ interface ComponentVisible {
4
+ ref: React.RefObject<HTMLElement>
5
+ isComponentVisible: boolean
6
+ setIsComponentVisible: React.Dispatch<React.SetStateAction<boolean>>
7
+ }
8
+
9
+ export function useComponentVisible(
10
+ initialIsVisible: boolean,
11
+ onClickOutside: () => void
12
+ ): ComponentVisible {
13
+ const [isComponentVisible, setIsComponentVisible] = useState(initialIsVisible)
14
+ const ref = useRef<HTMLElement>(null)
15
+ const handleClickOutside = event => {
16
+ const target = event.path ? event.path[0] : event.target
17
+ if (ref.current && !ref.current.contains(target)) {
18
+ setIsComponentVisible(false)
19
+ onClickOutside()
20
+ }
21
+ }
22
+ useEffect(() => {
23
+ document.addEventListener('click', handleClickOutside, false)
24
+ return () => {
25
+ document.removeEventListener('click', handleClickOutside, false)
26
+ }
27
+ })
28
+ return { ref, isComponentVisible, setIsComponentVisible }
29
+ }
@@ -0,0 +1,5 @@
1
+ import { useMemo } from 'react'
2
+
3
+ export const useComponentWillMount = func => {
4
+ useMemo(func, [])
5
+ }
@@ -0,0 +1,9 @@
1
+ import { useEffect, useRef } from 'react'
2
+
3
+ export function usePrevious(value) {
4
+ const ref = useRef()
5
+ useEffect(() => {
6
+ ref.current = value
7
+ })
8
+ return ref.current
9
+ }
@@ -0,0 +1,43 @@
1
+ import { useEffect } from 'react'
2
+
3
+ import { WebchatMessage } from '../../index-types'
4
+ import { scrollToBottom } from '../../util'
5
+ import { WebchatState } from '../index-types'
6
+
7
+ interface UseTyping {
8
+ webchatState: WebchatState
9
+ updateTyping: (typing: boolean) => void
10
+ updateMessage: (message: WebchatMessage) => void
11
+ host: any
12
+ }
13
+
14
+ export function useTyping({
15
+ webchatState,
16
+ updateTyping,
17
+ updateMessage,
18
+ host,
19
+ }: UseTyping): void {
20
+ useEffect(() => {
21
+ let delayTimeout, typingTimeout
22
+ scrollToBottom({ host })
23
+ try {
24
+ const nextMsg = webchatState.messagesJSON.filter(m => !m.display)[0]
25
+ if (nextMsg.delay && nextMsg.typing) {
26
+ delayTimeout = setTimeout(
27
+ () => updateTyping(true),
28
+ nextMsg.delay * 1000
29
+ )
30
+ } else if (nextMsg.typing) updateTyping(true)
31
+ const totalDelay = nextMsg.delay + nextMsg.typing
32
+ if (totalDelay)
33
+ typingTimeout = setTimeout(() => {
34
+ updateMessage({ ...nextMsg, display: true })
35
+ updateTyping(false)
36
+ }, totalDelay * 1000)
37
+ } catch (e) {}
38
+ return () => {
39
+ clearTimeout(delayTimeout)
40
+ clearTimeout(typingTimeout)
41
+ }
42
+ }, [webchatState.messagesJSON, webchatState.typing])
43
+ }
@@ -0,0 +1,213 @@
1
+ import { Input, Session } from '@botonic/core'
2
+ import { useReducer } from 'react'
3
+
4
+ import { ThemeProps, Webview } from '../../components/index-types'
5
+ import { COLORS, WEBCHAT } from '../../constants'
6
+ import { WebchatMessage } from '../../index-types'
7
+ import { WebchatAction } from '../actions'
8
+ import { DevSettings, ErrorMessage, WebchatState } from '../index-types'
9
+ import { webchatReducer } from '../webchat-reducer'
10
+
11
+ export const webchatInitialState: WebchatState = {
12
+ width: WEBCHAT.DEFAULTS.WIDTH,
13
+ height: WEBCHAT.DEFAULTS.HEIGHT,
14
+ messagesJSON: [],
15
+ messagesComponents: [],
16
+ replies: [],
17
+ latestInput: {},
18
+ typing: false,
19
+ webview: null,
20
+ webviewParams: null,
21
+ session: { user: undefined },
22
+ lastRoutePath: null,
23
+ handoff: false,
24
+ theme: {
25
+ headerTitle: WEBCHAT.DEFAULTS.TITLE,
26
+ brandColor: COLORS.BOTONIC_BLUE,
27
+ brandImage: WEBCHAT.DEFAULTS.LOGO,
28
+ triggerButtonImage: undefined,
29
+ textPlaceholder: WEBCHAT.DEFAULTS.PLACEHOLDER,
30
+ style: {
31
+ fontFamily: WEBCHAT.DEFAULTS.FONT_FAMILY,
32
+ },
33
+ },
34
+ themeUpdates: {},
35
+ error: {},
36
+ online: true,
37
+ devSettings: { keepSessionOnReload: false },
38
+ isWebchatOpen: false,
39
+ isEmojiPickerOpen: false,
40
+ isPersistentMenuOpen: false,
41
+ isCoverComponentOpen: false,
42
+ isCustomComponentRendered: false,
43
+ lastMessageUpdate: undefined,
44
+ currentAttachment: undefined,
45
+ jwt: undefined,
46
+ unreadMessages: 0,
47
+ }
48
+
49
+ export function useWebchat() {
50
+ const [webchatState, webchatDispatch] = useReducer(
51
+ webchatReducer,
52
+ webchatInitialState
53
+ )
54
+
55
+ const addMessage = (message: WebchatMessage) =>
56
+ webchatDispatch({ type: WebchatAction.ADD_MESSAGE, payload: message })
57
+
58
+ const addMessageComponent = (message: WebchatMessage) =>
59
+ webchatDispatch({
60
+ type: WebchatAction.ADD_MESSAGE_COMPONENT,
61
+ payload: message,
62
+ })
63
+
64
+ const updateMessage = (message: WebchatMessage) =>
65
+ webchatDispatch({ type: WebchatAction.UPDATE_MESSAGE, payload: message })
66
+
67
+ const updateReplies = replies =>
68
+ webchatDispatch({ type: WebchatAction.UPDATE_REPLIES, payload: replies })
69
+
70
+ const updateLatestInput = (input: Input) =>
71
+ webchatDispatch({ type: WebchatAction.UPDATE_LATEST_INPUT, payload: input })
72
+
73
+ const updateTyping = (typing: boolean) =>
74
+ webchatDispatch({ type: WebchatAction.UPDATE_TYPING, payload: typing })
75
+
76
+ const updateWebview = (webview: Webview, params: Record<string, string>) =>
77
+ webchatDispatch({
78
+ type: WebchatAction.UPDATE_WEBVIEW,
79
+ payload: { webview, webviewParams: params },
80
+ })
81
+
82
+ const updateSession = (session: Session) => {
83
+ webchatDispatch({
84
+ type: WebchatAction.UPDATE_SESSION,
85
+ payload: session,
86
+ })
87
+ }
88
+
89
+ const updateLastRoutePath = (path: string) =>
90
+ webchatDispatch({
91
+ type: WebchatAction.UPDATE_LAST_ROUTE_PATH,
92
+ payload: path,
93
+ })
94
+
95
+ const updateHandoff = (handoff: boolean) =>
96
+ webchatDispatch({
97
+ type: WebchatAction.UPDATE_HANDOFF,
98
+ payload: handoff,
99
+ })
100
+
101
+ const updateTheme = (theme: ThemeProps, themeUpdates?: ThemeProps) => {
102
+ const payload =
103
+ themeUpdates !== undefined ? { theme, themeUpdates } : { theme }
104
+ webchatDispatch({
105
+ type: WebchatAction.UPDATE_THEME,
106
+ payload,
107
+ })
108
+ }
109
+
110
+ const updateDevSettings = (settings: DevSettings) =>
111
+ webchatDispatch({
112
+ type: WebchatAction.UPDATE_DEV_SETTINGS,
113
+ payload: settings,
114
+ })
115
+
116
+ const toggleWebchat = (toggle: boolean) => {
117
+ webchatDispatch({
118
+ type: WebchatAction.TOGGLE_WEBCHAT,
119
+ payload: toggle,
120
+ })
121
+ }
122
+
123
+ const toggleEmojiPicker = (toggle: boolean) =>
124
+ webchatDispatch({
125
+ type: WebchatAction.TOGGLE_EMOJI_PICKER,
126
+ payload: toggle,
127
+ })
128
+
129
+ const togglePersistentMenu = (toggle: boolean) =>
130
+ webchatDispatch({
131
+ type: WebchatAction.TOGGLE_PERSISTENT_MENU,
132
+ payload: toggle,
133
+ })
134
+
135
+ const toggleCoverComponent = (toggle: boolean) =>
136
+ webchatDispatch({
137
+ type: WebchatAction.TOGGLE_COVER_COMPONENT,
138
+ payload: toggle,
139
+ })
140
+
141
+ const doRenderCustomComponent = (toggle: boolean) =>
142
+ webchatDispatch({
143
+ type: WebchatAction.DO_RENDER_CUSTOM_COMPONENT,
144
+ payload: toggle,
145
+ })
146
+
147
+ const setError = (error: ErrorMessage) =>
148
+ webchatDispatch({
149
+ type: WebchatAction.SET_ERROR,
150
+ payload: error,
151
+ })
152
+
153
+ const setOnline = (online: boolean) =>
154
+ webchatDispatch({
155
+ type: WebchatAction.SET_ONLINE,
156
+ payload: online,
157
+ })
158
+
159
+ const clearMessages = () => {
160
+ webchatDispatch({
161
+ type: WebchatAction.CLEAR_MESSAGES,
162
+ })
163
+ }
164
+
165
+ const updateLastMessageDate = (date: string) => {
166
+ webchatDispatch({
167
+ type: WebchatAction.UPDATE_LAST_MESSAGE_DATE,
168
+ payload: date,
169
+ })
170
+ }
171
+
172
+ const setCurrentAttachment = (attachment: File) => {
173
+ webchatDispatch({
174
+ type: WebchatAction.SET_CURRENT_ATTACHMENT,
175
+ payload: attachment,
176
+ })
177
+ }
178
+
179
+ const updateJwt = (jwt: string) => {
180
+ webchatDispatch({
181
+ type: WebchatAction.UPDATE_JWT,
182
+ payload: jwt,
183
+ })
184
+ }
185
+
186
+ return {
187
+ addMessage,
188
+ addMessageComponent,
189
+ clearMessages,
190
+ doRenderCustomComponent,
191
+ setCurrentAttachment,
192
+ setError,
193
+ setOnline,
194
+ toggleCoverComponent,
195
+ toggleEmojiPicker,
196
+ togglePersistentMenu,
197
+ toggleWebchat,
198
+ updateDevSettings,
199
+ updateHandoff,
200
+ updateJwt,
201
+ updateLastMessageDate,
202
+ updateLastRoutePath,
203
+ updateLatestInput,
204
+ updateMessage,
205
+ updateReplies,
206
+ updateSession,
207
+ updateTheme,
208
+ updateTyping,
209
+ updateWebview,
210
+ webchatDispatch,
211
+ webchatState,
212
+ }
213
+ }
@@ -1,7 +1,4 @@
1
- import type {
2
- Input as CoreInput,
3
- Session as CoreSession,
4
- } from '@botonic/core/lib/esm/models/legacy-types'
1
+ import type { Input as CoreInput, Session as CoreSession } from '@botonic/core'
5
2
  import { RefObject } from 'react'
6
3
 
7
4
  import { Webview } from '../components/index-types'
@@ -17,6 +14,14 @@ export interface WebchatStateTheme {
17
14
  }
18
15
  }
19
16
 
17
+ export interface ErrorMessage {
18
+ message?: string
19
+ }
20
+ export interface DevSettings {
21
+ keepSessionOnReload?: boolean
22
+ showSessionView?: boolean
23
+ }
24
+
20
25
  export interface WebchatState {
21
26
  width: number
22
27
  height: number
@@ -32,14 +37,18 @@ export interface WebchatState {
32
37
  handoff: boolean
33
38
  theme: WebchatStateTheme
34
39
  themeUpdates: Partial<WebchatStateTheme>
35
- error: { message?: string }
36
- devSettings: { keepSessionOnReload?: boolean; showSessionView?: boolean }
40
+ error: ErrorMessage
41
+ online: boolean
42
+ devSettings: DevSettings
37
43
  isWebchatOpen: boolean
38
44
  isEmojiPickerOpen: boolean
39
45
  isPersistentMenuOpen: boolean
40
46
  isCoverComponentOpen: boolean
41
- lastMessageUpdate: string
42
- currentAttachment: File | undefined
47
+ isCustomComponentRendered: boolean
48
+ lastMessageUpdate?: string
49
+ currentAttachment?: File
50
+ jwt?: string
51
+ unreadMessages: number
43
52
  }
44
53
 
45
54
  // export interface WebchatProps extends WebchatArgs {