| 1 |
import { useEffect } from 'react'; |
| 2 |
import Raven from '../lib/Raven'; |
| 3 |
|
| 4 |
import { |
| 5 |
accountName, |
| 6 |
adminUrl, |
| 7 |
deviceId, |
| 8 |
hubspotBaseUrl, |
| 9 |
leadinQueryParams, |
| 10 |
locale, |
| 11 |
plugins, |
| 12 |
portalDomain, |
| 13 |
portalEmail, |
| 14 |
portalId, |
| 15 |
reviewSkippedDate, |
| 16 |
refreshToken, |
| 17 |
impactLink, |
| 18 |
theme, |
| 19 |
leadinPluginVersion, |
| 20 |
phpVersion, |
| 21 |
wpVersion, |
| 22 |
contentEmbed, |
| 23 |
requiresContentEmbedScope, |
| 24 |
} from '../constants/leadinConfig'; |
| 25 |
import { App, AppIframe } from './constants'; |
| 26 |
import { messageMiddleware } from './messageMiddleware'; |
| 27 |
import { resizeWindow, useIframeNotRendered } from '../utils/iframe'; |
| 28 |
|
| 29 |
const getLeadinConfig = () => { |
| 30 |
const utm_query_params = Object.keys(leadinQueryParams) |
| 31 |
.filter(x => /^utm/.test(x)) |
| 32 |
.reduce( |
| 33 |
(p: { [key: string]: string }, c: string) => ({ |
| 34 |
[c]: leadinQueryParams[c], |
| 35 |
...p, |
| 36 |
}), |
| 37 |
{} |
| 38 |
); |
| 39 |
return { |
| 40 |
accountName, |
| 41 |
admin: leadinQueryParams.admin, |
| 42 |
adminUrl, |
| 43 |
company: leadinQueryParams.company, |
| 44 |
deviceId, |
| 45 |
email: leadinQueryParams.email, |
| 46 |
firstName: leadinQueryParams.firstName, |
| 47 |
irclickid: leadinQueryParams.irclickid, |
| 48 |
justConnected: leadinQueryParams.justConnected, |
| 49 |
lastName: leadinQueryParams.lastName, |
| 50 |
leadinPluginVersion, |
| 51 |
mpid: leadinQueryParams.mpid, |
| 52 |
nonce: leadinQueryParams.nonce, |
| 53 |
phpVersion, |
| 54 |
plugins, |
| 55 |
portalDomain, |
| 56 |
portalEmail, |
| 57 |
portalId, |
| 58 |
reviewSkippedDate, |
| 59 |
theme, |
| 60 |
trackConsent: leadinQueryParams.trackConsent, |
| 61 |
websiteName: leadinQueryParams.websiteName, |
| 62 |
wpVersion, |
| 63 |
contentEmbed, |
| 64 |
requiresContentEmbedScope, |
| 65 |
...utm_query_params, |
| 66 |
}; |
| 67 |
}; |
| 68 |
|
| 69 |
const getAppOptions = (app: App, createRoute = false) => { |
| 70 |
const { |
| 71 |
IntegratedAppOptions, |
| 72 |
FormsAppOptions, |
| 73 |
LiveChatAppOptions, |
| 74 |
PluginAppOptions, |
| 75 |
}: any = window; |
| 76 |
let options; |
| 77 |
switch (app) { |
| 78 |
case App.Plugin: |
| 79 |
options = new PluginAppOptions().setLeadinConfig(getLeadinConfig()); |
| 80 |
break; |
| 81 |
case App.PluginSettings: |
| 82 |
options = new PluginAppOptions() |
| 83 |
.setLeadinConfig(getLeadinConfig()) |
| 84 |
.setPluginSettingsInit(); |
| 85 |
break; |
| 86 |
case App.Forms: |
| 87 |
options = new FormsAppOptions(); |
| 88 |
if (createRoute) { |
| 89 |
options = options.setCreateFormAppInit(); |
| 90 |
} |
| 91 |
break; |
| 92 |
case App.LiveChat: |
| 93 |
options = new LiveChatAppOptions(); |
| 94 |
if (createRoute) { |
| 95 |
options = options.setCreateLiveChatAppInit(); |
| 96 |
} |
| 97 |
break; |
| 98 |
default: |
| 99 |
options = new IntegratedAppOptions(); |
| 100 |
} |
| 101 |
|
| 102 |
return options; |
| 103 |
}; |
| 104 |
|
| 105 |
export default function useAppEmbedder( |
| 106 |
app: App, |
| 107 |
createRoute: boolean, |
| 108 |
container: HTMLElement | null |
| 109 |
) { |
| 110 |
console.info( |
| 111 |
'HubSpot plugin - starting app embedder for:', |
| 112 |
AppIframe[app], |
| 113 |
container |
| 114 |
); |
| 115 |
const iframeNotRendered = useIframeNotRendered(AppIframe[app]); |
| 116 |
|
| 117 |
useEffect(() => { |
| 118 |
const { IntegratedAppEmbedder }: any = window; |
| 119 |
|
| 120 |
if (IntegratedAppEmbedder) { |
| 121 |
const options = getAppOptions(app, createRoute) |
| 122 |
.setLocale(locale) |
| 123 |
.setDeviceId(deviceId) |
| 124 |
.setRefreshToken(refreshToken); |
| 125 |
|
| 126 |
const embedder = new IntegratedAppEmbedder( |
| 127 |
AppIframe[app], |
| 128 |
portalId, |
| 129 |
hubspotBaseUrl, |
| 130 |
resizeWindow, |
| 131 |
refreshToken ? '' : impactLink |
| 132 |
).setOptions(options); |
| 133 |
|
| 134 |
embedder.subscribe(messageMiddleware(embedder)); |
| 135 |
embedder.attachTo(container, true); |
| 136 |
embedder.postStartAppMessage(); // lets the app know all all data has been passed to it |
| 137 |
|
| 138 |
(window as any).embedder = embedder; |
| 139 |
} |
| 140 |
}, []); |
| 141 |
|
| 142 |
if (iframeNotRendered) { |
| 143 |
console.error('HubSpot plugin Iframe not rendered', { |
| 144 |
portalId, |
| 145 |
container, |
| 146 |
appName: AppIframe[app], |
| 147 |
hasIntegratedAppEmbedder: !!(window as any).IntegratedAppEmbedder, |
| 148 |
}); |
| 149 |
Raven.captureException(new Error('Leadin Iframe not rendered'), { |
| 150 |
fingerprint: ['USE_APP_EMBEDDER', 'IFRAME_SETUP_ERROR'], |
| 151 |
extra: { |
| 152 |
portalId, |
| 153 |
container, |
| 154 |
app, |
| 155 |
hubspotBaseUrl, |
| 156 |
impactLink, |
| 157 |
appName: AppIframe[app], |
| 158 |
hasRefreshToken: !!refreshToken, |
| 159 |
}, |
| 160 |
}); |
| 161 |
} |
| 162 |
|
| 163 |
return iframeNotRendered; |
| 164 |
} |
| 165 |
|