@botonic/react 0.31.0-alpha.2 → 0.31.0-alpha.4

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 (161) hide show
  1. package/lib/cjs/components/element.js +6 -6
  2. package/lib/cjs/components/element.js.map +1 -1
  3. package/lib/cjs/components/index-types.d.ts +2 -0
  4. package/lib/cjs/components/multichannel/multichannel-button.js +2 -2
  5. package/lib/cjs/components/multichannel/multichannel-button.js.map +1 -1
  6. package/lib/cjs/components/multichannel/multichannel-carousel.js +4 -4
  7. package/lib/cjs/components/multichannel/multichannel-carousel.js.map +1 -1
  8. package/lib/cjs/components/multichannel/multichannel-text.js +12 -11
  9. package/lib/cjs/components/multichannel/multichannel-text.js.map +1 -1
  10. package/lib/cjs/components/multichannel/multichannel-utils.d.ts +9 -15
  11. package/lib/cjs/components/multichannel/multichannel-utils.js +48 -27
  12. package/lib/cjs/components/multichannel/multichannel-utils.js.map +1 -1
  13. package/lib/cjs/components/multichannel/multichannel.js +7 -7
  14. package/lib/cjs/components/multichannel/multichannel.js.map +1 -1
  15. package/lib/cjs/components/multichannel/whatsapp/constants.d.ts +11 -0
  16. package/lib/cjs/components/multichannel/whatsapp/constants.js +13 -0
  17. package/lib/cjs/components/multichannel/whatsapp/constants.js.map +1 -0
  18. package/lib/cjs/components/whatsapp-button-list.js +2 -2
  19. package/lib/cjs/components/whatsapp-button-list.js.map +1 -1
  20. package/lib/cjs/components/whatsapp-cta-url-button.js +5 -5
  21. package/lib/cjs/components/whatsapp-cta-url-button.js.map +1 -1
  22. package/lib/cjs/constants.d.ts +0 -6
  23. package/lib/cjs/constants.js +1 -7
  24. package/lib/cjs/constants.js.map +1 -1
  25. package/lib/cjs/contexts.js +6 -0
  26. package/lib/cjs/contexts.js.map +1 -1
  27. package/lib/cjs/dev-app.d.ts +7 -0
  28. package/lib/cjs/index-types.d.ts +4 -7
  29. package/lib/cjs/index-types.js.map +1 -1
  30. package/lib/cjs/index.d.ts +1 -1
  31. package/lib/cjs/index.js.map +1 -1
  32. package/lib/cjs/webchat/actions.d.ts +2 -1
  33. package/lib/cjs/webchat/actions.js +1 -0
  34. package/lib/cjs/webchat/actions.js.map +1 -1
  35. package/lib/cjs/webchat/cover-component/index.d.ts +6 -0
  36. package/lib/cjs/webchat/cover-component/index.js +21 -0
  37. package/lib/cjs/webchat/cover-component/index.js.map +1 -0
  38. package/lib/cjs/webchat/hooks/use-scroll-to-bottom.d.ts +6 -4
  39. package/lib/cjs/webchat/hooks/use-scroll-to-bottom.js.map +1 -1
  40. package/lib/cjs/webchat/hooks/use-webchat.d.ts +1 -0
  41. package/lib/cjs/webchat/hooks/use-webchat.js +8 -0
  42. package/lib/cjs/webchat/hooks/use-webchat.js.map +1 -1
  43. package/lib/cjs/webchat/index-types.d.ts +1 -0
  44. package/lib/cjs/webchat/input-panel/textarea.js +8 -2
  45. package/lib/cjs/webchat/input-panel/textarea.js.map +1 -1
  46. package/lib/cjs/webchat/message-list/index.js +63 -35
  47. package/lib/cjs/webchat/message-list/index.js.map +1 -1
  48. package/lib/cjs/webchat/message-list/styles.js +3 -3
  49. package/lib/cjs/webchat/typing-indicator/index.d.ts +3 -1
  50. package/lib/cjs/webchat/typing-indicator/index.js +4 -3
  51. package/lib/cjs/webchat/typing-indicator/index.js.map +1 -1
  52. package/lib/cjs/webchat/typing-indicator/styles.d.ts +3 -2
  53. package/lib/cjs/webchat/typing-indicator/styles.js +6 -3
  54. package/lib/cjs/webchat/typing-indicator/styles.js.map +1 -1
  55. package/lib/cjs/webchat/webchat-reducer.js +2 -0
  56. package/lib/cjs/webchat/webchat-reducer.js.map +1 -1
  57. package/lib/cjs/webchat/webchat-typed/styles.d.ts +7 -0
  58. package/lib/cjs/webchat/webchat-typed/styles.js +54 -0
  59. package/lib/cjs/webchat/webchat-typed/styles.js.map +1 -0
  60. package/lib/cjs/webchat/webchat-typed/webchat-typed.d.ts +0 -0
  61. package/lib/cjs/webchat/webchat-typed/webchat-typed.js +663 -0
  62. package/lib/cjs/webchat/webchat-typed/webchat-typed.js.map +1 -0
  63. package/lib/cjs/webchat/webchat.js +16 -18
  64. package/lib/cjs/webchat/webchat.js.map +1 -1
  65. package/lib/cjs/webchat-app.d.ts +117 -76
  66. package/lib/cjs/webchat-app.js +98 -55
  67. package/lib/cjs/webchat-app.js.map +1 -1
  68. package/lib/esm/components/element.js +6 -6
  69. package/lib/esm/components/element.js.map +1 -1
  70. package/lib/esm/components/index-types.d.ts +2 -0
  71. package/lib/esm/components/multichannel/multichannel-button.js +1 -1
  72. package/lib/esm/components/multichannel/multichannel-carousel.js +5 -5
  73. package/lib/esm/components/multichannel/multichannel-carousel.js.map +1 -1
  74. package/lib/esm/components/multichannel/multichannel-text.js +2 -1
  75. package/lib/esm/components/multichannel/multichannel-text.js.map +1 -1
  76. package/lib/esm/components/multichannel/multichannel-utils.d.ts +9 -15
  77. package/lib/esm/components/multichannel/multichannel-utils.js +40 -24
  78. package/lib/esm/components/multichannel/multichannel-utils.js.map +1 -1
  79. package/lib/esm/components/multichannel/multichannel.js +7 -7
  80. package/lib/esm/components/multichannel/multichannel.js.map +1 -1
  81. package/lib/esm/components/multichannel/whatsapp/constants.d.ts +11 -0
  82. package/lib/esm/components/multichannel/whatsapp/constants.js +10 -0
  83. package/lib/esm/components/multichannel/whatsapp/constants.js.map +1 -0
  84. package/lib/esm/components/whatsapp-button-list.js +1 -1
  85. package/lib/esm/components/whatsapp-cta-url-button.js +1 -1
  86. package/lib/esm/constants.d.ts +0 -6
  87. package/lib/esm/constants.js +0 -6
  88. package/lib/esm/constants.js.map +1 -1
  89. package/lib/esm/contexts.js +6 -0
  90. package/lib/esm/contexts.js.map +1 -1
  91. package/lib/esm/dev-app.d.ts +7 -0
  92. package/lib/esm/index-types.d.ts +4 -7
  93. package/lib/esm/index-types.js.map +1 -1
  94. package/lib/esm/index.d.ts +1 -1
  95. package/lib/esm/index.js.map +1 -1
  96. package/lib/esm/webchat/actions.d.ts +2 -1
  97. package/lib/esm/webchat/actions.js +1 -0
  98. package/lib/esm/webchat/actions.js.map +1 -1
  99. package/lib/esm/webchat/cover-component/index.d.ts +6 -0
  100. package/lib/esm/webchat/cover-component/index.js +17 -0
  101. package/lib/esm/webchat/cover-component/index.js.map +1 -0
  102. package/lib/esm/webchat/hooks/use-scroll-to-bottom.d.ts +6 -4
  103. package/lib/esm/webchat/hooks/use-scroll-to-bottom.js.map +1 -1
  104. package/lib/esm/webchat/hooks/use-webchat.d.ts +1 -0
  105. package/lib/esm/webchat/hooks/use-webchat.js +8 -0
  106. package/lib/esm/webchat/hooks/use-webchat.js.map +1 -1
  107. package/lib/esm/webchat/index-types.d.ts +1 -0
  108. package/lib/esm/webchat/input-panel/textarea.js +8 -2
  109. package/lib/esm/webchat/input-panel/textarea.js.map +1 -1
  110. package/lib/esm/webchat/message-list/index.js +62 -35
  111. package/lib/esm/webchat/message-list/index.js.map +1 -1
  112. package/lib/esm/webchat/message-list/styles.js +3 -3
  113. package/lib/esm/webchat/typing-indicator/index.d.ts +3 -1
  114. package/lib/esm/webchat/typing-indicator/index.js +5 -2
  115. package/lib/esm/webchat/typing-indicator/index.js.map +1 -1
  116. package/lib/esm/webchat/typing-indicator/styles.d.ts +3 -2
  117. package/lib/esm/webchat/typing-indicator/styles.js +5 -2
  118. package/lib/esm/webchat/typing-indicator/styles.js.map +1 -1
  119. package/lib/esm/webchat/webchat-reducer.js +2 -0
  120. package/lib/esm/webchat/webchat-reducer.js.map +1 -1
  121. package/lib/esm/webchat/webchat-typed/styles.d.ts +7 -0
  122. package/lib/esm/webchat/webchat-typed/styles.js +50 -0
  123. package/lib/esm/webchat/webchat-typed/styles.js.map +1 -0
  124. package/lib/esm/webchat/webchat-typed/webchat-typed.d.ts +0 -0
  125. package/lib/esm/webchat/webchat-typed/webchat-typed.js +663 -0
  126. package/lib/esm/webchat/webchat-typed/webchat-typed.js.map +1 -0
  127. package/lib/esm/webchat/webchat.js +16 -18
  128. package/lib/esm/webchat/webchat.js.map +1 -1
  129. package/lib/esm/webchat-app.d.ts +117 -76
  130. package/lib/esm/webchat-app.js +99 -56
  131. package/lib/esm/webchat-app.js.map +1 -1
  132. package/package.json +5 -4
  133. package/src/components/element.jsx +4 -11
  134. package/src/components/index-types.ts +4 -0
  135. package/src/components/multichannel/multichannel-button.jsx +1 -1
  136. package/src/components/multichannel/multichannel-carousel.jsx +7 -5
  137. package/src/components/multichannel/multichannel-text.jsx +4 -2
  138. package/src/components/multichannel/multichannel-utils.js +45 -27
  139. package/src/components/multichannel/multichannel.jsx +12 -7
  140. package/src/components/multichannel/whatsapp/constants.ts +10 -0
  141. package/src/components/whatsapp-button-list.tsx +1 -1
  142. package/src/components/whatsapp-cta-url-button.tsx +1 -1
  143. package/src/constants.js +0 -7
  144. package/src/contexts.tsx +6 -0
  145. package/src/index-types.ts +4 -7
  146. package/src/index.ts +1 -1
  147. package/src/webchat/actions.ts +1 -0
  148. package/src/webchat/cover-component/index.tsx +31 -0
  149. package/src/webchat/hooks/use-scroll-to-bottom.ts +8 -2
  150. package/src/webchat/hooks/use-webchat.ts +9 -0
  151. package/src/webchat/index-types.ts +1 -0
  152. package/src/webchat/input-panel/textarea.tsx +12 -1
  153. package/src/webchat/message-list/index.tsx +79 -48
  154. package/src/webchat/message-list/styles.ts +3 -3
  155. package/src/webchat/typing-indicator/index.tsx +20 -12
  156. package/src/webchat/typing-indicator/styles.ts +7 -3
  157. package/src/webchat/webchat-reducer.ts +2 -0
  158. package/src/webchat/webchat-typed/styles.ts +54 -0
  159. package/src/webchat/webchat-typed/webchat-typed.tsx +728 -0
  160. package/src/webchat/webchat.jsx +48 -48
  161. package/src/{webchat-app.jsx → webchat-app.tsx} +246 -77
