import * as IntercomTypes from "./types"; import { regionAPIs } from "./constants"; import { init, ref } from "./instance-manager"; const callIntercomMethod = (method: string, ...args: any[]) => { // window is undefined on server-side and this breaks our widget if (typeof window !== undefined && window.Intercom) { return window.Intercom(method, ...args); } else { console.warn("Please ensure Intercom is setup and running on client-side!"); } }; export const Intercom = (props: IntercomTypes.InitType) => { if (typeof props !== "object") { console.warn("Intercom initialiser called with invalid parameters."); return; } const { region = "us", widgetUrl, ...args } = props; if (typeof window !== "undefined" && !ref) { window.intercomSettings = { ...args, api_base: regionAPIs.get(region), }; init({ widgetUrl }); } }; // Public functions that can be called from outside the module export default Intercom; export const boot: (arg: IntercomTypes.IntercomSettings) => void = (arg: IntercomTypes.IntercomSettings) => callIntercomMethod("boot", arg); export const shutdown: () => void = () => callIntercomMethod("shutdown"); export const update: (arg: IntercomTypes.UserArgs) => void = (arg: IntercomTypes.UserArgs) => callIntercomMethod("update", arg); export const hide: () => void = () => callIntercomMethod("hide"); export const show: () => void = () => callIntercomMethod("show"); export const showSpace: (spaceName: string) => void = (spaceName: string) => callIntercomMethod("showSpace", spaceName); export const showMessages: () => void = () => callIntercomMethod("showMessages"); export const showNewMessage: (prePopulatedContent: string) => void = (prePopulatedContent: string) => callIntercomMethod("showNewMessage", prePopulatedContent); export const onHide: (callback: Function) => void = (callback: Function) => callIntercomMethod("onHide", callback); export const onShow: (callback: Function) => void = (callback: Function) => callIntercomMethod("onShow", callback); export const onUnreadCountChange: (callback: Function) => void = (callback: Function) => callIntercomMethod("onUnreadCountChange", callback); export const trackEvent: (...args: any) => void = (...args: any) => callIntercomMethod("trackEvent", ...args); export const getVisitorId: () => string | undefined = () => callIntercomMethod("getVisitorId"); export const whoami: () => Record | undefined = () => callIntercomMethod("whoami"); export const startTour: (tourId: string) => void = (tourId: string) => callIntercomMethod("startTour", tourId); export const showArticle: (articleId: string) => void = (articleId: string) => callIntercomMethod("showArticle", articleId); export const showNews: (newsItemId: string) => void = (newsItemId: string) => callIntercomMethod("showNews", newsItemId); export const startSurvey: (surveyId: string) => void = (surveyId: string) => callIntercomMethod("startSurvey", surveyId); export const startChecklist: (checklistId: string) => void = (checklistId: string) => callIntercomMethod("startChecklist", checklistId); export const showTicket: (ticketId: string) => void = (ticketId: string) => callIntercomMethod("showTicket", ticketId); export const showConversation: (conversationId: string) => void = (conversationId: string) => callIntercomMethod("showConversation", conversationId); export const onUserEmailSupplied: (callback: Function) => void = (callback: Function) => callIntercomMethod("onUserEmailSupplied", callback); export const hideNotifications: (hidden: boolean) => void = (hidden: boolean) => callIntercomMethod("hideNotifications", hidden); export const startConversation: (message: string) => void = (message: string) => callIntercomMethod("startConversation", message);