UNPKG

react-use-intercom

Version:

React Intercom integration without the hassle, driven by hooks.

1 lines 22.3 kB
{"version":3,"sources":["../src/provider.tsx","../src/logger.ts","../src/utils.ts","../src/api.ts","../src/context.ts","../src/initialize.ts","../src/mappers.ts","../src/useIntercom.ts"],"names":["React","log","level","message","packageName","isSSR","removeUndefined","obj","key","IntercomAPI","method","args","api_default","IntercomContext","context_default","initialize","appId","timeout","w","ic","d","i","l","s","x","initialize_default","mapMessengerAttributesToRawMessengerAttributes","attributes","mapDataAttributesCompanyToRawDataAttributesCompany","__spreadValues","mapDataAttributesAvatarToRawDataAttributesAvatar","mapDataAttributesToRawDataAttributes","_a","mapIntercomPropsToRawIntercomProps","props","IntercomProvider","_b","autoBoot","autoBootProps","children","onHide","onShow","onUnreadCountChange","onUserEmailSupplied","shouldInitialize","apiBase","initializeDelay","rest","__objRest","isBooted","isInitialized","invalidPropKeys","boot","metaData","isOpen","setIsOpen","onHideWrapper","onShowWrapper","ensureIntercom","functionName","callback","shutdown","hardShutdown","refresh","last_request_at","update","rawProps","hide","show","showMessages","showNewMessage","getVisitorId","startTour","tourId","startChecklist","checklistId","trackEvent","event","showArticle","articleId","showSpace","spaceName","startSurvey","surveyId","showNews","newsId","providerValue","useIntercomContext","context","useIntercom"],"mappings":"6fAAA,UAAYA,MAAW,QCQhB,IAAMC,EAAM,CAACC,EAAiBC,IAAoB,CACvD,IAAMC,EAAc,uBAEpB,OAAQF,EAAO,CACb,IAAK,OACH,QAAQ,IAAI,GAAGE,KAAeD,GAAS,EACvC,MACF,IAAK,OACH,QAAQ,KAAK,GAAGC,KAAeD,GAAS,EACxC,MACF,IAAK,QACH,QAAQ,MAAM,GAAGC,KAAeD,GAAS,EACzC,MACF,QACE,QAAQ,IAAI,GAAGC,KAAeD,GAAS,CAC3C,CACF,ECxBO,IAAME,EAAQ,OAAO,QAAW,YAO1BC,EAAmBC,IAC9B,OAAO,KAAKA,CAAG,EAAE,QAASC,GAAQ,CAC5BD,EAAIC,CAAG,G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AYyE,CAAM,CAChC,CAAC,EACH,CAAC3B,CAAc,CACjB,EAEM4B,GAAsB,UAA+B,KAClD,CACL,KAAAlC,EACA,SAAAS,EACA,aAAAC,EACA,OAAAG,EACA,KAAAE,EACA,KAAAC,EACA,OAAAd,EACA,aAAAe,EACA,eAAAC,EACA,aAAAC,EACA,UAAAC,EACA,eAAAE,EACA,WAAAE,EACA,YAAAE,EACA,YAAAI,EACA,UAAAF,EACA,SAAAI,CACF,GACC,CACDhC,EACAS,EACAC,EACAG,EACAE,EACAC,EACAd,EACAe,EACAC,EACAC,EACAC,EACAE,EACAE,EACAE,EACAI,EACAF,EACAI,CACF,CAAC,EAED,OACE,gBAACtE,EAAgB,SAAhB,CAAyB,MAAOwE,IAC9B/C,CACH,CAEJ,EAEagD,EAAqB,IAAM,CACtC,IAAMC,EAAgB,aAAW1E,CAAe,EAEhD,GAAI0E,IAAY,OACd,MAAM,IAAI,MAAM,uDAAuD,EAGzE,OAAOA,CACT,EO/TO,IAAMC,GAAc,IAClBF,EAAmB","sourcesContent":["import * as React from 'react';\n\nimport IntercomAPI from './api';\nimport IntercomContext from './context';\nimport initialize from './initialize';\nimport * as logger from './logger';\nimport { mapIntercomPropsToRawIntercomProps } from './mappers';\nimport {\n IntercomContextValues,\n IntercomProps,\n IntercomProviderProps,\n IntercomSpace,\n RawIntercomBootProps,\n} from './types';\nimport { isSSR } from './utils';\n\nexport const IntercomProvider: React.FC<\n React.PropsWithChildren<IntercomProviderProps>\n> = ({\n appId,\n autoBoot = false,\n autoBootProps,\n children,\n onHide,\n onShow,\n onUnreadCountChange,\n onUserEmailSupplied,\n shouldInitialize = !isSSR,\n apiBase,\n initializeDelay,\n ...rest\n}) => {\n const isBooted = React.useRef(false);\n const isInitialized = React.useRef(false);\n\n // Allow data-x attributes, see https://github.com/devrnt/react-use-intercom/issues/478\n const invalidPropKeys = Object.keys(rest).filter(\n (key) => !key.startsWith('data-'),\n );\n\n if (invalidPropKeys.length > 0) {\n logger.log(\n 'warn',\n [\n 'some invalid props were passed to IntercomProvider. ',\n `Please check following props: ${invalidPropKeys.join(', ')}.`,\n ].join(''),\n );\n }\n\n const boot = React.useCallback(\n (props?: IntercomProps) => {\n if (!window.Intercom && !shouldInitialize) {\n logger.log(\n 'warn',\n 'Intercom instance is not initialized because `shouldInitialize` is set to `false` in `IntercomProvider`',\n );\n return;\n }\n\n const metaData: RawIntercomBootProps = {\n app_id: appId,\n ...(apiBase && { api_base: apiBase }),\n ...(props && mapIntercomPropsToRawIntercomProps(props)),\n };\n\n window.intercomSettings = metaData;\n IntercomAPI('boot', metaData);\n isBooted.current = true;\n },\n [apiBase, appId, shouldInitialize],\n );\n\n const [isOpen, setIsOpen] = React.useState(false);\n\n const onHideWrapper = React.useCallback(() => {\n setIsOpen(false);\n if (onHide) onHide();\n }, [onHide, setIsOpen]);\n\n const onShowWrapper = React.useCallback(() => {\n setIsOpen(true);\n if (onShow) onShow();\n }, [onShow, setIsOpen]);\n\n if (!isSSR && shouldInitialize && !isInitialized.current) {\n initialize(appId, initializeDelay);\n\n // attach listeners\n IntercomAPI('onHide', onHideWrapper);\n IntercomAPI('onShow', onShowWrapper);\n IntercomAPI('onUserEmailSupplied', onUserEmailSupplied);\n\n if (onUnreadCountChange)\n IntercomAPI('onUnreadCountChange', onUnreadCountChange);\n\n if (autoBoot) {\n boot(autoBootProps);\n }\n\n isInitialized.current = true;\n }\n\n const ensureIntercom = React.useCallback(\n (functionName: string, callback: (() => void) | (() => string)) => {\n if (!window.Intercom && !shouldInitialize) {\n logger.log(\n 'warn',\n 'Intercom instance is not initialized because `shouldInitialize` is set to `false` in `IntercomProvider`',\n );\n return;\n }\n if (!isBooted.current) {\n logger.log(\n 'warn',\n [\n `\"${functionName}\" was called but Intercom has not booted yet. `,\n `Please call 'boot' before calling '${functionName}' or `,\n `set 'autoBoot' to true in the IntercomProvider.`,\n ].join(''),\n );\n return;\n }\n return callback();\n },\n [shouldInitialize],\n );\n\n const shutdown = React.useCallback(() => {\n if (!isBooted.current) return;\n\n IntercomAPI('shutdown');\n delete window.intercomSettings;\n isBooted.current = false;\n }, []);\n\n const hardShutdown = React.useCallback(() => {\n if (!isBooted.current) return;\n\n IntercomAPI('shutdown');\n delete window.Intercom;\n delete window.intercomSettings;\n isBooted.current = false;\n }, []);\n\n const refresh = React.useCallback(() => {\n ensureIntercom('update', () => {\n const last_request_at = Math.floor(new Date().getTime() / 1000);\n IntercomAPI('update', { last_request_at });\n });\n }, [ensureIntercom]);\n\n const update = React.useCallback(\n (props?: IntercomProps) => {\n ensureIntercom('update', () => {\n if (!props) {\n refresh();\n return;\n }\n const rawProps = mapIntercomPropsToRawIntercomProps(props);\n window.intercomSettings = { ...window.intercomSettings, ...rawProps };\n IntercomAPI('update', rawProps);\n });\n },\n [ensureIntercom, refresh],\n );\n\n const hide = React.useCallback(() => {\n ensureIntercom('hide', () => {\n IntercomAPI('hide');\n });\n }, [ensureIntercom]);\n\n const show = React.useCallback(() => {\n ensureIntercom('show', () => IntercomAPI('show'));\n }, [ensureIntercom]);\n\n const showMessages = React.useCallback(() => {\n ensureIntercom('showMessages', () => {\n IntercomAPI('showMessages');\n });\n }, [ensureIntercom]);\n\n const showNewMessage = React.useCallback(\n (message?: string) => {\n ensureIntercom('showNewMessage', () => {\n if (!message) {\n IntercomAPI('showNewMessage');\n } else {\n IntercomAPI('showNewMessage', message);\n }\n });\n },\n [ensureIntercom],\n );\n\n const getVisitorId = React.useCallback(() => {\n return ensureIntercom('getVisitorId', () => {\n return IntercomAPI('getVisitorId');\n }) as string;\n }, [ensureIntercom]);\n\n const startTour = React.useCallback(\n (tourId: number) => {\n ensureIntercom('startTour', () => {\n IntercomAPI('startTour', tourId);\n });\n },\n [ensureIntercom],\n );\n\n const startChecklist = React.useCallback(\n (checklistId: number) => {\n ensureIntercom('startChecklist', () => {\n IntercomAPI('startChecklist', checklistId);\n });\n },\n [ensureIntercom],\n );\n\n const trackEvent = React.useCallback(\n (event: string, metaData?: object) => {\n ensureIntercom('trackEvent', () => {\n if (metaData) {\n IntercomAPI('trackEvent', event, metaData);\n } else {\n IntercomAPI('trackEvent', event);\n }\n });\n },\n [ensureIntercom],\n );\n\n const showArticle = React.useCallback(\n (articleId: number) =>\n ensureIntercom('showArticle', () => {\n IntercomAPI('showArticle', articleId);\n }),\n [ensureIntercom],\n );\n\n const showSpace = React.useCallback(\n (spaceName: IntercomSpace) =>\n ensureIntercom('showSpace', () => {\n IntercomAPI('showSpace', spaceName);\n }),\n [ensureIntercom],\n );\n\n const startSurvey = React.useCallback(\n (surveyId: number) => {\n ensureIntercom('startSurvey', () => {\n IntercomAPI('startSurvey', surveyId);\n });\n },\n [ensureIntercom],\n );\n\n const showNews = React.useCallback(\n (newsId: number) =>\n ensureIntercom('showNews', () => {\n IntercomAPI('showNews', newsId);\n }),\n [ensureIntercom],\n );\n\n const providerValue = React.useMemo<IntercomContextValues>(() => {\n return {\n boot,\n shutdown,\n hardShutdown,\n update,\n hide,\n show,\n isOpen,\n showMessages,\n showNewMessage,\n getVisitorId,\n startTour,\n startChecklist,\n trackEvent,\n showArticle,\n startSurvey,\n showSpace,\n showNews,\n };\n }, [\n boot,\n shutdown,\n hardShutdown,\n update,\n hide,\n show,\n isOpen,\n showMessages,\n showNewMessage,\n getVisitorId,\n startTour,\n startChecklist,\n trackEvent,\n showArticle,\n startSurvey,\n showSpace,\n showNews,\n ]);\n\n return (\n <IntercomContext.Provider value={providerValue}>\n {children}\n </IntercomContext.Provider>\n );\n};\n\nexport const useIntercomContext = () => {\n const context = React.useContext(IntercomContext);\n\n if (context === undefined) {\n throw new Error('\"useIntercom\" must be used within `IntercomProvider`.');\n }\n\n return context as IntercomContextValues;\n};\n","import { LogLevel } from './types';\n\n/**\n * Logs messages in the console with a corresponding urgency\n *\n * @param level the urgency of the message\n * @param message the message to log in the console\n */\nexport const log = (level: LogLevel, message: string) => {\n const packageName = '[react-use-intercom]';\n\n switch (level) {\n case 'info':\n console.log(`${packageName} ${message}`);\n break;\n case 'warn':\n console.warn(`${packageName} ${message}`);\n break;\n case 'error':\n console.error(`${packageName} ${message}`);\n break;\n default:\n console.log(`${packageName} ${message}`);\n }\n};\n","export const isSSR = typeof window === 'undefined';\n\n/**\n * Removes object entries where the value equals to `undefined`\n *\n * @param obj\n */\nexport const removeUndefined = (obj: any) => {\n Object.keys(obj).forEach((key) => {\n if (obj[key] && typeof obj[key] === 'object') removeUndefined(obj[key]);\n else if (obj[key] === undefined) delete obj[key];\n });\n return obj;\n};\n","import * as logger from './logger';\nimport { IntercomMethod } from './types';\nimport { isSSR } from './utils';\n\n/**\n * Safely exposes `window.Intercom` and passes the arguments to the instance\n *\n * @param method method passed to the `window.Intercom` instance\n * @param args arguments passed to the `window.Intercom` instance\n *\n * @see {@link https://developers.intercom.com/installing-intercom/docs/intercom-javascript}\n */\nconst IntercomAPI = (method: IntercomMethod, ...args: Array<any>) => {\n if (!isSSR && window.Intercom) {\n return window.Intercom.apply(null, [method, ...args]);\n } else {\n logger.log('error', `${method} Intercom instance is not initalized yet`);\n }\n};\n\nexport default IntercomAPI;\n","import * as React from 'react';\n\nimport { IntercomContextValues } from './types';\n\nconst IntercomContext = React.createContext<IntercomContextValues | undefined>(\n undefined,\n);\n\nexport default IntercomContext;\n","// @ts-nocheck\n/**\n * Snippet to initialize the Intercom instance\n *\n * @param appId - Intercom app id\n * @param [timeout=0] - Amount of milliseconds that the initialization should be delayed, defaults to 0\n *\n * @see {@link https://developers.intercom.com/installing-intercom/docs/basic-javascript}\n */\nconst initialize = (appId: string, timeout = 0) => {\n var w = window;\n var ic = w.Intercom;\n if (typeof ic === 'function') {\n ic('reattach_activator');\n ic('update', w.intercomSettings);\n } else {\n var d = document;\n var i = function () {\n i.c(arguments);\n };\n i.q = [];\n i.c = function (args) {\n i.q.push(args);\n };\n w.Intercom = i;\n var l = function () {\n setTimeout(function () {\n var s = d.createElement('script');\n s.type = 'text/javascript';\n s.async = true;\n s.src = 'https://widget.intercom.io/widget/' + appId;\n var x = d.getElementsByTagName('script')[0];\n x.parentNode.insertBefore(s, x);\n }, timeout);\n };\n if (document.readyState === 'complete') {\n l();\n } else if (w.attachEvent) {\n w.attachEvent('onload', l);\n } else {\n w.addEventListener('load', l, false);\n }\n }\n};\n\nexport default initialize;\n","import {\n DataAttributes,\n DataAttributesAvatar,\n DataAttributesCompany,\n IntercomProps,\n MessengerAttributes,\n RawDataAttributes,\n RawDataAttributesAvatar,\n RawDataAttributesCompany,\n RawIntercomProps,\n RawMessengerAttributes,\n} from './types';\nimport { removeUndefined } from './utils';\n\nexport const mapMessengerAttributesToRawMessengerAttributes = (\n attributes: MessengerAttributes,\n): RawMessengerAttributes => ({\n custom_launcher_selector: attributes.customLauncherSelector,\n alignment: attributes.alignment,\n vertical_padding: attributes.verticalPadding,\n horizontal_padding: attributes.horizontalPadding,\n hide_default_launcher: attributes.hideDefaultLauncher,\n session_duration: attributes.sessionDuration,\n action_color: attributes.actionColor,\n background_color: attributes.backgroundColor,\n});\n\nconst mapDataAttributesCompanyToRawDataAttributesCompany = (\n attributes: DataAttributesCompany,\n): RawDataAttributesCompany => ({\n company_id: attributes.companyId,\n name: attributes.name,\n created_at: attributes.createdAt,\n plan: attributes.plan,\n monthly_spend: attributes.monthlySpend,\n user_count: attributes.userCount,\n size: attributes.size,\n website: attributes.website,\n industry: attributes.industry,\n ...attributes.customAttributes,\n});\n\nconst mapDataAttributesAvatarToRawDataAttributesAvatar = (\n attributes: DataAttributesAvatar,\n): RawDataAttributesAvatar => ({\n type: attributes.type,\n image_url: attributes.imageUrl,\n});\n\nexport const mapDataAttributesToRawDataAttributes = (\n attributes: DataAttributes,\n): RawDataAttributes => ({\n email: attributes.email,\n user_id: attributes.userId,\n created_at: attributes.createdAt,\n name: attributes.name,\n phone: attributes.phone,\n last_request_at: attributes.lastRequestAt,\n unsubscribed_from_emails: attributes.unsubscribedFromEmails,\n language_override: attributes.languageOverride,\n utm_campaign: attributes.utmCampaign,\n utm_content: attributes.utmContent,\n utm_medium: attributes.utmMedium,\n utm_source: attributes.utmSource,\n utm_term: attributes.utmTerm,\n avatar:\n attributes.avatar &&\n mapDataAttributesAvatarToRawDataAttributesAvatar(attributes.avatar),\n user_hash: attributes.userHash,\n company:\n attributes.company &&\n mapDataAttributesCompanyToRawDataAttributesCompany(attributes.company),\n companies: attributes.companies?.map(\n mapDataAttributesCompanyToRawDataAttributesCompany,\n ),\n ...attributes.customAttributes,\n});\n\nexport const mapIntercomPropsToRawIntercomProps = (\n props: IntercomProps,\n): RawIntercomProps => {\n return removeUndefined({\n ...mapMessengerAttributesToRawMessengerAttributes(props),\n ...mapDataAttributesToRawDataAttributes(props),\n });\n};\n","import { useIntercomContext } from './provider';\n\nexport const useIntercom = () => {\n return useIntercomContext();\n};\n"]}