@@ -1,15 +1,128 @@
1
- import { HubtypeService, INPUT } from '@botonic/core'
1
+ import { HubtypeService, INPUT, Input, ServerConfig } from '@botonic/core'
2
2
  import merge from 'lodash.merge'
3
3
  import React, { createRef } from 'react'
4
- import { createRoot } from 'react-dom/client'
5
-
4
+ import { createRoot, Root } from 'react-dom/client'
5
+
6
+ import {
7
+ BlockInputOption,
8
+ CoverComponentOptions,
9
+ PersistentMenuTheme,
10
+ ThemeProps,
11
+ WebchatSettingsProps,
12
+ } from './components/index-types'
6
13
  import { WEBCHAT } from './constants'
7
- import { SENDERS, Typing } from './index-types'
14
+ import { CloseWebviewOptions } from './contexts'
15
+ import {
16
+ ActionRequest,
17
+ EventArgs,
18
+ SENDERS,
19
+ Typing,
20
+ WebchatMessage,
21
+ } from './index-types'
8
22
  import { msgToBotonic } from './msg-to-botonic'
9
23
  import { isShadowDOMSupported, onDOMLoaded } from './util/dom'
24
+ import { ErrorMessage } from './webchat/index-types'
10
25
  import { Webchat } from './webchat/webchat'
