|
| 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 isHubspotWidgetDefined = () => window.HubSpotConversations && window.HubSpotConversations.widget; |
| 17 | + |
| 18 | +const loadScript = (hsId: string) => { |
| 19 | + // Detect the provider is already loaded and return early |
| 20 | + if (window.HubSpotConversations) { |
| 21 | + return false |
| 22 | + } |
| 23 | + (function loadHubSpotSDK(d, s, id) { |
| 24 | + // fetch customerchat.js |
| 25 | + const fjs = d.getElementsByTagName(s)[0] |
| 26 | + if (d.getElementById(id)) { |
| 27 | + return |
| 28 | + } |
| 29 | + //eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 30 | + const js = d.createElement(s) as any |
| 31 | + js.id = id |
| 32 | + js.src = `https://${domain}/${hsId}.js` |
| 33 | + js.type = 'text/javascript' |
| 34 | + js.async = 1 |
| 35 | + js.defer = 1 |
| 36 | + if (fjs) { |
| 37 | + fjs.parentNode?.insertBefore(js, fjs) |
| 38 | + } else { |
| 39 | + d.body.appendChild(js) |
| 40 | + } |
| 41 | + })(window.document, 'script', 'hs-script-loader') |
| 42 | + |
| 43 | + return true |
| 44 | +} |
| 45 | + |
| 46 | +const load = ({ |
| 47 | + providerKey, |
| 48 | + setState, |
| 49 | + beforeInit = () => undefined, |
| 50 | + onReady = () => undefined, |
| 51 | +}: { |
| 52 | + providerKey: string |
| 53 | + setState: (state: State) => void |
| 54 | + beforeInit?: () => void |
| 55 | + onReady?: () => void |
| 56 | +}): boolean => { |
| 57 | + window.hsConversationsOnReady = [ |
| 58 | + () => { |
| 59 | + isHubspotWidgetDefined() && window.HubSpotConversations.widget.load() |
| 60 | + } |
| 61 | + ] |
| 62 | + const loaded = loadScript(providerKey) |
| 63 | + if (loaded) { |
| 64 | + beforeInit() |
| 65 | + |
| 66 | + waitForLoad( |
| 67 | + () => { |
| 68 | + return Boolean( |
| 69 | + isHubspotWidgetDefined() && |
| 70 | + window.HubSpotConversations.widget.status().loaded |
| 71 | + ) |
| 72 | + }, |
| 73 | + // Allow hubspot to complete loading before removing fake widget |
| 74 | + () => { |
| 75 | + isHubspotWidgetDefined() && window.HubSpotConversations.widget.open() |
| 76 | + setState('complete') |
| 77 | + onReady() |
| 78 | + } |
| 79 | + ) |
| 80 | + } |
| 81 | + return loaded |
| 82 | +} |
| 83 | + |
| 84 | +const open = (): unknown => { |
| 85 | + isHubspotWidgetDefined() && |
| 86 | + !window.HubSpotConversations.widget.status().loaded && |
| 87 | + window.HubSpotConversations.widget.load() |
| 88 | + |
| 89 | + return ( |
| 90 | + isHubspotWidgetDefined() && |
| 91 | + window.HubSpotConversations.widget.open() |
| 92 | + ) |
| 93 | +} // Open provider |
| 94 | +const close = (): unknown => isHubspotWidgetDefined() && window.HubSpotConversations.widget.close() // Close provider |
| 95 | + |
| 96 | +export default { |
| 97 | + load, |
| 98 | + open, |
| 99 | + close, |
| 100 | + domain |
| 101 | +} |
0 commit comments