PluginProbe
HubSpot All-In-One Marketing – Forms, Popups, Live Chat / 11.3.75
HubSpot All-In-One Marketing – Forms, Popups, Live Chat v11.3.75
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
← All changes | scripts/iframe/useAppEmbedder.ts +110 -13 11.0.3111.3.75 View file →
@@ -3,8 +3,9 @@
3 3
4 4 import {
5 5 accountName,
6 6 adminUrl,
7 + connectionStatus,
7 8 deviceId,
8 9 hubspotBaseUrl,
9 10 leadinQueryParams,
10 11 locale,
@@ -12,20 +13,83 @@
12 13 portalDomain,
13 14 portalEmail,
14 15 portalId,
15 16 reviewSkippedDate,
16 - refreshToken,
17 17 impactLink,
18 18 theme,
19 + lastAuthorizeTime,
20 + lastDeauthorizeTime,
21 + lastDisconnectTime,
19 22 leadinPluginVersion,
20 23 phpVersion,
21 24 wpVersion,
25 + contentEmbed,
26 + requiresContentEmbedScope,
27 + decryptError,
28 + LeadinConfig,
22 29 } from '../constants/leadinConfig';
30 +import { fetchAccessToken } from '../api/wordpressApiClient';
23 31 import { App, AppIframe } from './constants';
24 32 import { messageMiddleware } from './messageMiddleware';
25 33 import { resizeWindow, useIframeNotRendered } from '../utils/iframe';
26 34
27 -const getLeadinConfig = () => {
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 => {
28 92 const utm_query_params = Object.keys(leadinQueryParams)
29 93 .filter(x => /^utm/.test(x))
30 94 .reduce(
31 95 (p: { [key: string]: string }, c: string) => ({
@@ -38,8 +102,9 @@
38 102 accountName,
39 103 admin: leadinQueryParams.admin,
40 104 adminUrl,
41 105 company: leadinQueryParams.company,
106 + connectionStatus,
42 107 deviceId,
43 108 email: leadinQueryParams.email,
44 109 firstName: leadinQueryParams.firstName,
45 110 irclickid: leadinQueryParams.irclickid,
@@ -44,8 +109,11 @@
44 109 firstName: leadinQueryParams.firstName,
45 110 irclickid: leadinQueryParams.irclickid,
46 111 justConnected: leadinQueryParams.justConnected,
47 112 lastName: leadinQueryParams.lastName,
113 + lastAuthorizeTime,
114 + lastDeauthorizeTime,
115 + lastDisconnectTime,
48 116 leadinPluginVersion,
49 117 mpid: leadinQueryParams.mpid,
50 118 nonce: leadinQueryParams.nonce,
51 119 phpVersion,
@@ -57,8 +125,11 @@
57 125 theme,
58 126 trackConsent: leadinQueryParams.trackConsent,
59 127 websiteName: leadinQueryParams.websiteName,
60 128 wpVersion,
129 + contentEmbed,
130 + requiresContentEmbedScope,
131 + decryptError,
61 132 ...utm_query_params,
62 133 };
63 134 };
64 135
@@ -69,20 +140,19 @@
69 140 LiveChatAppOptions,
70 141 PluginAppOptions,
71 142 }: any = window;
72 143 let options;
73 -
74 144 switch (app) {
75 145 case App.Plugin:
76 - options = new PluginAppOptions().setLeadinConfig(getLeadinConfig());
146 + options = new PluginAppOptions();
77 147 break;
78 148 case App.PluginSettings:
79 - options = new PluginAppOptions()
80 - .setLeadinConfig(getLeadinConfig())
81 - .setPluginSettingsInit();
149 + options = new PluginAppOptions().setPluginSettingsInit();
82 150 break;
83 151 case App.Forms:
84 - options = new FormsAppOptions();
152 + options = new FormsAppOptions().setIntegratedAppConfig(
153 + getIntegrationConfig()
154 + );
85 155 if (createRoute) {
86 156 options = options.setCreateFormAppInit();
87 157 }
88 158 break;
@@ -113,13 +183,18 @@
113 183
114 184 useEffect(() => {
115 185 const { IntegratedAppEmbedder }: any = window;
116 186
117 - if (IntegratedAppEmbedder) {
187 + if (!IntegratedAppEmbedder) {
188 + return;
189 + }
190 +
191 + const createEmbedder = (accessToken = '', expiresIn = 0) => {
118 192 const options = getAppOptions(app, createRoute)
119 193 .setLocale(locale)
120 194 .setDeviceId(deviceId)
121 - .setRefreshToken(refreshToken);
195 + .setAccessToken(accessToken, expiresIn)
196 + .setLeadinConfig(getLeadinConfig());
122 197
123 198 const embedder = new IntegratedAppEmbedder(
124 199 AppIframe[app],
125 200 portalId,
@@ -124,16 +199,38 @@
124 199 AppIframe[app],
125 200 portalId,
126 201 hubspotBaseUrl,
127 202 resizeWindow,
128 - refreshToken ? '' : impactLink
203 + accessToken ? '' : impactLink
129 204 ).setOptions(options);
130 205
131 - embedder.subscribe(messageMiddleware(embedder));
206 + embedder.subscribe((message: any) => {
207 + messageMiddleware(embedder)(message);
208 + });
209 + embedder.setTokenRenewalCallback(fetchAccessToken);
210 +
132 211 embedder.attachTo(container, true);
133 212 embedder.postStartAppMessage(); // lets the app know all all data has been passed to it
134 213
135 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();
136 233 }
137 234 }, []);
138 235
139 236 if (iframeNotRendered) {
@@ -151,9 +248,9 @@
151 248 app,
152 249 hubspotBaseUrl,
153 250 impactLink,
154 251 appName: AppIframe[app],
155 - hasRefreshToken: !!refreshToken,
252 + isConnected: connectionStatus === 'Connected',
156 253 },
157 254 });
158 255 }
159 256