import { NativeModules, Platform, NativeEventEmitter, EmitterSubscription } from "react-native"; import { isAndroid, isIos } from "./helpshiftUtils"; import { version } from "../package.json"; import * as helpshiftConstants from "./helpshiftConstants"; import HelpshiftIdentityLoginError from "./HelpshiftIdentityLoginError"; import HelpshiftEventData from "./HelpshiftEventData"; const LINKING_ERROR = `The package 'helpshift-plugin-sdkx-react-native' doesn't seem to be linked. Make sure: \n\n` + Platform.select({ ios: "- You have run 'pod install'\n", default: "" }) + "- You rebuilt the app after installing the package\n" + "- You are not using Expo Go\n"; // @ts-expect-error const isTurboModuleEnabled = global.__turboModuleProxy != null; const HelpshiftPluginSdkxReactNativeModule = isTurboModuleEnabled ? require("./NativeHelpshiftPluginSdkxReactNative").default : NativeModules.HelpshiftPluginSdkxReactNative; const Helpshift = HelpshiftPluginSdkxReactNativeModule ? HelpshiftPluginSdkxReactNativeModule : new Proxy( {}, { get() { throw new Error(LINKING_ERROR); } } ); export const helpshiftEmitter = new NativeEventEmitter(Helpshift); export const onEventOccurredListener = ( listener: (payload: HelpshiftEventData) => void ): EmitterSubscription => { return Helpshift.helpshiftEvents(listener); }; export const onUserAuthFailedListener = ( listener: (payload: HelpshiftEventData) => void ): EmitterSubscription => { return Helpshift.userAuthFailureEvent(listener); }; export const install = (platformId: string, domainId: string, config: object) => { const updatedConfig = { ...config, sdkType: "reactnativex", pluginVersion: version }; if (isAndroid || isIos) { Helpshift.install(platformId, domainId, updatedConfig); } }; export const showConversation = (config: object) => { if (isAndroid || isIos) { Helpshift.showConversation(config); } }; export const requestUnreadMessageCount = (shouldFetchFromServer: boolean) => { if (isAndroid || isIos) { Helpshift.requestUnreadMessageCount(shouldFetchFromServer); } }; export const showFAQsWithConfig = (config: object) => { if (isAndroid || isIos) { Helpshift.showFAQsWithConfig(config); } }; export const showFAQSectionWithConfig = (faqSectionPublishID: string, config: object) => { if (isAndroid || isIos) { Helpshift.showFAQSectionWithConfig(faqSectionPublishID, config); } }; export const showSingleFaqWithConfig = (faqPublishID: string, config: object) => { if (isAndroid || isIos) { Helpshift.showSingleFaqWithConfig(faqPublishID, config); } }; export const closeSDKSession = () => { if (isAndroid || isIos) { Helpshift.closeSession(); } }; export const sdkVersion = (): string => { if (isAndroid || isIos) { return version; } return ""; }; export const setLanguage = (languageCode: string) => { if (isAndroid || isIos) { Helpshift.setLanguage(languageCode); } }; // eslint-disable-next-line no-unused-vars export const login = (userDetailsDictionary: object, callback: (isLoggedIn: boolean) => void) => { if (isAndroid || isIos) { Helpshift.login(userDetailsDictionary, callback); } }; export const logout = () => { if (isAndroid || isIos) { Helpshift.logout(); } }; export const clearAnonymousUser = (shouldClear: boolean) => { if (isAndroid || isIos) { Helpshift.clearAnonymousUserOnLogin(shouldClear); } }; export const pauseDisplayOfInAppNotification = (shouldPauseInAppNotification: boolean) => { if (isAndroid || isIos) { Helpshift.pauseDisplayOfInAppNotification(shouldPauseInAppNotification); } }; export const clearBreadcrumbs = () => { if (isAndroid || isIos) { Helpshift.clearBreadcrumbs(); } }; export const leaveBreadCrumb = (breadCrumb: string) => { if (isAndroid || isIos) { Helpshift.leaveBreadCrumb(breadCrumb); } }; export const onAppBackground = () => { if (isAndroid) { Helpshift.onAppBackground(); } }; export const onAppForeground = () => { if (isAndroid) { Helpshift.onAppForeground(); } }; /** * @deprecated */ export const handleProactiveLink = (proactiveLink: string, localConfig: object) => { if (isAndroid || isIos) { Helpshift.handleProactive(proactiveLink, localConfig); } }; export const registerPushToken = (token: string) => { if (isAndroid || isIos) { Helpshift.registerPushToken(token); } }; export const handlePush = (notificationData: object, isAppLaunch: boolean) => { if (isAndroid || isIos) { Helpshift.handlePush(notificationData, isAppLaunch ?? false); } }; export const setProactiveConfig = (config: object) => { if (isAndroid || isIos) { Helpshift.setProactiveConfig(config); } }; export const handleIOSForegroundNotification = (notificationData: object): Promise<{ alert: boolean; sound: boolean; badge: boolean; list: boolean }> => { return new Promise((resolve) => { if (isIos) { Helpshift.handleIOSForegroundNotification(notificationData, (options: number) => { // `options` is a bitmask from iOS UNNotificationPresentationOptions: const presentationOptions = { badge: (options & 1) !== 0, // 1 = Badge → show app icon badge sound: (options & 2) !== 0, // 2 = Sound → play notification sound alert: (options & 4) !== 0 || (options & 16) !== 0, // 4 = Alert → show alert (iOS ≤ 13) list: (options & 8) !== 0 // 8 = List → show in Notification Center list (iOS 14+) }; resolve(presentationOptions); }); } else { resolve({ alert: false, sound: false, badge: false, list: false }); } }); }; export const handleIOSNotificationClick = (notificationData: object) => { if (isIos) { Helpshift.handleIOSNotificationClick(notificationData); } }; export const handleIOSSilentNotification = (notificationData: object): Promise<"newData" | "failed" | "noData"> => { return new Promise((resolve) => { if (isIos) { Helpshift.handleIOSSilentNotification(notificationData, (value: number) => { switch (value) { case 0: resolve("newData"); return; case 1: resolve("noData"); return; case 2: resolve("failed"); return; default: resolve("failed"); return; } }); } else { resolve("failed"); } }); }; export const addUserTrail = (userTrail: string) => { if (isAndroid || isIos) { Helpshift.addUserTrail(userTrail); } }; export const HelpshiftLog = { v(tag: string, msg: string) { if (isAndroid || isIos) { Helpshift.addDebugLog("Verbose", tag, msg); } }, d(tag: string, msg: string) { if (isAndroid || isIos) { Helpshift.addDebugLog("Debug", tag, msg); } }, i(tag: string, msg: string) { if (isAndroid || isIos) { Helpshift.addDebugLog("Info", tag, msg); } }, w(tag: string, msg: string) { if (isAndroid || isIos) { Helpshift.addDebugLog("Warn", tag, msg); } }, e(tag: string, msg: string) { if (isAndroid || isIos) { Helpshift.addDebugLog("Error", tag, msg); } } }; // User identity API's export const loginWithIdentity = async (jwt: string, config: object): Promise => { try { await Helpshift.loginWithIdentity(jwt, config); } catch (error: unknown) { const loginError = error as Record; if (loginError) { throw new HelpshiftIdentityLoginError({ reason: (loginError.message as string) ?? "unknownError", errors: (loginError.userInfo as Record) ?? {} }); } throw new HelpshiftIdentityLoginError({ reason: "unknownError", errors: {} }); } }; // Re-exporting helpshiftConstants to allow cleaner imports from the module export { helpshiftConstants }; export { HelpshiftIdentityLoginError }; export { HelpshiftEventData }; export const addUserIdentities = (identitiesJwt: string) => { if (isAndroid || isIos) { Helpshift.addUserIdentities(identitiesJwt); } }; export const updateMasterAttributes = (attributes: object) => { if (isAndroid || isIos) { Helpshift.updateMasterAttributes(attributes); } }; export const updateAppAttributes = (attributes: object) => { if (isAndroid || isIos) { Helpshift.updateAppAttributes(attributes); } }; export const setAndroidProactivePushNotificationDefaults = (config: object) => { if (isAndroid) { Helpshift.setAndroidProactivePushNotificationDefaults(config); } };