@hellotext/hellotext 2.3.8 → 2.4.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 (40) hide show
  1. package/README.md +1 -1
  2. package/dist/hellotext.js +1 -1
  3. package/dist/webchat-emoji-en.js +1 -0
  4. package/dist/webchat-emoji-es.js +1 -0
  5. package/dist/webchat-emoji.js +1 -0
  6. package/index.d.ts +74 -15
  7. package/lib/api/webchats.cjs +20 -0
  8. package/lib/api/webchats.js +20 -0
  9. package/lib/controllers/mixins/usePopover.cjs +7 -2
  10. package/lib/controllers/mixins/usePopover.js +7 -2
  11. package/lib/controllers/webchat/emoji_picker_controller.cjs +49 -10
  12. package/lib/controllers/webchat/emoji_picker_controller.js +66 -8
  13. package/lib/controllers/webchat/useBehaviour.cjs +74 -0
  14. package/lib/controllers/webchat/useBehaviour.js +67 -0
  15. package/lib/controllers/webchat/useOpeningSequence.cjs +103 -0
  16. package/lib/controllers/webchat/useOpeningSequence.js +96 -0
  17. package/lib/controllers/webchat/useTeaser.cjs +163 -0
  18. package/lib/controllers/webchat/useTeaser.js +156 -0
  19. package/lib/controllers/webchat_controller.cjs +165 -22
  20. package/lib/controllers/webchat_controller.js +173 -24
  21. package/lib/core/configuration/webchat.cjs +117 -42
  22. package/lib/core/configuration/webchat.js +121 -43
  23. package/lib/hellotext.cjs +33 -3
  24. package/lib/hellotext.js +37 -6
  25. package/lib/models/business.cjs +71 -0
  26. package/lib/models/business.js +82 -0
  27. package/lib/models/webchat.cjs +25 -0
  28. package/lib/models/webchat.js +25 -0
  29. package/package.json +1 -1
  30. package/src/api/webchats.js +17 -0
  31. package/src/controllers/mixins/usePopover.js +4 -2
  32. package/src/controllers/webchat/emoji_picker_controller.js +51 -9
  33. package/src/controllers/webchat/useBehaviour.js +81 -0
  34. package/src/controllers/webchat/useOpeningSequence.js +127 -0
  35. package/src/controllers/webchat/useTeaser.js +192 -0
  36. package/src/controllers/webchat_controller.js +187 -23
  37. package/src/core/configuration/webchat.js +127 -43
  38. package/src/hellotext.js +38 -3
  39. package/src/models/business.js +76 -0
  40. package/src/models/webchat.js +28 -0
@@ -1,11 +1,83 @@
1
1
  import locales from '../locales'
2
+ import BusinessesAPI from '../api/businesses'
2
3
 
4
+ /**
5
+ * @typedef {Object} BusinessCountry
6
+ * @property {String} [code] - ISO country code configured for the business.
7
+ * @property {String} [prefix] - Phone country prefix configured for the business.
8
+ */
9
+
10
+ /**
11
+ * @typedef {Object} BusinessWebchat
12
+ * @property {String} [id] - Dashboard webchat id configured for the business.
13
+ * @property {Object} [appearance] - Dashboard appearance defaults for the webchat.
14
+ * @property {Object} [whatsapp] - Dashboard WhatsApp handoff defaults for the webchat.
15
+ */
16
+
17
+ /**
18
+ * Public business metadata returned by `public/businesses/:id`.
19
+ *
20
+ * @typedef {Object} BusinessData
21
+ * @property {String} [id] - Public business id.
22
+ * @property {BusinessCountry|String} [country] - Business country metadata.
23
+ * @property {Object} [features] - Feature flags enabled for the business.
24
+ * @property {String} [locale] - Default dashboard locale for the business.
25
+ * @property {String} [style_url] - Stylesheet URL to inject for dashboard-managed surfaces.
26
+ * @property {BusinessWebchat|null} [webchat] - Dashboard webchat defaults.
27
+ * @property {String|Array<String>} [whitelist] - Domain whitelist configuration.
28
+ * @property {String} [subscription] - Current business subscription tier.
29
+ */
30
+
31
+ /**
32
+ * Public business context used by the SDK for tracking, forms, and webchat defaults.
33
+ */
3
34
  class Business {
35
+ /**
36
+ * @param {String} id - Public business id.
37
+ */
4
38
  constructor(id) {
5
39
  this.id = id
6
40
  this.data = null
7
41
  }
8
42
 
43
+ /**
44
+ * Hydrates business metadata from the public business endpoint.
45
+ *
46
+ * Fetch failures return `null` so tracking initialization can continue even
47
+ * when dashboard-driven webchat defaults are unavailable.
48
+ *
49
+ * @returns {Promise<BusinessData|null>}
50
+ */
51
+ async hydrate() {
52
+ try {
53
+ const response = await BusinessesAPI.get(this.id)
54
+
55
+ if (response.ok === false) {
56
+ return null
57
+ }
58
+
59
+ const business = await response.json()
60
+
61
+ if (!business) {
62
+ return null
63
+ }
64
+
65
+ this.setData(business)
66
+
67
+ if (business.locale) {
68
+ this.setLocale(business.locale)
69
+ }
70
+
71
+ return business
72
+ } catch (_error) {
73
+ return null
74
+ }
75
+ }
76
+
77
+ /**
78
+ * @param {BusinessData} data
79
+ * @returns {void}
80
+ */
9
81
  setData(data) {
10
82
  this.data = data
11
83
 
@@ -30,6 +102,10 @@ class Business {
30
102
  return this.data.whitelist !== 'disabled'
31
103
  }
32
104
 
105
+ /**
106
+ * @param {String} locale
107
+ * @returns {void}
108
+ */
33
109
  setLocale(locale) {
34
110
  if (!locales[locale]) {
35
111
  return console.warn(`Locale ${locale} not found`)
@@ -16,9 +16,37 @@ class Webchat {
16
16
  }
17
17
 
18
18
  render() {
19
+ this.applyBehaviourOverride()
19
20
  this.containerToAppendTo.appendChild(this.data.html)
20
21
  }
21
22
 
23
+ applyBehaviourOverride() {
24
+ if (!Configuration.webchat.hasBehaviourOverride || !Configuration.webchat.behaviour) return
25
+
26
+ this.data.html.setAttribute(
27
+ 'data-hellotext--webchat-behaviour-value',
28
+ JSON.stringify(this.serializedBehaviour),
29
+ )
30
+ }
31
+
32
+ get serializedBehaviour() {
33
+ const behaviour = Configuration.webchat.behaviour
34
+
35
+ return {
36
+ trigger: this.serializeTrigger(behaviour.trigger),
37
+ delay_seconds: behaviour.delaySeconds,
38
+ first_visit_only: behaviour.firstVisitOnly,
39
+ once_per_session: behaviour.oncePerSession,
40
+ }
41
+ }
42
+
43
+ serializeTrigger(trigger) {
44
+ if (trigger === 'onLoad') return 'on_load'
45
+ if (trigger === 'onClick') return 'on_click'
46
+
47
+ return trigger
48
+ }
49
+
22
50
  get containerToAppendTo() {
23
51
  return document.querySelector(Configuration.webchat.container)
24
52
  }