PluginProbe
HubSpot All-In-One Marketing – Forms, Popups, Live Chat / 11.0.31
HubSpot All-In-One Marketing – Forms, Popups, Live Chat v11.0.31
11.3.75 11.3.73 11.3.71 11.3.70 11.3.69 11.3.64 11.3.65 11.3.62 11.3.61 11.3.56 11.3.58 11.0.31 11.0.52 11.0.54 11.0.56 11.0.58 11.0.7 11.1.10 11.1.11 11.1.13 11.1.14 11.1.15 11.1.2 11.1.20 11.1.21 All 73 releases
leadin / scripts / iframe / useAppEmbedder.ts

useAppEmbedder.ts in HubSpot All-In-One Marketing – Forms, Popups, Live Chat 11.0.31, at scripts/iframe/useAppEmbedder.ts

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