| 1 |
import { |
| 2 |
deviceId, |
| 3 |
hubspotBaseUrl, |
| 4 |
locale, |
| 5 |
portalId, |
| 6 |
} from '../constants/leadinConfig'; |
| 7 |
import { initApp } from './appUtils'; |
| 8 |
|
| 9 |
type CallbackFn = (...args: any[]) => void; |
| 10 |
|
| 11 |
export function initBackgroundApp(initFn: CallbackFn | CallbackFn[]) { |
| 12 |
function main() { |
| 13 |
if (Array.isArray(initFn)) { |
| 14 |
initFn.forEach(callback => callback()); |
| 15 |
} else { |
| 16 |
initFn(); |
| 17 |
} |
| 18 |
} |
| 19 |
initApp(main); |
| 20 |
} |
| 21 |
|
| 22 |
export const getOrCreateBackgroundApp = (refreshToken: string) => { |
| 23 |
if ((window as any).LeadinBackgroundApp) { |
| 24 |
return (window as any).LeadinBackgroundApp; |
| 25 |
} |
| 26 |
const { IntegratedAppEmbedder, IntegratedAppOptions }: any = window; |
| 27 |
const options = new IntegratedAppOptions() |
| 28 |
.setLocale(locale) |
| 29 |
.setDeviceId(deviceId) |
| 30 |
.setRefreshToken(refreshToken); |
| 31 |
|
| 32 |
const embedder = new IntegratedAppEmbedder( |
| 33 |
'integrated-plugin-proxy', |
| 34 |
portalId, |
| 35 |
hubspotBaseUrl, |
| 36 |
() => {} |
| 37 |
).setOptions(options); |
| 38 |
|
| 39 |
embedder.attachTo(document.body, false); |
| 40 |
embedder.postStartAppMessage(); // lets the app know all all data has been passed to it |
| 41 |
|
| 42 |
(window as any).LeadinBackgroundApp = embedder; |
| 43 |
return (window as any).LeadinBackgroundApp; |
| 44 |
}; |
| 45 |
|