UNPKG

react-native-webengage-inbox

Version:
355 lines (319 loc) 9.71 kB
import { NativeModules } from "react-native"; import { enableDevMode, weLogs } from "./utils/weLogs"; // Initialize WE Inbox module based on architecture function initializeWEInboxModule() { try { if (global.__turboModuleProxy) { // New Architecture - try TurboModule first try { const NativeWEInboxModule = require('./NativeWEInboxModule').default; return NativeWEInboxModule?.initWENotificationInbox ? NativeWEInboxModule : (NativeModules?.WEInboxReact || null); } catch (e) { weLogs.debug("TurboModule initialization failed:", e); // Fallback to legacy module return NativeModules?.WEInboxReact || null; } } // Legacy Architecture return NativeModules?.WEInboxReact || null; } catch (e) { weLogs.debug("Module initialization failed:", e); return null; } } const WEInboxReact = initializeWEInboxModule(); function notificationListResponse(result, error, resolve, reject) { try { weLogs.debug("notificationListResponse received result-", result); if (error) { weLogs.debug("Error in NotificationListResponse - ", error); reject(error); return; } let responseData = { messageList: [], hasNext: false }; if (result && typeof result === 'object') { responseData = { ...result }; const { messageList: messageString } = result; if (messageString && typeof messageString === 'string' && messageString.trim()) { try { const messageList = JSON.parse(messageString); responseData.messageList = Array.isArray(messageList) ? messageList : []; } catch (e) { weLogs.debug("Error parsing messageList JSON:", e); responseData.messageList = []; } } else { responseData.messageList = []; } } weLogs.debug("notificationList -", responseData); resolve(responseData); } catch (e) { weLogs.debug("Unexpected error in notificationListResponse:", e); reject(new Error("Failed to process notification list response")); } } /** * Initializes the WebEngage Notification Inbox */ function initWENotificationInbox() { try { if (WEInboxReact?.initWENotificationInbox) { WEInboxReact.initWENotificationInbox(); } else { weLogs.debug("WEInbox module not available for initialization"); } } catch (e) { weLogs.debug("Error initializing WENotificationInbox:", e); } } /** * Retrieves the list of notifications * @param {Object|null} offset - Optional notification item for pagination * @returns {Promise<Object>} Promise resolving to notification list response */ function getNotificationList(offset = null) { if (!WEInboxReact?.getNotificationList) { return Promise.reject(new Error("WEInbox module not available")); } try { let jsonOffset = null; if (offset) { try { jsonOffset = JSON.stringify(offset); } catch (e) { weLogs.debug("Error stringifying offset:", e); return Promise.reject(new Error("Invalid offset parameter")); } } weLogs.debug("JSON offset passed to Native- ", jsonOffset); return new Promise((resolve, reject) => { try { WEInboxReact.getNotificationList(jsonOffset, (result, error) => notificationListResponse(result, error, resolve, reject) ); } catch (e) { weLogs.debug("Error calling getNotificationList:", e); reject(new Error("Failed to retrieve notification list")); } }); } catch (e) { weLogs.debug("Unexpected error in getNotificationList:", e); return Promise.reject(new Error("Failed to retrieve notification list")); } } /** * Retrieves the notification count * @returns {Promise<string>} Promise resolving to notification count */ function getNotificationCount() { if (!WEInboxReact?.getNotificationCount) { return Promise.reject(new Error("WEInbox module not available")); } return new Promise((resolve, reject) => { try { WEInboxReact.getNotificationCount((result, error) => { if (error) { reject(error); } else { resolve(result); } }); } catch (e) { weLogs.debug("Error calling getNotificationCount:", e); reject(new Error("Failed to retrieve notification count")); } }); } /** * Marks a notification as read * @param {Object} notificationItem - The notification item to mark as read */ function markRead(notificationItem = {}) { if (!notificationItem || typeof notificationItem !== 'object' || Object.keys(notificationItem).length === 0) { weLogs.debug("No Notification Item passed to markRead"); return; } if (!WEInboxReact?.markRead) { weLogs.debug("WEInbox module not available for markRead"); return; } try { const { status = "" } = notificationItem; if (status?.toLowerCase() !== "read") { WEInboxReact.markRead(notificationItem); } else { weLogs.debug("markRead - status is already read"); } } catch (e) { weLogs.debug("Error in markRead:", e); } } /** * Marks a notification as unread * @param {Object} notificationItem - The notification item to mark as unread */ function markUnread(notificationItem = {}) { if (!notificationItem || typeof notificationItem !== 'object' || Object.keys(notificationItem).length === 0) { weLogs.debug("No Notification Item passed to markUnread"); return; } if (!WEInboxReact?.markUnread) { weLogs.debug("WEInbox module not available for markUnread"); return; } try { const { status = "" } = notificationItem; if (status?.toLowerCase() !== "unread") { WEInboxReact.markUnread(notificationItem); } else { weLogs.debug("markUnread - status is already Unread"); } } catch (e) { weLogs.debug("Error in markUnread:", e); } } /** * Tracks a click event on a notification * @param {Object} notificationItem - The notification item that was clicked */ function trackClick(notificationItem) { if (!notificationItem || typeof notificationItem !== 'object' || Object.keys(notificationItem).length === 0) { weLogs.debug("No Notification Item passed to trackClick"); return; } if (!WEInboxReact?.trackClick) { weLogs.debug("WEInbox module not available for trackClick"); return; } try { WEInboxReact.trackClick(notificationItem); } catch (e) { weLogs.debug("Error in trackClick:", e); } } /** * Tracks a view event for a notification * @param {Object} notificationItem - The notification item that was viewed */ function trackView(notificationItem) { if (!notificationItem || typeof notificationItem !== 'object' || Object.keys(notificationItem).length === 0) { weLogs.debug("No Notification Item passed to trackView"); return; } if (!WEInboxReact?.trackView) { weLogs.debug("WEInbox module not available for trackView"); return; } try { WEInboxReact.trackView(notificationItem); } catch (e) { weLogs.debug("Error in trackView:", e); } } /** * Marks a notification for deletion * @param {Object} notificationItem - The notification item to delete */ function markDelete(notificationItem) { if (!notificationItem || typeof notificationItem !== 'object' || Object.keys(notificationItem).length === 0) { weLogs.debug("No Notification Item passed to markDelete"); return; } if (!WEInboxReact?.markDelete) { weLogs.debug("WEInbox module not available for markDelete"); return; } try { WEInboxReact.markDelete(notificationItem); } catch (e) { weLogs.debug("Error in markDelete:", e); } } /** * Marks all notifications in the list as read * @param {Array} list - Array of notification items */ function readAll(list) { if (!Array.isArray(list) || list.length === 0) { weLogs.debug("readAll - list is empty or invalid"); return; } if (!WEInboxReact?.readAll) { weLogs.debug("WEInbox module not available for readAll"); return; } try { WEInboxReact.readAll(list); } catch (e) { weLogs.debug("Error in readAll:", e); } } /** * Marks all notifications in the list as unread * @param {Array} list - Array of notification items */ function unReadAll(list) { if (!Array.isArray(list) || list.length === 0) { weLogs.debug("unReadAll - list is empty or invalid"); return; } if (!WEInboxReact?.unReadAll) { weLogs.debug("WEInbox module not available for unReadAll"); return; } try { WEInboxReact.unReadAll(list); } catch (e) { weLogs.debug("Error in unReadAll:", e); } } /** * Deletes all notifications in the list * @param {Array} list - Array of notification items */ function deleteAll(list) { if (!Array.isArray(list) || list.length === 0) { weLogs.debug("deleteAll - list is empty or invalid"); return; } if (!WEInboxReact?.deleteAll) { weLogs.debug("WEInbox module not available for deleteAll"); return; } try { WEInboxReact.deleteAll(list); } catch (e) { weLogs.debug("Error in deleteAll:", e); } } /** * Resets the notification count when notification icon is clicked */ function resetNotificationCount() { if (!WEInboxReact?.onNotificationIconClick) { weLogs.debug("WEInbox module not available for resetNotificationCount"); return; } try { WEInboxReact.onNotificationIconClick(); } catch (e) { weLogs.debug("Error in resetNotificationCount:", e); } } export { getNotificationList, getNotificationCount, readAll, unReadAll, deleteAll, markRead, markUnread, markDelete, trackClick, trackView, resetNotificationCount, initWENotificationInbox, enableDevMode, };