UNPKG

@applicaster/zapp-react-dom-app

Version:

Zapp App Component for Applicaster's Quick Brick React Native App

120 lines (99 loc) 4.43 kB
import { NativeModules } from "react-native"; import uuidv4 from "uuid/v4"; import { isEmptyOrNil } from "@applicaster/zapp-react-native-utils/cellUtils"; import { appStore } from "@applicaster/zapp-react-native-redux/AppStore"; import { sessionStorage } from "@applicaster/zapp-react-native-bridge/ZappStorage/SessionStorage"; import { localStorage } from "@applicaster/zapp-react-native-bridge/ZappStorage/LocalStorage"; import { getDeviceData, getUserAgent, renameKeys } from "./utils/platform"; import { desiredKeysMap } from "./utils/const"; /** * Gets all of the plugins that you pass to it, or all of the plugins in the appStore * and loads them into session storage, if the value is null or undefined it will skip the key * If the key is malformed it will skip it as well */ export const loadPluginDataIntoSession = async (plugins = null) => { try { const pluginsToLoad = plugins || appStore.get("plugins"); if (Array.isArray(pluginsToLoad)) { pluginsToLoad.forEach((plugin) => { if (!plugin || !plugin.identifier || !plugin.configuration) return; Object.entries(plugin.configuration).forEach(([key, value]) => { if (isEmptyOrNil(key) || value === null || value === undefined) { return; } sessionStorage.setItem(key, value, plugin.identifier); }); }); } } catch (error) { // eslint-disable-next-line no-console console.error( "loadSessionStorageData: Error loading plugin data into session storage", error ); } }; /** * Gathers data from various sources including device data, Native Modules, * and session storage. Merges them and returns as a single object. * We specifically exclude layoutId because it is loaded in middleware * https://github.com/applicaster/QuickBrick/blob/main/packages/zapp-react-native-redux/middlewares/riversLoader.js#L24-L25 * @async * @param {string} uuid - The UUID to be set if not already present in local storage. * @returns {Promise<Object>} The gathered data object. * @throws Will throw an error if any of the data-gathering steps fail. */ async function gatherData(uuid) { const deviceData = await getDeviceData(); // Prevent the device sdkVersion from overriding the build time value delete deviceData.sdkVersion; const { languageLocale, countryLocale } = NativeModules?.QuickBrickCommunicationModule || {}; const sessionStorageData = { // Build time values ...NativeModules?.QuickBrickCommunicationModule, // Device data retrieved from native device apis ...deviceData, userAgent: deviceData.userAgent || getUserAgent(), uuid: uuid || uuidv4(), // Created on first session, persisted in local thereafter sessionId: uuidv4(), // Created on every session locale: `{language=${languageLocale}, country=${countryLocale}}`, }; return sessionStorageData; } /** * Stores the given data object into session storage. The stored keys are * determined by the provided `desiredKeys` map, which may rename the keys. * * @param {Object} data - The data object to be stored. * @param {Object} desiredKeys - The map of original key names to desired key names. */ function storeData(data, desiredKeys) { const desiredKeysList = Object.keys(desiredKeys); Object.entries(data).forEach(([key, value]) => { if (!desiredKeysList.includes(key)) return; if (isEmptyOrNil(value)) return; if (typeof value === "object" || typeof value === "function") return; const renamedKey = renameKeys(key); if (renamedKey === "uuid") { localStorage.setItem(renamedKey, value); } sessionStorage.setItem(renamedKey, value); }); } /** * This method gathers all of the info we can get from device, user agent, and the QB communication module * and loads them into session storage. You could pass an object with the keys you want and the value is how you want it to be named * or stored in session storage. If you don't pass anything it will use the desiredKeysMap from utils/const.js */ export async function loadSessionStorageData(desiredKeys = desiredKeysMap) { try { const uuid = await localStorage.getItem("uuid"); const data = await gatherData(uuid); storeData(data, desiredKeys); loadPluginDataIntoSession(); } catch (error) { // eslint-disable-next-line no-console console.error("Error loading data into session storage", error); } }