@botpress/webchat 0.5.1 → 1.0.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 (295) hide show
  1. package/dist/App.d.ts +10 -0
  2. package/dist/Utils/colors.d.ts +18 -0
  3. package/dist/Utils/eventEmitter.d.ts +12 -0
  4. package/dist/Utils/index.d.ts +2 -0
  5. package/dist/client/MessagingClient.d.ts +27 -0
  6. package/dist/client/adapters/Audio.d.ts +19 -0
  7. package/dist/client/adapters/Card.d.ts +188 -0
  8. package/dist/client/adapters/Carousel.d.ts +147 -0
  9. package/dist/client/adapters/Choice.d.ts +45 -0
  10. package/dist/client/adapters/Dropdown.d.ts +46 -0
  11. package/dist/client/adapters/File.d.ts +19 -0
  12. package/dist/client/adapters/Image.d.ts +19 -0
  13. package/dist/client/adapters/Location.d.ts +27 -0
  14. package/dist/client/adapters/Message.d.ts +433 -0
  15. package/dist/client/adapters/Text.d.ts +20 -0
  16. package/dist/client/adapters/Utils.d.ts +5 -0
  17. package/dist/client/adapters/Video.d.ts +19 -0
  18. package/dist/client/adapters/Voice.d.ts +15 -0
  19. package/dist/client/adapters/index.d.ts +12 -0
  20. package/dist/client/index.d.ts +2 -0
  21. package/dist/components/Avatar.d.ts +6 -0
  22. package/dist/components/Block.d.ts +4 -0
  23. package/dist/components/Composer.d.ts +12 -14
  24. package/dist/components/Container.d.ts +2 -12
  25. package/dist/components/Header.d.ts +36 -26
  26. package/dist/components/LoadingIndicator.d.ts +2 -0
  27. package/dist/components/Message.d.ts +7 -0
  28. package/dist/components/MessageList.d.ts +2 -0
  29. package/dist/components/Modal.d.ts +17 -0
  30. package/dist/components/RestartConversation.d.ts +5 -0
  31. package/dist/components/Webchat.d.ts +6 -0
  32. package/dist/components/dev-tools/DevTools.d.ts +1 -0
  33. package/dist/components/dev-tools/configuration.d.ts +2 -0
  34. package/dist/components/dev-tools/helpers.d.ts +5 -0
  35. package/dist/components/index.d.ts +12 -0
  36. package/dist/components/renderers/Audio.d.ts +3 -0
  37. package/dist/components/renderers/Bubble.d.ts +5 -0
  38. package/dist/components/renderers/Button.d.ts +4 -0
  39. package/dist/components/renderers/Carousel.d.ts +3 -0
  40. package/dist/components/renderers/Column.d.ts +5 -0
  41. package/dist/components/renderers/Dropdown.d.ts +5 -0
  42. package/dist/components/renderers/File.d.ts +3 -0
  43. package/dist/components/renderers/Image.d.ts +3 -0
  44. package/dist/components/renderers/Location.d.ts +3 -0
  45. package/dist/components/renderers/Row.d.ts +5 -0
  46. package/dist/components/renderers/Text.d.ts +4 -0
  47. package/dist/components/renderers/Video.d.ts +3 -0
  48. package/dist/components/renderers/index.d.ts +2 -0
  49. package/dist/contexts/ComposerContext.d.ts +8 -0
  50. package/dist/contexts/MessageContext.d.ts +8 -0
  51. package/dist/contexts/ModalContext.d.ts +14 -0
  52. package/dist/contexts/WebchatContext.d.ts +56 -0
  53. package/dist/contexts/index.d.ts +4 -0
  54. package/dist/hooks/index.d.ts +3 -0
  55. package/dist/hooks/useImageSize.d.ts +2 -0
  56. package/dist/hooks/useRefresh.d.ts +10 -0
  57. package/dist/hooks/useWebchatStore.d.ts +30 -0
  58. package/dist/index.d.ts +3 -17
  59. package/dist/index.js +43569 -48
  60. package/dist/index.umd.cjs +702 -0
  61. package/dist/main.d.ts +11 -13
  62. package/dist/providers/ModalProvider.d.ts +8 -0
  63. package/dist/providers/WebchatProvider.d.ts +13 -0
  64. package/dist/providers/index.d.ts +2 -0
  65. package/dist/schemas/index.d.ts +1 -0
  66. package/dist/schemas/theme.d.ts +3371 -0
  67. package/dist/services/clipboard.d.ts +1 -0
  68. package/dist/services/images.d.ts +2 -0
  69. package/dist/services/index.d.ts +3 -0
  70. package/dist/services/toast.d.ts +17 -0
  71. package/dist/themes/dawn.d.ts +2 -0
  72. package/dist/themes/duskTheme.d.ts +2 -0
  73. package/dist/themes/eggplant.d.ts +2 -0
  74. package/dist/themes/galaxy.d.ts +2 -0
  75. package/dist/themes/index.d.ts +6 -0
  76. package/dist/themes/midnight.d.ts +2 -0
  77. package/dist/themes/prism.d.ts +2 -0
  78. package/dist/twind.config.d.ts +9 -0
  79. package/dist/types/block-type.d.ts +93 -0
  80. package/dist/types/image.d.ts +11 -0
  81. package/dist/types/index.d.ts +2 -0
  82. package/dist/vite.svg +1 -0
  83. package/index.html +18 -0
  84. package/package.json +60 -49
  85. package/public/vite.svg +1 -0
  86. package/src/App.tsx +41 -0
  87. package/src/Utils/colors.ts +45 -0
  88. package/src/Utils/eventEmitter.ts +31 -0
  89. package/src/Utils/index.ts +2 -0
  90. package/src/assets/check-circle-bold.svg +5 -0
  91. package/src/assets/chevron-up.svg +3 -0
  92. package/src/assets/file-05.svg +6 -0
  93. package/src/assets/globe-02.svg +6 -0
  94. package/src/assets/help-circle.svg +3 -0
  95. package/src/assets/info-circle.svg +3 -0
  96. package/src/assets/lock-01.svg +4 -0
  97. package/src/assets/mail-01.svg +6 -0
  98. package/src/assets/minus-circle.svg +3 -0
  99. package/src/assets/phone.svg +6 -0
  100. package/src/assets/send-03.svg +4 -0
  101. package/src/assets/share-04.svg +5 -0
  102. package/src/assets/slash-circle-01.svg +3 -0
  103. package/src/assets/x-circle-bold.svg +5 -0
  104. package/src/assets/x-close.svg +3 -0
  105. package/src/assets/x.svg +3 -0
  106. package/src/client/MessagingClient.ts +87 -0
  107. package/src/client/adapters/Audio.ts +10 -0
  108. package/src/client/adapters/Card.ts +104 -0
  109. package/src/client/adapters/Carousel.ts +11 -0
  110. package/src/client/adapters/Choice.ts +48 -0
  111. package/src/client/adapters/Dropdown.ts +39 -0
  112. package/src/client/adapters/File.ts +10 -0
  113. package/src/client/adapters/Image.ts +10 -0
  114. package/src/client/adapters/Location.ts +18 -0
  115. package/src/client/adapters/Message.ts +26 -0
  116. package/src/client/adapters/Text.ts +11 -0
  117. package/src/client/adapters/Utils.ts +11 -0
  118. package/src/client/adapters/Video.ts +10 -0
  119. package/src/client/adapters/Voice.ts +9 -0
  120. package/src/client/adapters/index.ts +12 -0
  121. package/src/client/index.ts +2 -0
  122. package/src/components/Avatar.tsx +22 -0
  123. package/src/components/Block.tsx +17 -0
  124. package/src/components/Composer.tsx +115 -0
  125. package/src/components/Container.tsx +17 -0
  126. package/src/components/Header.tsx +141 -0
  127. package/src/components/LoadingIndicator.tsx +15 -0
  128. package/src/components/Message.tsx +52 -0
  129. package/src/components/MessageList.tsx +75 -0
  130. package/src/components/Modal.tsx +49 -0
  131. package/src/components/RestartConversation.tsx +52 -0
  132. package/src/components/Webchat.tsx +68 -0
  133. package/src/components/dev-tools/DevTools.tsx +496 -0
  134. package/src/components/dev-tools/configuration.tsx +27 -0
  135. package/src/components/dev-tools/helpers.ts +21 -0
  136. package/src/components/index.ts +12 -0
  137. package/src/components/renderers/Audio.tsx +11 -0
  138. package/src/components/renderers/Bubble.tsx +12 -0
  139. package/src/components/renderers/Button.tsx +59 -0
  140. package/src/components/renderers/Carousel.tsx +51 -0
  141. package/src/components/renderers/Column.tsx +22 -0
  142. package/src/components/renderers/Dropdown.tsx +170 -0
  143. package/src/components/renderers/File.tsx +13 -0
  144. package/src/components/renderers/Image.tsx +63 -0
  145. package/src/components/renderers/Location.tsx +16 -0
  146. package/src/components/renderers/Row.tsx +22 -0
  147. package/src/components/renderers/Text.tsx +32 -0
  148. package/src/components/renderers/Video.tsx +11 -0
  149. package/src/components/renderers/index.ts +28 -0
  150. package/src/contexts/ComposerContext.ts +16 -0
  151. package/src/contexts/MessageContext.ts +16 -0
  152. package/src/contexts/ModalContext.ts +19 -0
  153. package/src/contexts/WebchatContext.ts +61 -0
  154. package/src/contexts/index.ts +4 -0
  155. package/src/hooks/index.ts +3 -0
  156. package/src/hooks/useImageSize.ts +30 -0
  157. package/src/hooks/useRefresh.ts +33 -0
  158. package/src/hooks/useWebchatStore.ts +45 -0
  159. package/src/index.css +18 -0
  160. package/src/index.ts +3 -0
  161. package/src/main.tsx +33 -0
  162. package/src/providers/ModalProvider.tsx +35 -0
  163. package/src/providers/WebchatProvider.tsx +107 -0
  164. package/src/providers/index.ts +2 -0
  165. package/src/schemas/index.ts +1 -0
  166. package/src/schemas/theme.ts +188 -0
  167. package/src/services/clipboard.ts +8 -0
  168. package/src/services/images.ts +39 -0
  169. package/src/services/index.ts +3 -0
  170. package/src/services/toast.tsx +71 -0
  171. package/src/themes/dawn.ts +277 -0
  172. package/src/themes/duskTheme.ts +349 -0
  173. package/src/themes/eggplant.ts +353 -0
  174. package/src/themes/galaxy.ts +323 -0
  175. package/src/themes/index.ts +6 -0
  176. package/src/themes/midnight.ts +276 -0
  177. package/src/themes/prism.ts +349 -0
  178. package/src/twind.config.ts +31 -0
  179. package/src/types/block-type.ts +150 -0
  180. package/src/types/image.ts +10 -0
  181. package/src/types/index.ts +2 -0
  182. package/src/vite-env.d.ts +1 -0
  183. package/tailwind.config.js +0 -0
  184. package/tsconfig.json +30 -0
  185. package/tsconfig.node.json +10 -0
  186. package/vite.config.ts +31 -0
  187. package/README.md +0 -41
  188. package/assets/fonts/roboto/roboto.woff2 +0 -0
  189. package/assets/fonts/roboto/roboto500.woff2 +0 -0
  190. package/assets/fonts/roboto.css +0 -128
  191. package/assets/notification.mp3 +0 -0
  192. package/dist/components/Composer.js +0 -118
  193. package/dist/components/Container.js +0 -62
  194. package/dist/components/ConversationList.d.ts +0 -10
  195. package/dist/components/ConversationList.js +0 -41
  196. package/dist/components/Footer.d.ts +0 -3
  197. package/dist/components/Footer.js +0 -21
  198. package/dist/components/Header.js +0 -181
  199. package/dist/components/VoiceRecorder.d.ts +0 -10
  200. package/dist/components/VoiceRecorder.js +0 -137
  201. package/dist/components/common/Avatar/index.d.ts +0 -9
  202. package/dist/components/common/Avatar/index.js +0 -13
  203. package/dist/components/common/BotInfo/index.d.ts +0 -10
  204. package/dist/components/common/BotInfo/index.js +0 -107
  205. package/dist/components/common/BotInfo/style.scss +0 -88
  206. package/dist/components/common/ConfirmDialog/index.d.ts +0 -11
  207. package/dist/components/common/ConfirmDialog/index.js +0 -78
  208. package/dist/components/common/ConfirmDialog/style.module.scss +0 -48
  209. package/dist/components/common/Dialog/index.d.ts +0 -17
  210. package/dist/components/common/Dialog/index.js +0 -57
  211. package/dist/components/common/Dialog/style.module.scss +0 -29
  212. package/dist/components/common/ToolTip/index.d.ts +0 -10
  213. package/dist/components/common/ToolTip/index.js +0 -163
  214. package/dist/components/common/ToolTip/style.module.scss +0 -108
  215. package/dist/components/common/ToolTip/utils.d.ts +0 -15
  216. package/dist/components/common/ToolTip/utils.js +0 -78
  217. package/dist/components/common/variables.scss +0 -38
  218. package/dist/components/messages/InlineFeedback.d.ts +0 -11
  219. package/dist/components/messages/InlineFeedback.js +0 -56
  220. package/dist/components/messages/Message.d.ts +0 -11
  221. package/dist/components/messages/Message.js +0 -106
  222. package/dist/components/messages/MessageGroup.d.ts +0 -23
  223. package/dist/components/messages/MessageGroup.js +0 -63
  224. package/dist/components/messages/MessageList.d.ts +0 -10
  225. package/dist/components/messages/MessageList.js +0 -148
  226. package/dist/core/api.d.ts +0 -23
  227. package/dist/core/api.js +0 -117
  228. package/dist/core/constants.d.ts +0 -14
  229. package/dist/core/constants.js +0 -29
  230. package/dist/core/socket.d.ts +0 -14
  231. package/dist/core/socket.js +0 -57
  232. package/dist/declaration.d.ts +0 -2
  233. package/dist/declaration.js +0 -1
  234. package/dist/fonts/roboto.d.ts +0 -4
  235. package/dist/fonts/roboto.js +0 -9
  236. package/dist/globals.d.ts +0 -7
  237. package/dist/globals.js +0 -2
  238. package/dist/icons/Add.d.ts +0 -6
  239. package/dist/icons/Add.js +0 -10
  240. package/dist/icons/Cancel.d.ts +0 -5
  241. package/dist/icons/Cancel.js +0 -10
  242. package/dist/icons/Chat.d.ts +0 -6
  243. package/dist/icons/Chat.js +0 -9
  244. package/dist/icons/Close.d.ts +0 -3
  245. package/dist/icons/Close.js +0 -10
  246. package/dist/icons/Delete.d.ts +0 -3
  247. package/dist/icons/Delete.js +0 -11
  248. package/dist/icons/Download.d.ts +0 -3
  249. package/dist/icons/Download.js +0 -10
  250. package/dist/icons/Email.d.ts +0 -3
  251. package/dist/icons/Email.js +0 -8
  252. package/dist/icons/Information.d.ts +0 -3
  253. package/dist/icons/Information.js +0 -12
  254. package/dist/icons/List.d.ts +0 -3
  255. package/dist/icons/List.js +0 -15
  256. package/dist/icons/Microphone.d.ts +0 -5
  257. package/dist/icons/Microphone.js +0 -12
  258. package/dist/icons/Phone.d.ts +0 -3
  259. package/dist/icons/Phone.js +0 -8
  260. package/dist/icons/Reload.d.ts +0 -3
  261. package/dist/icons/Reload.js +0 -10
  262. package/dist/icons/ThumbsDown.d.ts +0 -3
  263. package/dist/icons/ThumbsDown.js +0 -11
  264. package/dist/icons/ThumbsUp.d.ts +0 -3
  265. package/dist/icons/ThumbsUp.js +0 -11
  266. package/dist/icons/Website.d.ts +0 -3
  267. package/dist/icons/Website.js +0 -8
  268. package/dist/main.js +0 -336
  269. package/dist/store/composer.d.ts +0 -17
  270. package/dist/store/composer.js +0 -98
  271. package/dist/store/index.d.ts +0 -89
  272. package/dist/store/index.js +0 -604
  273. package/dist/store/view.d.ts +0 -61
  274. package/dist/store/view.js +0 -365
  275. package/dist/translations/ar.json +0 -30
  276. package/dist/translations/de.json +0 -32
  277. package/dist/translations/en.json +0 -40
  278. package/dist/translations/es.json +0 -19
  279. package/dist/translations/fr.json +0 -40
  280. package/dist/translations/index.d.ts +0 -9
  281. package/dist/translations/index.js +0 -95
  282. package/dist/translations/it.json +0 -38
  283. package/dist/translations/pt.json +0 -19
  284. package/dist/translations/ru.json +0 -24
  285. package/dist/translations/uk.json +0 -24
  286. package/dist/typings.d.ts +0 -410
  287. package/dist/typings.js +0 -2
  288. package/dist/utils/analytics.d.ts +0 -5
  289. package/dist/utils/analytics.js +0 -37
  290. package/dist/utils/index.d.ts +0 -3
  291. package/dist/utils/index.js +0 -27
  292. package/dist/utils/storage.d.ts +0 -16
  293. package/dist/utils/storage.js +0 -129
  294. package/dist/utils/webchatEvents.d.ts +0 -2
  295. package/dist/utils/webchatEvents.js +0 -14
