| 1 |
import Penpal from 'penpal'; |
| 2 |
import Raven from './Raven'; |
| 3 |
import { syncRoute, leadinPageReload, leadinPageRedirect } from '../navigation'; |
| 4 |
import * as leadinConfig from '../constants/leadinConfig'; |
| 5 |
import { |
| 6 |
leadinClearQueryParam, |
| 7 |
getQueryParam, |
| 8 |
filterLeadinQueryParams, |
| 9 |
} from '../utils/queryParams'; |
| 10 |
import { leadinGetPortalInfo } from '../utils/portalInfo'; |
| 11 |
|
| 12 |
import { |
| 13 |
fetchOAuthToken, |
| 14 |
fetchRefreshToken, |
| 15 |
makeInterframeProxyRequest, |
| 16 |
disableInternalTracking, |
| 17 |
fetchDisableInternalTracking, |
| 18 |
updateHublet, |
| 19 |
trackConsent, |
| 20 |
skipReview, |
| 21 |
leadinDisconnectPortal, |
| 22 |
setBusinessUnitId, |
| 23 |
getBusinessUnitId, |
| 24 |
getBusinessUnits, |
| 25 |
} from '../api/wordpressApiClient'; |
| 26 |
import { setLeadinUnAuthedNavigation } from '../utils/sideNav'; |
| 27 |
import { gutenbergTriggerConnectCalendarRefresh } from '../gutenberg/MeetingsBlock/MeetingGutenbergInterframe'; |
| 28 |
|
| 29 |
const methods = { |
| 30 |
leadinClearQueryParam, |
| 31 |
leadinPageReload, |
| 32 |
leadinPageRedirect, |
| 33 |
leadinGetPortalInfo, |
| 34 |
leadinDisconnectPortal, |
| 35 |
getLeadinConfig: () => leadinConfig, |
| 36 |
setLeadinUnAuthedNavigation, |
| 37 |
makeInterframeProxyRequest, |
| 38 |
fetchOAuthToken, |
| 39 |
fetchRefreshToken, |
| 40 |
skipReview, |
| 41 |
updateHublet, |
| 42 |
trackConsent, |
| 43 |
disableInternalTracking, |
| 44 |
fetchDisableInternalTracking, |
| 45 |
gutenbergTriggerConnectCalendarRefresh, |
| 46 |
setBusinessUnitId, |
| 47 |
getBusinessUnitId, |
| 48 |
getBusinessUnits, |
| 49 |
}; |
| 50 |
|
| 51 |
const UNAUTHORIZED = 'unauthorized'; |
| 52 |
const REDIRECT = 'REDIRECT'; |
| 53 |
const hubspotBaseUrl = leadinConfig.hubspotBaseUrl; |
| 54 |
|
| 55 |
function createConnectionToIframe(iframe: HTMLIFrameElement) { |
| 56 |
return Penpal.connectToChild({ |
| 57 |
url: iframe.src, |
| 58 |
// The iframe to which a connection should be made |
| 59 |
iframe, |
| 60 |
// Methods the parent is exposing to the child |
| 61 |
methods, |
| 62 |
}); |
| 63 |
} |
| 64 |
|
| 65 |
export function initInterframe(iframe: HTMLIFrameElement) { |
| 66 |
//@ts-expect-error global |
| 67 |
if (!window.leadinChildFrameConnection) { |
| 68 |
//@ts-expect-error global |
| 69 |
window.leadinChildFrameConnection = createConnectionToIframe(iframe); |
| 70 |
//@ts-expect-error global |
| 71 |
window.leadinChildFrameConnection.promise.catch(error => { |
| 72 |
Raven.captureException(error, { |
| 73 |
fingerprint: ['INTERFRAME_CONNECTION_ERROR'], |
| 74 |
}); |
| 75 |
}); |
| 76 |
} |
| 77 |
|
| 78 |
const redirectToLogin = (event: any) => { |
| 79 |
if (event.data === UNAUTHORIZED) { |
| 80 |
window.removeEventListener('message', redirectToLogin); |
| 81 |
iframe.src = leadinConfig.loginUrl; |
| 82 |
setLeadinUnAuthedNavigation(); |
| 83 |
} |
| 84 |
}; |
| 85 |
|
| 86 |
const handleNavigation = (event: any) => { |
| 87 |
if (event.origin !== hubspotBaseUrl) return; |
| 88 |
try { |
| 89 |
const data = JSON.parse(event.data); |
| 90 |
if (data['leadin_sync_route']) { |
| 91 |
const route = data['leadin_sync_route']; |
| 92 |
const search = data['leadin_sync_search']; |
| 93 |
|
| 94 |
syncRoute(route, filterLeadinQueryParams(search)); |
| 95 |
} else if (data['message'] === REDIRECT) { |
| 96 |
window.location.href = data['url']; |
| 97 |
} |
| 98 |
} catch (e) { |
| 99 |
// Error in parsing message |
| 100 |
} |
| 101 |
}; |
| 102 |
|
| 103 |
const currentPage = getQueryParam('page'); |
| 104 |
if ( |
| 105 |
currentPage !== 'leadin_settings' && |
| 106 |
currentPage !== 'leadin' && |
| 107 |
currentPage !== 'leadin_user_guide' |
| 108 |
) { |
| 109 |
window.addEventListener('message', redirectToLogin); |
| 110 |
} |
| 111 |
|
| 112 |
window.addEventListener('message', handleNavigation); |
| 113 |
} |
| 114 |
|