| 1 |
import { useEffect } from 'react'; |
| 2 |
import Raven from '../lib/Raven'; |
| 3 |
|
| 4 |
import { |
| 5 |
accountName, |
| 6 |
adminUrl, |
| 7 |
connectionStatus, |
| 8 |
deviceId, |
| 9 |
hubspotBaseUrl, |
| 10 |
leadinQueryParams, |
| 11 |
locale, |
| 12 |
plugins, |
| 13 |
portalDomain, |
| 14 |
portalEmail, |
| 15 |
portalId, |
| 16 |
reviewSkippedDate, |
| 17 |
impactLink, |
| 18 |
theme, |
| 19 |
lastAuthorizeTime, |
| 20 |
lastDeauthorizeTime, |
| 21 |
lastDisconnectTime, |
| 22 |
leadinPluginVersion, |
| 23 |
phpVersion, |
| 24 |
wpVersion, |
| 25 |
contentEmbed, |
| 26 |
requiresContentEmbedScope, |
| 27 |
decryptError, |
| 28 |
LeadinConfig, |
| 29 |
} from '../constants/leadinConfig'; |
| 30 |
import { fetchAccessToken } from '../api/wordpressApiClient'; |
| 31 |
import { App, AppIframe } from './constants'; |
| 32 |
import { messageMiddleware } from './messageMiddleware'; |
| 33 |
import { resizeWindow, useIframeNotRendered } from '../utils/iframe'; |
| 34 |
|
| 35 |
type PartialLeadinConfig = Pick< |
| 36 |
LeadinConfig, |
| 37 |
| 'accountName' |
| 38 |
| 'adminUrl' |
| 39 |
| 'connectionStatus' |
| 40 |
| 'deviceId' |
| 41 |
| 'plugins' |
| 42 |
| 'portalDomain' |
| 43 |
| 'portalEmail' |
| 44 |
| 'portalId' |
| 45 |
| 'reviewSkippedDate' |
| 46 |
| 'refreshToken' |
| 47 |
| 'impactLink' |
| 48 |
| 'theme' |
| 49 |
| 'trackConsent' |
| 50 |
| 'lastAuthorizeTime' |
| 51 |
| 'lastDeauthorizeTime' |
| 52 |
| 'lastDisconnectTime' |
| 53 |
| 'leadinPluginVersion' |
| 54 |
| 'phpVersion' |
| 55 |
| 'wpVersion' |
| 56 |
| 'contentEmbed' |
| 57 |
| 'requiresContentEmbedScope' |
| 58 |
| 'decryptError' |
| 59 |
>; |
| 60 |
|
| 61 |
type AppIntegrationConfig = Pick<LeadinConfig, 'adminUrl'>; |
| 62 |
|
| 63 |
const getIntegrationConfig = (): AppIntegrationConfig => { |
| 64 |
return { |
| 65 |
adminUrl: leadinQueryParams.adminUrl, |
| 66 |
}; |
| 67 |
}; |
| 68 |
|
| 69 |
/** |
| 70 |
* A modified version of the original leadinConfig that is passed to some integrated apps. |
| 71 |
* |
| 72 |
* Important: |
| 73 |
* Try not to add new fields here. |
| 74 |
* This config is already too large and broad in scope. |
| 75 |
* It tightly couples the apps that use it with the WordPress plugin. |
| 76 |
* Consider instead passing new required fields as new entry to PluginAppOptions or app-specific options. |
| 77 |
*/ |
| 78 |
type AppLeadinConfig = { |
| 79 |
admin: string; |
| 80 |
company: string; |
| 81 |
email: string; |
| 82 |
firstName: string; |
| 83 |
irclickid: string; |
| 84 |
justConnected: string; |
| 85 |
lastName: string; |
| 86 |
mpid: string; |
| 87 |
nonce: string; |
| 88 |
websiteName: string; |
| 89 |
} & PartialLeadinConfig; |
| 90 |
|
| 91 |
const getLeadinConfig = (): AppLeadinConfig => { |
| 92 |
const utm_query_params = Object.keys(leadinQueryParams) |
| 93 |
.filter(x => /^utm/.test(x)) |
| 94 |
.reduce( |
| 95 |
(p: { [key: string]: string }, c: string) => ({ |
| 96 |
[c]: leadinQueryParams[c], |
| 97 |
...p, |
| 98 |
}), |
| 99 |
{} |
| 100 |
); |
| 101 |
return { |
| 102 |
accountName, |
| 103 |
admin: leadinQueryParams.admin, |
| 104 |
adminUrl, |
| 105 |
company: leadinQueryParams.company, |
| 106 |
connectionStatus, |
| 107 |
deviceId, |
| 108 |
email: leadinQueryParams.email, |
| 109 |
firstName: leadinQueryParams.firstName, |
| 110 |
irclickid: leadinQueryParams.irclickid, |
| 111 |
justConnected: leadinQueryParams.justConnected, |
| 112 |
lastName: leadinQueryParams.lastName, |
| 113 |
lastAuthorizeTime, |
| 114 |
lastDeauthorizeTime, |
| 115 |
lastDisconnectTime, |
| 116 |
leadinPluginVersion, |
| 117 |
mpid: leadinQueryParams.mpid, |
| 118 |
nonce: leadinQueryParams.nonce, |
| 119 |
phpVersion, |
| 120 |
plugins, |
| 121 |
portalDomain, |
| 122 |
portalEmail, |
| 123 |
portalId, |
| 124 |
reviewSkippedDate, |
| 125 |
theme, |
| 126 |
trackConsent: leadinQueryParams.trackConsent, |
| 127 |
websiteName: leadinQueryParams.websiteName, |
| 128 |
wpVersion, |
| 129 |
contentEmbed, |
| 130 |
requiresContentEmbedScope, |
| 131 |
decryptError, |
| 132 |
...utm_query_params, |
| 133 |
}; |
| 134 |
}; |
| 135 |
|
| 136 |
const getAppOptions = (app: App, createRoute = false) => { |
| 137 |
const { |
| 138 |
IntegratedAppOptions, |
| 139 |
FormsAppOptions, |
| 140 |
LiveChatAppOptions, |
| 141 |
PluginAppOptions, |
| 142 |
}: any = window; |
| 143 |
let options; |
| 144 |
switch (app) { |
| 145 |
case App.Plugin: |
| 146 |
options = new PluginAppOptions(); |
| 147 |
break; |
| 148 |
case App.PluginSettings: |
| 149 |
options = new PluginAppOptions().setPluginSettingsInit(); |
| 150 |
break; |
| 151 |
case App.Forms: |
| 152 |
options = new FormsAppOptions().setIntegratedAppConfig( |
| 153 |
getIntegrationConfig() |
| 154 |
); |
| 155 |
if (createRoute) { |
| 156 |
options = options.setCreateFormAppInit(); |
| 157 |
} |
| 158 |
break; |
| 159 |
case App.LiveChat: |
| 160 |
options = new LiveChatAppOptions(); |
| 161 |
if (createRoute) { |
| 162 |
options = options.setCreateLiveChatAppInit(); |
| 163 |
} |
| 164 |
break; |
| 165 |
default: |
| 166 |
options = new IntegratedAppOptions(); |
| 167 |
} |
| 168 |
|
| 169 |
return options; |
| 170 |
}; |
| 171 |
|
| 172 |
export default function useAppEmbedder( |
| 173 |
app: App, |
| 174 |
createRoute: boolean, |
| 175 |
container: HTMLElement | null |
| 176 |
) { |
| 177 |
console.info( |
| 178 |
'HubSpot plugin - starting app embedder for:', |
| 179 |
AppIframe[app], |
| 180 |
container |
| 181 |
); |
| 182 |
const iframeNotRendered = useIframeNotRendered(AppIframe[app]); |
| 183 |
|
| 184 |
useEffect(() => { |
| 185 |
const { IntegratedAppEmbedder }: any = window; |
| 186 |
|
| 187 |
if (!IntegratedAppEmbedder) { |
| 188 |
return; |
| 189 |
} |
| 190 |
|
| 191 |
const createEmbedder = (accessToken = '', expiresIn = 0) => { |
| 192 |
const options = getAppOptions(app, createRoute) |
| 193 |
.setLocale(locale) |
| 194 |
.setDeviceId(deviceId) |
| 195 |
.setAccessToken(accessToken, expiresIn) |
| 196 |
.setLeadinConfig(getLeadinConfig()); |
| 197 |
|
| 198 |
const embedder = new IntegratedAppEmbedder( |
| 199 |
AppIframe[app], |
| 200 |
portalId, |
| 201 |
hubspotBaseUrl, |
| 202 |
resizeWindow, |
| 203 |
accessToken ? '' : impactLink |
| 204 |
).setOptions(options); |
| 205 |
|
| 206 |
embedder.subscribe((message: any) => { |
| 207 |
messageMiddleware(embedder)(message); |
| 208 |
}); |
| 209 |
embedder.setTokenRenewalCallback(fetchAccessToken); |
| 210 |
|
| 211 |
embedder.attachTo(container, true); |
| 212 |
embedder.postStartAppMessage(); // lets the app know all all data has been passed to it |
| 213 |
|
| 214 |
(window as any).embedder = embedder; |
| 215 |
}; |
| 216 |
|
| 217 |
if (connectionStatus === 'Connected') { |
| 218 |
fetchAccessToken() |
| 219 |
.then( |
| 220 |
({ |
| 221 |
accessToken, |
| 222 |
expiresIn, |
| 223 |
}: { |
| 224 |
accessToken: string; |
| 225 |
expiresIn: number; |
| 226 |
}) => { |
| 227 |
createEmbedder(accessToken, expiresIn); |
| 228 |
} |
| 229 |
) |
| 230 |
.catch(() => {}); |
| 231 |
} else { |
| 232 |
createEmbedder(); |
| 233 |
} |
| 234 |
}, []); |
| 235 |
|
| 236 |
if (iframeNotRendered) { |
| 237 |
console.error('HubSpot plugin Iframe not rendered', { |
| 238 |
portalId, |
| 239 |
container, |
| 240 |
appName: AppIframe[app], |
| 241 |
hasIntegratedAppEmbedder: !!(window as any).IntegratedAppEmbedder, |
| 242 |
}); |
| 243 |
Raven.captureException(new Error('Leadin Iframe not rendered'), { |
| 244 |
fingerprint: ['USE_APP_EMBEDDER', 'IFRAME_SETUP_ERROR'], |
| 245 |
extra: { |
| 246 |
portalId, |
| 247 |
container, |
| 248 |
app, |
| 249 |
hubspotBaseUrl, |
| 250 |
impactLink, |
| 251 |
appName: AppIframe[app], |
| 252 |
isConnected: connectionStatus === 'Connected', |
| 253 |
}, |
| 254 |
}); |
| 255 |
} |
| 256 |
|
| 257 |
return iframeNotRendered; |
| 258 |
} |
| 259 |
|