@@ -0,0 +1,87 @@
1
+ import {
2
+ MessagingSocket,
3
+ type MessagingSocketOptions,
4
+ type UserCredentials,
5
+ type Message,
6
+ } from '@botpress/messaging-socket'
7
+ import { EventEmitter } from '../Utils'
8
+
9
+ type Events = {
10
+ connect: UserCredentials
11
+ disconnect: undefined
12
+ user: string | undefined
13
+ conversation: string | undefined
14
+ message: Message
15
+ messageSent: string
16
+ startTyping: undefined
17
+ stopTyping: undefined
18
+ }
19
+ export class WebchatClient {
20
+ private socket: MessagingSocket
21
+ public userId: string | undefined
22
+ private conversationId: string | undefined
23
+ private userToken: string | undefined
24
+ private connected = false
25
+ private readonly emitter: EventEmitter<Events>
26
+ public on: EventEmitter<Events>['on']
27
+
28
+ constructor(options: MessagingSocketOptions) {
29
+ this.socket = new MessagingSocket(options)
30
+ this.emitter = new EventEmitter()
31
+
32
+ this.on = this.emitter.on.bind(this.emitter)
33
+
34
+ this.socket.on('connect', (event) => {
35
+ this.emitter.emit('connect', event)
36
+ })
37
+
38
+ this.socket.on('disconnect', (event) => {
39
+ this.emitter.emit('disconnect', event)
40
+ })
41
+
42
+ this.socket.on('message', (message) => {
43
+ this.emitter.emit('message', message)
44
+ })
45
+
46
+ this.socket.on('conversation', (conversation) => {
47
+ this.emitter.emit('conversation', conversation)
48
+ })
49
+ this.socket.on('user', (user) => {
50
+ this.emitter.emit('user', user)
51
+ })
52
+ }
53
+
54
+ public async connect(creds?: UserCredentials) {
55
+ if (this.connected) {
56
+ return
57
+ }
58
+ this.connected = true
59
+ const user = await this.socket.connect(creds).catch((err) => {
60
+ this.connected = false
61
+ throw err
62
+ })
63
+ this.userId = user.userId
64
+ this.userToken = user.userToken
65
+
66
+ return user
67
+ }
68
+
69
+ public async disconnect() {
70
+ await this.socket.disconnect()
71
+ this.connected = false
72
+ }
73
+
74
+ public async sendMessage(message: string) {
75
+ if (!this.conversationId) {
76
+ await this.newConversation()
77
+ }
78
+
79
+ await this.socket.sendText(message)
80
+ this.emitter.emit('messageSent', message)
81
+ }
82
+
83
+ public async newConversation() {
84
+ const { id } = await this.socket.createConversation()
85
+ this.conversationId = id
86
+ }
87
+ }
@@ -0,0 +1,10 @@
1
+ import { z } from 'zod'
2
+ import { AudioBlock } from '../../types'
3
+
4
+ export const AudioSchema = z
5
+ .object({
6
+ type: z.literal('audio'),
7
+ audio: z.string(),
8
+ title: z.string().optional(),
9
+ })
10
+ .transform<AudioBlock>(({ type, audio }) => ({ type, url: audio }))
@@ -0,0 +1,104 @@
1
+ import { z } from 'zod'
2
+ import { ButtonBlock, ColumnBlock, ImageBlock, TextBlock } from '../../types'
3
+ import { WithBubble, withBubble } from './Utils'
4
+
5
+ const ActionSaySomethingSchema = z
6
+ .object({
7
+ title: z.string(),
8
+ action: z.literal('Say something'),
9
+ text: z.string(),
10
+ })
11
+ .transform<ButtonBlock>(({ title, text }) => ({
12
+ type: 'button',
13
+ variant: 'action',
14
+ text: title,
15
+ buttonValue: text,
16
+ reusable: true,
17
+ }))
18
+
19
+ const ActionOpenURLSchema = z
20
+ .object({
21
+ title: z.string(),
22
+ action: z.literal('Open URL'),
23
+ url: z.string(),
24
+ })
25
+ .transform<ButtonBlock>(({ title, url }) => ({
26
+ type: 'button',
27
+ variant: 'link',
28
+ text: title,
29
+ buttonValue: url,
30
+ reusable: true,
31
+ }))
32
+
33
+ const ActionPostbackSchema = z
34
+ .object({
35
+ title: z.string(),
36
+ action: z.literal('Postback'),
37
+ payload: z.string(),
38
+ })
39
+ .transform<ButtonBlock>(({ title, payload }) => ({
40
+ type: 'button',
41
+ variant: 'action',
42
+ text: title,
43
+ buttonValue: payload,
44
+ reusable: true,
45
+ }))
46
+
47
+ const ActionButtonSchema = z.union([ActionSaySomethingSchema, ActionOpenURLSchema, ActionPostbackSchema])
48
+
49
+ export type CardBlockType = { blocks: CardBlocks } & Omit<ColumnBlock, 'blocks'>
50
+ type CardBlocks = (ImageBlock | TextBlock | { type: 'row'; blocks: ButtonBlock[] })[]
51
+
52
+ export const BaseCardSchema = z.object({
53
+ type: z.literal('card'),
54
+ title: z.string().optional(),
55
+ subtitle: z.string().optional(),
56
+ image: z.string().optional(),
57
+ actions: z.array(ActionButtonSchema),
58
+ })
59
+
60
+ export const CardSchemaTransform = ({
61
+ title,
62
+ image,
63
+ actions,
64
+ subtitle,
65
+ }: Omit<z.infer<typeof BaseCardSchema>, 'type'>): CardBlockType => {
66
+ const cardBlocks: CardBlocks = []
67
+
68
+ if (image) {
69
+ cardBlocks.push({
70
+ type: 'image',
71
+ url: image,
72
+ })
73
+ }
74
+
75
+ if (title) {
76
+ cardBlocks.push({
77
+ type: 'text',
78
+ text: `## ${title}`,
79
+ })
80
+ }
81
+ if (subtitle) {
82
+ cardBlocks.push({
83
+ type: 'text',
84
+ text: subtitle,
85
+ })
86
+ }
87
+
88
+ if (actions.length > 0) {
89
+ cardBlocks.push({
90
+ type: 'row',
91
+ blocks: actions,
92
+ })
93
+ }
94
+
95
+ return {
96
+ type: 'column',
97
+ horizontalAlignment: 'center',
98
+ blocks: [...cardBlocks],
99
+ }
100
+ }
101
+
102
+ export const CardSchema = BaseCardSchema.transform<WithBubble<CardBlockType>>((card) =>
103
+ withBubble(CardSchemaTransform(card))
104
+ )
@@ -0,0 +1,11 @@
1
+ import { z } from 'zod'
2
+ import { BaseCardSchema, CardBlockType, CardSchemaTransform } from './Card'
3
+ import { CarouselBlock } from '../../types'
4
+
5
+ type CarouselBlockType = { blocks: CardBlockType[] } & Omit<CarouselBlock, 'blocks'>
6
+ export const CarouselSchema = z
7
+ .object({
8
+ type: z.literal('carousel'),
9
+ items: z.array(BaseCardSchema.omit({ type: true }).transform<CardBlockType>(CardSchemaTransform)),
10
+ })
11
+ .transform<CarouselBlockType>(({ items }) => ({ type: 'carousel', blocks: items }))
@@ -0,0 +1,48 @@
1
+ import { z } from 'zod'
2
+ import { v4 } from 'uuid'
3
+ import { ButtonBlock, ColumnBlock, RowBlock, TextBlock } from '../../types'
4
+ import { WithBubble, withBubble } from './Utils'
5
+
6
+ //TODO: Fix choice title that will not show up
7
+ type ButtonBlocks = { blocks: ButtonBlock[] } & Omit<RowBlock, 'blocks'>
8
+
9
+ type ChoiceBlockType = { blocks: (ButtonBlocks | TextBlock)[] } & Omit<ColumnBlock, 'blocks'>
10
+ export const ChoiceSchema = z
11
+ .object({
12
+ type: z.literal('single-choice'),
13
+ text: z.string(),
14
+ choices: z.array(
15
+ z.object({
16
+ title: z.string(),
17
+ value: z.string(),
18
+ })
19
+ ),
20
+ })
21
+ .transform<WithBubble<ChoiceBlockType>>(({ choices, text }) => {
22
+ const groupId = v4()
23
+
24
+ const blocks: (ButtonBlocks | TextBlock)[] = [
25
+ {
26
+ type: 'row',
27
+ blocks: choices.map(({ title, value }) => ({
28
+ type: 'button',
29
+ variant: 'action',
30
+ text: title,
31
+ buttonValue: value,
32
+ groupId,
33
+ })),
34
+ },
35
+ ]
36
+
37
+ if (text) {
38
+ blocks.unshift({
39
+ type: 'text',
40
+ text,
41
+ })
42
+ }
43
+
44
+ return withBubble({
45
+ type: 'column',
46
+ blocks,
47
+ })
48
+ })
@@ -0,0 +1,39 @@
1
+ import { z } from 'zod'
2
+ import { v4 } from 'uuid'
3
+ import { ButtonBlock, ColumnBlock, DropdownBlock, RowBlock, TextBlock } from '../../types'
4
+ import { WithBubble, withBubble } from './Utils'
5
+
6
+ type DropdownBlockType = { blocks: (DropdownBlock | TextBlock)[] } & Omit<ColumnBlock, 'blocks'>
7
+ export const DropdownSchema = z
8
+ .object({
9
+ type: z.literal('dropdown'),
10
+ text: z.string().optional(),
11
+ message: z.string().optional(),
12
+ options: z.array(
13
+ z.object({
14
+ label: z.string(),
15
+ value: z.string(),
16
+ })
17
+ ),
18
+ })
19
+ .transform<WithBubble<DropdownBlockType>>(({ options, text }) => {
20
+ const blocks: (DropdownBlock | TextBlock)[] = [
21
+ {
22
+ type: 'dropdown',
23
+ label: text ?? 'Select an option',
24
+ options,
25
+ },
26
+ ]
27
+
28
+ if (text) {
29
+ blocks.unshift({
30
+ type: 'text',
31
+ text,
32
+ })
33
+ }
34
+
35
+ return withBubble({
36
+ type: 'column',
37
+ blocks,
38
+ })
39
+ })
@@ -0,0 +1,10 @@
1
+ import { z } from 'zod'
2
+ import { FileBlock } from '../../types'
3
+
4
+ export const FileSchema = z
5
+ .object({
6
+ type: z.literal('file'),
7
+ file: z.string(),
8
+ title: z.string().optional(),
9
+ })
10
+ .transform<FileBlock>(({ file, ...props }) => ({ url: file, ...props }))
@@ -0,0 +1,10 @@
1
+ import { z } from 'zod'
2
+ import { ImageBlock } from '../../types'
3
+
4
+ export const ImageSchema = z
5
+ .object({
6
+ type: z.literal('image'),
7
+ image: z.string(),
8
+ title: z.string().optional(),
9
+ })
10
+ .transform<ImageBlock>(({ type, image }) => ({ type, url: image }))
@@ -0,0 +1,18 @@
1
+ import { z } from 'zod'
2
+ import { LocationBlock } from '../../types'
3
+
4
+ //TODO: Decide what to do with title and address and fix ui when they are empty
5
+ export const LocationSchema = z
6
+ .object({
7
+ type: z.literal('location'),
8
+ latitude: z.number(),
9
+ longitude: z.number(),
10
+ address: z.string().optional(),
11
+ title: z.string().optional(),
12
+ })
13
+ .transform<LocationBlock>(({ type, latitude, longitude, title, address }) => ({
14
+ type,
15
+ latitude,
16
+ longitude,
17
+ title: title ?? address ?? 'View on map',
18
+ }))
@@ -0,0 +1,26 @@
1
+ import { z } from 'zod'
2
+ import { TextSchema } from './Text'
3
+ import { ImageSchema } from './Image'
4
+ import { AudioSchema } from './Audio'
5
+ import { VideoSchema } from './Video'
6
+ import { CarouselSchema } from './Carousel'
7
+ import { CardSchema } from './Card'
8
+ import { LocationSchema } from './Location'
9
+ import { FileSchema } from './File'
10
+ import { ChoiceSchema } from './Choice'
11
+ import { VoiceSchema } from './Voice'
12
+ import { DropdownSchema } from './Dropdown.ts'
13
+
14
+ export const MessageContentSchema = z.union([
15
+ TextSchema,
16
+ ImageSchema,
17
+ AudioSchema,
18
+ VideoSchema,
19
+ CarouselSchema,
20
+ CardSchema,
21
+ LocationSchema,
22
+ FileSchema,
23
+ VoiceSchema,
24
+ ChoiceSchema,
25
+ DropdownSchema,
26
+ ])
@@ -0,0 +1,11 @@
1
+ import { z } from 'zod'
2
+ import { TextBlock } from '../../types'
3
+ import { WithBubble, withBubble } from './Utils'
4
+
5
+ export const TextSchema = z
6
+ .object({
7
+ type: z.literal('text'),
8
+ text: z.string(),
9
+ markdown: z.boolean().optional(),
10
+ })
11
+ .transform<WithBubble<TextBlock>>(({ type, text }) => withBubble({ type, text }))
@@ -0,0 +1,11 @@
1
+ import { BlockObject, BubbleBlock } from '../../types'
2
+
3
+ export type WithBubble<T extends BlockObject> = { block: T } & Omit<BubbleBlock, 'block'>
4
+
5
+ //with bubble function with a generic type param
6
+ export function withBubble<T extends BlockObject>(block: T): WithBubble<T> {
7
+ return {
8
+ block,
9
+ type: 'bubble',
10
+ }
11
+ }
@@ -0,0 +1,10 @@
1
+ import { z } from 'zod'
2
+ import { VideoBlock } from '../../types'
3
+
4
+ export const VideoSchema = z
5
+ .object({
6
+ type: z.literal('video'),
7
+ video: z.string(),
8
+ title: z.string().optional(),
9
+ })
10
+ .transform<VideoBlock>(({ type, video }) => ({ type, url: video }))
@@ -0,0 +1,9 @@
1
+ import { z } from 'zod'
2
+ import { AudioBlock } from '../../types'
3
+
4
+ export const VoiceSchema = z
5
+ .object({
6
+ type: z.literal('voice'),
7
+ audio: z.string(),
8
+ })
9
+ .transform<AudioBlock>(({ audio }) => ({ type: 'audio', url: audio }))
@@ -0,0 +1,12 @@
1
+ export * from './Audio'
2
+ export * from './Card'
3
+ export * from './Carousel'
4
+ export * from './Choice'
5
+ export * from './File'
6
+ export * from './Image'
7
+ export * from './Location'
8
+ export * from './Message'
9
+ export * from './Text'
10
+ export * from './Utils'
11
+ export * from './Video'
12
+ export * from './Voice'
@@ -0,0 +1,2 @@
1
+ export * from './adapters'
2
+ export * from './MessagingClient'
@@ -0,0 +1,22 @@
1
+ import * as AvatarPrimitive from '@radix-ui/react-avatar'
2
+ import { ComponentPropsWithoutRef, ElementRef, forwardRef } from 'react'
3
+
4
+ const Avatar = forwardRef<
5
+ ElementRef<typeof AvatarPrimitive.Root>,
6
+ ComponentPropsWithoutRef<typeof AvatarPrimitive.Root>
7
+ >((props, ref) => <AvatarPrimitive.Root ref={ref} {...props} />)
8
+ Avatar.displayName = AvatarPrimitive.Root.displayName
9
+
10
+ const AvatarImage = forwardRef<
11
+ ElementRef<typeof AvatarPrimitive.Image>,
12
+ ComponentPropsWithoutRef<typeof AvatarPrimitive.Image>
13
+ >((props, ref) => <AvatarPrimitive.Image ref={ref} {...props} />)
14
+ AvatarImage.displayName = AvatarPrimitive.Image.displayName
15
+
16
+ const AvatarFallback = forwardRef<
17
+ ElementRef<typeof AvatarPrimitive.Fallback>,
18
+ ComponentPropsWithoutRef<typeof AvatarPrimitive.Fallback>
19
+ >((props, ref) => <AvatarPrimitive.Fallback ref={ref} {...props} />)
20
+ AvatarFallback.displayName = AvatarPrimitive.Fallback.displayName
21
+
22
+ export { Avatar, AvatarImage, AvatarFallback }
@@ -0,0 +1,17 @@
1
+ import { ComponentType } from 'react'
2
+ import { BlockMap, BlockTypes, CommonBlockProps } from '../types'
3
+ import { renderers } from './renderers/index.ts'
4
+ import { Renderers } from '../contexts/WebchatContext.ts'
5
+
6
+ export const Block = <T extends BlockTypes>({
7
+ block,
8
+ styles,
9
+ ...props
10
+ }: {
11
+ block: BlockMap[T]
12
+ } & CommonBlockProps): JSX.Element => {
13
+ const Block = renderers[block.type] as ComponentType<Renderers[T]>
14
+
15
+ // This is a hack to get the types to work, this returns the correct component based on the block type.
16
+ return <Block {...props} {...(block as any)} styles={styles} />
17
+ }
@@ -0,0 +1,115 @@
1
+ import { ComponentProps, FC, forwardRef, memo, useEffect, useRef, useState } from 'react'
2
+ import { useWebchatContext, type StyleOptions, ComposerContext, useComposerContext } from '../contexts'
3
+ import { ArrowUpCircleIcon } from '@heroicons/react/20/solid'
4
+ import { Theme } from '../schemas'
5
+ import { useWebchatStore } from '../hooks'
6
+
7
+ const Composer = memo(
8
+ forwardRef<HTMLDivElement, ComponentProps<'div'>>((props, ref) => {
9
+ const {
10
+ state,
11
+ theme: { composer },
12
+ } = useWebchatContext()
13
+
14
+ const [value, setValue] = useState('')
15
+
16
+ return (
17
+ <ComposerContext.Provider value={{ value, setValue }}>
18
+ <div data-disabled={state.disableComposer} {...props} {...composer?.container} ref={ref} />
19
+ </ComposerContext.Provider>
20
+ )
21
+ })
22
+ )
23
+
24
+ const ComposerInput = memo(({ ...props }: ComponentProps<'textarea'>) => {
25
+ const {
26
+ state,
27
+ theme: { composer },
28
+ client,
29
+ } = useWebchatContext()
30
+ const { value, setValue } = useComposerContext()
31
+
32
+ const ref = useRef<HTMLTextAreaElement>(null)
33
+
34
+ const [historyIndex, setHistoryIndex] = useState(-1)
35
+ const currentUser = useWebchatStore((state) => state.user)
36
+ const messageHistory = useWebchatStore((state) => state.messageHistory)
37
+
38
+ const sendComposerMessage = () => {
39
+ if (!value) return
40
+ void client.sendMessage(value)
41
+ setValue('')
42
+ }
43
+
44
+ useEffect(() => {
45
+ if (ref.current) {
46
+ ref.current.selectionStart = ref.current.value.length
47
+ ref.current.selectionEnd = ref.current.value.length
48
+ }
49
+ }, [value])
50
+
51
+ return (
52
+ <textarea
53
+ {...composer?.input}
54
+ disabled={state.disableComposer}
55
+ ref={ref}
56
+ value={value}
57
+ data-has-value={!!value}
58
+ onChange={(e) => setValue(e.target.value)}
59
+ onKeyDown={(e) => {
60
+ if (state.disableComposer) return
61
+ if (e.key === 'Enter') {
62
+ e.preventDefault()
63
+ sendComposerMessage()
64
+ setHistoryIndex(-1)
65
+ }
66
+ if (e.key === 'ArrowUp') {
67
+ e.preventDefault()
68
+ const userHistory = messageHistory[currentUser?.userId ?? ''] ?? []
69
+ if (historyIndex < userHistory.length - 1) {
70
+ setValue(userHistory[historyIndex + 1])
71
+ }
72
+ setHistoryIndex(() => Math.min(historyIndex + 1, userHistory.length - 1))
73
+ }
74
+ if (e.key === 'ArrowDown') {
75
+ e.preventDefault()
76
+ const userHistory = messageHistory[currentUser?.userId ?? ''] ?? []
77
+ if (historyIndex === 0) {
78
+ setValue('')
79
+ } else {
80
+ setValue(userHistory[historyIndex - 1])
81
+ }
82
+ setHistoryIndex(() => Math.max(historyIndex - 1, -1))
83
+ }
84
+ }}
85
+ {...props}
86
+ />
87
+ )
88
+ })
89
+
90
+ type ComposerButtonStyle = NonNullable<Theme['composer']>['button']
91
+ type ComposerButtonProps = {
92
+ icon?: FC<StyleOptions>
93
+ styles?: ComposerButtonStyle
94
+ } & ComponentProps<'button'>
95
+ const ComposerButton = memo(
96
+ forwardRef<HTMLButtonElement, ComposerButtonProps>(({ icon: Icon = ArrowUpCircleIcon, ...props }, ref) => {
97
+ //TODO: This should be in the context
98
+ const { value, setValue } = useComposerContext()
99
+ const sendComposerMessage = () => {
100
+ if (!value) return
101
+ setValue('')
102
+ }
103
+
104
+ const {
105
+ theme: { composer },
106
+ } = useWebchatContext()
107
+
108
+ return (
109
+ <button ref={ref} {...props} {...composer?.button?.container} disabled={!value} onClick={sendComposerMessage}>
110
+ <Icon {...composer?.button?.icon} />
111
+ </button>
112
+ )
113
+ })
114
+ )
115
+ export { Composer, ComposerInput, ComposerButton }
@@ -0,0 +1,17 @@
1
+ import { ComponentProps, forwardRef } from 'react'
2
+ import { useWebchatContext } from '../contexts'
3
+ import { Toaster } from 'react-hot-toast'
4
+ import { ModalProvider } from '../providers'
5
+
6
+ export const Container = forwardRef<HTMLDivElement, ComponentProps<'div'>>(({ children, ...props }, ref) => {
7
+ const {
8
+ theme: { container },
9
+ } = useWebchatContext()
10
+
11
+ return (
12
+ <div {...props} {...container} ref={ref}>
13
+ <Toaster />
14
+ <ModalProvider>{children}</ModalProvider>
15
+ </div>
16
+ )
17
+ })