PluginProbe
HubSpot All-In-One Marketing – Forms, Popups, Live Chat / 11.0.7
HubSpot All-In-One Marketing – Forms, Popups, Live Chat v11.0.7
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.7, at scripts/iframe/useAppEmbedder.ts

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