11
26
 
27
+ export interface WebchatAppProps {
28
+ theme?: ThemeProps
29
+ persistentMenu?: PersistentMenuTheme
30
+ coverComponent?: CoverComponentOptions
31
+ blockInputs?: BlockInputOption[]
32
+ enableEmojiPicker?: boolean
33
+ enableAttachments?: boolean
34
+ enableUserInput?: boolean
35
+ enableAnimations?: boolean
36
+ hostId?: string
37
+ shadowDOM?: boolean | (() => boolean)
38
+ defaultDelay?: number
39
+ defaultTyping?: number
40
+ storage?: Storage | null
41
+ storageKey?: string
42
+ onInit?: (app: WebchatApp, args: any) => void
43
+ onOpen?: (app: WebchatApp, args: any) => void
44
+ onClose?: (app: WebchatApp, args: any) => void
45
+ onMessage?: (app: WebchatApp, message: WebchatMessage) => void
46
+ onTrackEvent?: (
47
+ request: ActionRequest,
48
+ eventName: string,
49
+ args?: EventArgs
50
+ ) => Promise<void>
51
+ onConnectionChange?: (app: WebchatApp, isOnline: boolean) => void
52
+ appId?: string
53
+ visibility?: boolean | (() => boolean) | 'dynamic'
54
+ server?: ServerConfig
55
+ }
56
+
57
+ interface WebchatRef {
58
+ addBotResponse: ({
59
+ response,
60
+ session,
61
+ lastRoutePath,
62
+ }: AddBotResponseArgs) => void
63
+ setTyping: (typing: boolean) => void
64
+ addUserMessage: (message: any) => Promise<void>
65
+ updateUser: (userToUpdate: any) => void
66
+ openWebchat: () => void
67
+ closeWebchat: () => void
68
+ toggleWebchat: () => void
69
+ openCoverComponent: () => void
70
+ closeCoverComponent: () => void
71
+ renderCustomComponent: (customComponent: any) => void
72
+ unmountCustomComponent: () => void
73
+ toggleCoverComponent: () => void
74
+ openWebviewApi: (component: any) => void
75
+ setError: (error: ErrorMessage) => void
76
+ setOnline: (online: boolean) => void
77
+ getMessages: () => { id: string; ack: number; unsentInput: Input }[] // TODO: define MessagesJSON
78
+ isOnline: () => boolean
79
+ clearMessages: () => void
80
+ getLastMessageUpdate: () => string
81
+ updateMessageInfo: (msgId: string, messageInfo: any) => void
82
+ updateWebchatSettings: (settings: WebchatSettingsProps) => void
83
+ closeWebview: (options?: CloseWebviewOptions) => Promise<void>
84
+ }
85
+
86
+ interface AddBotResponseArgs {
87
+ response: any
88
+ session?: any
89
+ lastRoutePath?: any
90
+ }
91
+
12
92
  export class WebchatApp {
93
+ public theme?: ThemeProps
94
+ public persistentMenu?: PersistentMenuTheme
95
+ public coverComponent?: CoverComponentOptions
96
+ public blockInputs?: BlockInputOption[]
97
+ public enableEmojiPicker?: boolean
98
+ public enableAttachments?: boolean
99
+ public enableUserInput?: boolean
100
+ public enableAnimations?: boolean
101
+ public hostId?: string
102
+ public shadowDOM?: boolean | (() => boolean)
103
+ public defaultDelay?: number
104
+ public defaultTyping?: number
105
+ public storage?: Storage | null
106
+ public storageKey: string
107
+ public onInit?: (app: WebchatApp, args: any) => void
108
+ public onOpen?: (app: WebchatApp, args: any) => void
109
+ public onClose?: (app: WebchatApp, args: any) => void
110
+ public onMessage?: (app: WebchatApp, message: WebchatMessage) => void
111
+ public onTrackEvent?: (
112
+ request: ActionRequest,
113
+ eventName: string,
114
+ args?: EventArgs
115
+ ) => Promise<void>
116
+ public onConnectionChange?: (app: WebchatApp, isOnline: boolean) => void
117
+ public appId?: string
118
+ public visibility?: boolean | (() => boolean) | 'dynamic'
119
+ public server?: ServerConfig
120
+ public webchatRef: React.RefObject<WebchatRef | null>
121
+
122
+ private reactRoot: Root | null = null
123
+ private host: (HTMLElement | null) | ShadowRoot = null
124
+ private hubtypeService: HubtypeService
125
+
13
126
  constructor({
14
127
  theme = {},
15
128
  persistentMenu,
@@ -19,8 +132,8 @@ export class WebchatApp {
19
132
  enableAttachments,
20
133
  enableUserInput,
21
134
  enableAnimations,
22
- hostId,
23
- shadowDOM,
135
+ hostId = 'root',
136
+ shadowDOM = false,
24
137
  defaultDelay,
25
138
  defaultTyping,
26
139
  storage,
@@ -34,7 +147,7 @@ export class WebchatApp {
34
147
  appId,
35
148
  visibility,
36
149
  server,
37
- }) {
150
+ }: WebchatAppProps) {
38
151
  this.theme = theme
39
152
  this.persistentMenu = persistentMenu
40
153
  this.coverComponent = coverComponent
@@ -43,13 +156,15 @@ export class WebchatApp {
43
156
  this.enableAttachments = enableAttachments
44
157
  this.enableUserInput = enableUserInput
45
158
  this.enableAnimations = enableAnimations
159
+
46
160
  this.shadowDOM = Boolean(
47
161
  typeof shadowDOM === 'function' ? shadowDOM() : shadowDOM
48
162
  )
49
163
  if (this.shadowDOM && !isShadowDOMSupported()) {
50
164
  console.warn('[botonic] ShadowDOM not supported on this browser')
51
165
  this.shadowDOM = false
52
- }
166
+ } // Review this
167
+
53
168
  this.hostId = hostId || WEBCHAT.DEFAULTS.HOST_ID
54
169
  this.defaultDelay = defaultDelay
55
170
  this.defaultTyping = defaultTyping
@@ -63,12 +178,14 @@ export class WebchatApp {
63
178
  this.onConnectionChange = onConnectionChange
64
179
  this.visibility = visibility
65
180
  this.server = server
66
- this.webchatRef = createRef()
181
+ this.webchatRef = createRef<WebchatRef>()
67
182
  this.appId = appId
183
+
184
+ this.host = null
68
185
  this.reactRoot = null
69
186
  }
70
187
 
71
- createRootElement(host) {
188
+ createRootElement(host: HTMLElement | null) {
72
189
  // Create root element <div id='root'> if not exists
73
190
  // Create shadowDOM to root element if needed
74
191
  if (host) {
@@ -79,45 +196,63 @@ export class WebchatApp {
79
196
  )
80
197
  this.hostId = host.id
81
198
  }
82
- } else if (host.id) this.hostId = host.id
83
- else if (this.hostId) host.id = this.hostId
199
+ } else if (host.id) {
200
+ this.hostId = host.id
201
+ } else if (this.hostId) {
202
+ host.id = this.hostId
203
+ }
84
204
  } else {
85
- host = document.getElementById(this.hostId)
205
+ host = this.hostId ? document.getElementById(this.hostId) : null
86
206
  }
207
+
87
208
  if (!host) {
88
209
  host = document.createElement('div')
89
- host.id = this.hostId
90
- if (document.body.firstChild)
210
+ host.id = this.hostId!
211
+ if (document.body.firstChild) {
91
212
  document.body.insertBefore(host, document.body.firstChild)
92
- else document.body.appendChild(host)
213
+ } else {
214
+ document.body.appendChild(host)
215
+ }
93
216
  }
94
217
  this.host = this.shadowDOM ? host.attachShadow({ mode: 'open' }) : host
95
218
  }
96
219
 
97
- getReactMountNode(node) {
220
+ getReactMountNode(
221
+ node?: (HTMLElement | null) | ShadowRoot
222
+ ): Element | DocumentFragment {
98
223
  if (!node) {
99
224
  node = this.host
100
225
  }
101
- return node.shadowRoot ? node.shadowRoot : node
226
+
227
+ if (node === null) {
228
+ throw new Error('Host element not found')
229
+ }
230
+
231
+ // TODO: Review logic of ShadowRoot
232
+ if ('shadowRoot' in node && node.shadowRoot !== null) {
233
+ return node.shadowRoot
234
+ }
235
+
236
+ return node
102
237
  }
103
238
 
104
- onInitWebchat(...args) {
239
+ onInitWebchat(...args: [any]) {
105
240
  this.onInit && this.onInit(this, ...args)
106
241
  }
107
242
 
108
- onOpenWebchat(...args) {
243
+ onOpenWebchat(...args: [any]) {
109
244
  this.onOpen && this.onOpen(this, ...args)
110
245
  }
111
246
 
112
- onCloseWebchat(...args) {
247
+ onCloseWebchat(...args: [any]) {
113
248
  this.onClose && this.onClose(this, ...args)
114
249
  }
115
250
 
116
251
  async onUserInput({ user, input }) {
117
252
  this.onMessage &&
118
253
  this.onMessage(this, {
254
+ ...input,
119
255
  sentBy: SENDERS.user,
120
- message: input,
121
256
  isUnread: false,
122
257
  })
123
258
  return this.hubtypeService.postMessage(user, input)
@@ -131,29 +266,32 @@ export class WebchatApp {
131
266
  const lastMessage = messagesJSON[messagesJSON.length - 1]
132
267
  const lastMessageId = lastMessage && lastMessage.id
133
268
  const lastMessageUpdateDate = this.getLastMessageUpdate()
269
+
134
270
  if (this.hubtypeService) {
135
271
  this.hubtypeService.lastMessageId = lastMessageId
136
272
  this.hubtypeService.lastMessageUpdateDate = lastMessageUpdateDate
137
- } else if (!this.hubtypeService && user) {
273
+ }
274
+
275
+ if (!this.hubtypeService && user) {
138
276
  this.hubtypeService = new HubtypeService({
139
- appId: this.appId,
277
+ appId: this.appId!,
140
278
  user,
141
279
  lastMessageId,
142
280
  lastMessageUpdateDate,
143
- onEvent: event => this.onServiceEvent(event),
281
+ onEvent: (event: any) => this.onServiceEvent(event),
144
282
  unsentInputs: () =>
145
283
  this.webchatRef.current
146
- .getMessages()
147
- .filter(msg => msg.ack === 0 && msg.unsentInput),
284
+ ?.getMessages()
285
+ .filter(msg => msg.ack === 0 && msg.unsentInput) || [],
148
286
  server: this.server,
149
287
  })
150
288
  }
151
289
  }
152
290
 
153
- onServiceEvent(event) {
291
+ onServiceEvent(event: any) {
154
292
  if (event.action === 'connectionChange') {
155
293
  this.onConnectionChange && this.onConnectionChange(this, event.online)
156
- this.webchatRef.current.setOnline(event.online)
294
+ this.webchatRef.current?.setOnline(event.online)
157
295
  } else if (event.action === 'update_message_info') {
158
296
  this.updateMessageInfo(event.message.id, event.message)
159
297
  } else if (event.message?.type === 'update_webchat_settings') {
@@ -162,115 +300,122 @@ export class WebchatApp {
162
300
  this.setTyping(event.message.data === Typing.On)
163
301
  } else {
164
302
  this.onMessage &&
165
- this.onMessage(this, { sentBy: SENDERS.bot, message: event.message })
303
+ this.onMessage(this, {
304
+ sentBy: SENDERS.bot,
305
+ ...event.message,
306
+ } as WebchatMessage)
166
307
  this.addBotMessage(event.message)
167
308
  }
168
309
  }
169
310
 
170
- updateUser(user) {
171
- this.webchatRef.current.updateUser(user)
311
+ updateUser(user: any) {
312
+ this.webchatRef.current?.updateUser(user)
172
313
  }
173
314
 
174
- addBotMessage(message) {
315
+ addBotMessage(message: any) {
175
316
  message.ack = 0
176
317
  message.isUnread = true
177
318
  message.sentBy = message.sent_by?.split('message_sent_by_')[1]
178
319
  delete message.sent_by
179
320
  const response = msgToBotonic(
180
321
  message,
181
- this.theme.message?.customTypes || this.theme.customMessageTypes
322
+ // TODO: Review if is neded allow declar customTypes inside and ouside theme
323
+ // @ts-ignore
324
+ this.theme?.message?.customTypes || this.theme?.customMessageTypes || []
182
325
  )
183
326
 
184
- this.webchatRef.current.addBotResponse({
327
+ this.webchatRef.current?.addBotResponse({
185
328
  response,
186
329
  })
187
330
  }
188
331
 
189
- addBotText(text) {
332
+ addBotText(text: string) {
190
333
  this.addBotMessage({ type: INPUT.TEXT, data: text })
191
334
  }
192
335
 
193
- addUserMessage(message) {
194
- this.webchatRef.current.addUserMessage(message)
336
+ addUserMessage(message: any) {
337
+ this.webchatRef.current?.addUserMessage(message)
195
338
  }
196
339
 
197
- addUserText(text) {
340
+ addUserText(text: string) {
198
341
  this.addUserMessage({ type: INPUT.TEXT, data: text })
199
342
  }
200
343
 
201
- addUserPayload(payload) {
344
+ addUserPayload(payload: string) {
202
345
  this.addUserMessage({ type: INPUT.POSTBACK, payload })
203
346
  }
204
347
 
205
- setTyping(typing) {
206
- this.webchatRef.current.setTyping(typing)
348
+ setTyping(typing: boolean) {
349
+ this.webchatRef.current?.setTyping(typing)
207
350
  }
208
351
 
209
352
  open() {
210
- this.webchatRef.current.openWebchat()
353
+ this.webchatRef.current?.openWebchat()
211
354
  }
212
355
 
213
356
  close() {
214
- this.webchatRef.current.closeWebchat()
357
+ this.webchatRef.current?.closeWebchat()
215
358
  }
216
359
 
217
- async closeWebview() {
218
- await this.webchatRef.current.closeWebview()
360
+ async closeWebview(options?: CloseWebviewOptions) {
361
+ await this.webchatRef.current?.closeWebview(options)
219
362
  }
220
363
 
364
+ // TODO: Remove this function because we have open and close functions
221
365
  toggle() {
222
- this.webchatRef.current.toggleWebchat()
366
+ this.webchatRef.current?.toggleWebchat()
223
367
  }
224
368
 
225
369
  openCoverComponent() {
226
- this.webchatRef.current.openCoverComponent()
370
+ this.webchatRef.current?.openCoverComponent()
227
371
  }
228
372
 
229
373
  closeCoverComponent() {
230
- this.webchatRef.current.closeCoverComponent()
374
+ this.webchatRef.current?.closeCoverComponent()
231
375
  }
232
376
 
233
- renderCustomComponent(_customComponent) {
234
- this.webchatRef.current.renderCustomComponent(_customComponent)
377
+ renderCustomComponent(_customComponent: any) {
378
+ this.webchatRef.current?.renderCustomComponent(_customComponent)
235
379
  }
236
380
 
237
381
  unmountCustomComponent() {
238
- this.webchatRef.current.unmountCustomComponent()
382
+ this.webchatRef.current?.unmountCustomComponent()
239
383
  }
240
384
 
385
+ // TODO: Remove this function because we have openCoverComponent and closeCoverComponent functions
241
386
  toggleCoverComponent() {
242
- this.webchatRef.current.toggleCoverComponent()
387
+ this.webchatRef.current?.toggleCoverComponent()
243
388
  }
244
389
 
245
390
  getMessages() {
246
- return this.webchatRef.current.getMessages()
391
+ return this.webchatRef.current?.getMessages()
247
392
  }
248
393
 
249
394
  clearMessages() {
250
- this.webchatRef.current.clearMessages()
395
+ this.webchatRef.current?.clearMessages()
251
396
  }
252
397
 
253
398
  async getVisibility() {
254
399
  return this.resolveWebchatVisibility({
255
- appId: this.appId,
400
+ appId: this.appId!,
256
401
  visibility: this.visibility,
257
402
  })
258
403
  }
259
404
 
260
405
  getLastMessageUpdate() {
261
- return this.webchatRef.current.getLastMessageUpdate()
406
+ return this.webchatRef.current?.getLastMessageUpdate()
262
407
  }
263
408
 
264
- updateMessageInfo(msgId, messageInfo) {
265
- return this.webchatRef.current.updateMessageInfo(msgId, messageInfo)
409
+ updateMessageInfo(msgId: string, messageInfo: any) {
410
+ return this.webchatRef.current?.updateMessageInfo(msgId, messageInfo)
266
411
  }
267
412
 
268
- updateWebchatSettings(settings) {
269
- return this.webchatRef.current.updateWebchatSettings(settings)
413
+ updateWebchatSettings(settings: WebchatSettingsProps) {
414
+ return this.webchatRef.current?.updateWebchatSettings(settings)
270
415
  }
271
416
 
272
417
  // eslint-disable-next-line complexity
273
- getComponent(host, optionsAtRuntime = {}) {
418
+ getComponent(host: HTMLDivElement, optionsAtRuntime: WebchatAppProps = {}) {
274
419
  let {
275
420
  theme = {},
276
421
  persistentMenu,
@@ -319,10 +464,12 @@ export class WebchatApp {
319
464
  this.appId = appId || this.appId
320
465
  this.hostId = hostId || this.hostId
321
466
  this.createRootElement(host)
467
+
322
468
  return (
323
469
  <Webchat
324
470
  {...webchatOptions}
325
471
  ref={this.webchatRef}
472
+ // @ts-ignore
326
473
  host={this.host}
327
474
  shadowDOM={this.shadowDOM}
328
475
  theme={theme}
@@ -337,22 +484,24 @@ export class WebchatApp {
337
484
  storageKey={this.storageKey}
338
485
  defaultDelay={defaultDelay}
339
486
  defaultTyping={defaultTyping}
340
- onInit={(...args) => this.onInitWebchat(...args)}
341
- onOpen={(...args) => this.onOpenWebchat(...args)}
342
- onClose={(...args) => this.onCloseWebchat(...args)}
343
- onUserInput={(...args) => this.onUserInput(...args)}
487
+ onInit={(...args: [any]) => this.onInitWebchat(...args)}
488
+ onOpen={(...args: [any]) => this.onOpenWebchat(...args)}
489
+ onClose={(...args: [any]) => this.onCloseWebchat(...args)}
490
+ onUserInput={(...args: [any]) => this.onUserInput(...args)}
344
491
  onStateChange={webchatState => this.onStateChange(webchatState)}
345
- onTrackEvent={(...args) => this.onTrackEvent(...args)}
492
+ onTrackEvent={(
493
+ request: ActionRequest,
494
+ eventName: string,
495
+ args?: EventArgs
496
+ ) => this.onTrackEvent && this.onTrackEvent(request, eventName, args)}
346
497
  server={server}
347
498
  />
348
499
  )
349
500
  }
350
501
 
351
- async isWebchatVisible({ appId }) {
502
+ async isWebchatVisible(appId: string): Promise<boolean> {
352
503
  try {
353
- const { status } = await HubtypeService.getWebchatVisibility({
354
- appId,
355
- })
504
+ const { status } = await HubtypeService.getWebchatVisibility(appId)
356
505
  return status === 200
357
506
  } catch (e) {
358
507
  return false
@@ -360,16 +509,36 @@ export class WebchatApp {
360
509
  }
361
510
 
362
511
  isOnline() {
363
- return this.webchatRef.current.isOnline()
512
+ return this.webchatRef.current?.isOnline()
364
513
  }
365
514
 
366
- async resolveWebchatVisibility(optionsAtRuntime) {
515
+ async resolveWebchatVisibility(
516
+ optionsAtRuntime?: WebchatAppProps
517
+ ): Promise<boolean> {
518
+ if (!optionsAtRuntime) {
519
+ // If optionsAtRuntime is not provided, always render the webchat
520
+ return true
521
+ }
522
+
367
523
  let { appId, visibility } = optionsAtRuntime
368
524
  visibility = visibility || this.visibility
369
- if (visibility === undefined || visibility === true) return true
370
- if (typeof visibility === 'function' && visibility()) return true
371
- if (visibility === 'dynamic' && (await this.isWebchatVisible({ appId })))
525
+
526
+ if (visibility === undefined || visibility === true) {
372
527
  return true
528
+ }
529
+
530
+ if (typeof visibility === 'function' && visibility()) {
531
+ return true
532
+ }
533
+
534
+ if (
535
+ appId &&
536
+ visibility === 'dynamic' &&
537
+ (await this.isWebchatVisible(appId))
538
+ ) {
539
+ return true
540
+ }
541
+
373
542
  return false
374
543
  }
375
544
 
@@ -379,7 +548,7 @@ export class WebchatApp {
379
548
  if (this.storage) this.storage.removeItem(this.storageKey)
380
549
  }
381
550
 
382
- async render(dest, optionsAtRuntime = {}) {
551
+ async render(dest: HTMLDivElement, optionsAtRuntime?: WebchatAppProps) {
383
552
  onDOMLoaded(async () => {
384
553
  const isVisible = await this.resolveWebchatVisibility(optionsAtRuntime)
385
554
  if (isVisible) {