|
| 1 | +import { State } from '../types' |
| 2 | +import waitForLoad from '../utils/waitForLoad' |
| 3 | + |
| 4 | +export const domain = 'js.hs-scripts.com' |
| 5 | +declare global { |
| 6 | + interface Window { |
| 7 | + //eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 8 | + HubSpotConversations: any |
| 9 | + //eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 10 | + hsConversationsSettings: any |
| 11 | + //eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 12 | + hsConversationsOnReady: any |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +const loadScript = (hsId: string) => { |
| 17 | + // Detect the provider is already loaded and return early |
| 18 | + console.debug({ loadScript: window.HubSpotConversations }) |
| 19 | + if (window.HubSpotConversations) { |
| 20 | + return false |
| 21 | + } |
| 22 | + (function loadHubSpotSDK(d, s, id) { |
| 23 | + // fetch customerchat.js |
| 24 | + const fjs = d.getElementsByTagName(s)[0] |
| 25 | + if (d.getElementById(id)) { |
| 26 | + return |
| 27 | + } |
| 28 | + //eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 29 | + const js = d.createElement(s) as any |
| 30 | + js.id = id |
| 31 | + js.src = `https://${domain}/${hsId}.js` |
| 32 | + js.type = 'text/javascript' |
| 33 | + js.async = 1 |
| 34 | + js.defer = 1 |
| 35 | + if (fjs) { |
| 36 | + fjs.parentNode?.insertBefore(js, fjs) |
| 37 | + } else { |
| 38 | + d.body.appendChild(js) |
| 39 | + } |
| 40 | + })(window.document, 'script', 'hs-script-loader') |
| 41 | + |
| 42 | + return true |
| 43 | +} |
| 44 | + |
| 45 | +const load = ({ |
| 46 | + providerKey, |
| 47 | + setState, |
| 48 | + beforeInit = () => undefined, |
| 49 | + onReady = () => undefined, |
| 50 | +}: { |
| 51 | + providerKey: string |
| 52 | + setState: (state: State) => void |
| 53 | + beforeInit?: () => void |
| 54 | + onReady?: () => void |
| 55 | +}): boolean => { |
| 56 | + window.hsConversationsOnReady = [ |
| 57 | + () => { |
| 58 | + window.HubSpotConversations.widget.load() |
| 59 | + console.debug({ |
| 60 | + hsConversationsOnReady: 'ready', |
| 61 | + widgetStatus: window.HubSpotConversations.widget.status() |
| 62 | + }) |
| 63 | + } |
| 64 | + ] |
| 65 | + const loaded = loadScript(providerKey) |
| 66 | + if (loaded) { |
| 67 | + beforeInit() |
| 68 | + |
| 69 | + waitForLoad( |
| 70 | + () => { |
| 71 | + console.debug({ |
| 72 | + waitForLoad: { |
| 73 | + HubSpotConversations: window.HubSpotConversations, |
| 74 | + widget: window.HubSpotConversations?.widget |
| 75 | + } |
| 76 | + }) |
| 77 | + return Boolean( |
| 78 | + window.HubSpotConversations && |
| 79 | + window.HubSpotConversations.widget && |
| 80 | + window.HubSpotConversations.widget.status().loaded |
| 81 | + ) |
| 82 | + }, |
| 83 | + // Allow intercom to complete loading before removing fake widget |
| 84 | + () => { |
| 85 | + console.debug({ |
| 86 | + loadComplete: window.HubSpotConversations, |
| 87 | + widgetStatus: window.HubSpotConversations.widget.status() |
| 88 | + }) |
| 89 | + window.HubSpotConversations.widget.open() |
| 90 | + setState('complete') |
| 91 | + onReady() |
| 92 | + console.debug({ |
| 93 | + widgetStatusAfterTimeout: window.HubSpotConversations.widget.status() |
| 94 | + }) |
| 95 | + } |
| 96 | + ) |
| 97 | + } |
| 98 | + return loaded |
| 99 | +} |
| 100 | + |
| 101 | +const open = (): unknown => { |
| 102 | + console.debug({ open: 'called' }) |
| 103 | + window.HubSpotConversations && |
| 104 | + window.HubSpotConversations.widget && |
| 105 | + !window.HubSpotConversations.widget.status().loaded && |
| 106 | + window.HubSpotConversations.widget.load() |
| 107 | + |
| 108 | + return ( |
| 109 | + window.HubSpotConversations && |
| 110 | + window.HubSpotConversations.widget && |
| 111 | + window.HubSpotConversations.widget.open() |
| 112 | + ) |
| 113 | +} // Open provider |
| 114 | +const close = (): unknown => window.HubSpotConversations.widget.close() // Close provider |
| 115 | + |
| 116 | +export default { |
| 117 | + load, |
| 118 | + open, |
| 119 | + close, |
| 120 | + domain |
| 121 | +} |
0 commit comments