UNPKG

@dynatrace/react-native-plugin

Version:

This plugin gives you the ability to use the Dynatrace Mobile agent in your react native application.

63 lines (62 loc) 2.84 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.monitorNavigation = void 0; const react_1 = require("react"); const react_native_1 = require("react-native"); const ConsoleLogger_1 = require("../../../lib/core/logging/ConsoleLogger"); const DynatraceBridge_1 = require("../../../lib/core/DynatraceBridge"); const BaseDataEventModifier_1 = require("../../../lib/next/events/modifier/BaseDataEventModifier"); const logger = new ConsoleLogger_1.ConsoleLogger('ReactNavigation'); let activePath = ''; let appState = react_native_1.AppState.currentState; const monitorNavigation = (getRootState) => { (0, react_1.useEffect)(() => { var _a, _b; let currentState = getRootState(); let newPath = ''; while (currentState === null || currentState === void 0 ? void 0 : currentState.routes[(_a = currentState.index) !== null && _a !== void 0 ? _a : 0]) { const currentRoute = currentState === null || currentState === void 0 ? void 0 : currentState.routes[(_b = currentState.index) !== null && _b !== void 0 ? _b : 0]; newPath += `/${currentRoute.name}`; currentState = currentRoute.state; } if (newPath !== activePath) { if (activePath !== '') { stopView(); } startView(newPath); activePath = newPath; } }); (0, react_1.useEffect)(() => { const subscription = react_native_1.AppState.addEventListener('change', (nextAppState) => { if (nextAppState !== 'active' && appState === 'active') { stopView(); } else if (nextAppState === 'active' && appState !== 'active') { startView(activePath); } appState = nextAppState; }); return () => { subscription.remove(); }; }, []); }; exports.monitorNavigation = monitorNavigation; const startView = (newPath) => { const startViewInternalEvent = new BaseDataEventModifier_1.BaseDataEventModifier().modifyEvent(Object.assign({ 'view.detected_name': newPath }, (activePath && { 'view.source.detected_name': activePath, }))); const fn = DynatraceBridge_1.DynatraceNative === null || DynatraceBridge_1.DynatraceNative === void 0 ? void 0 : DynatraceBridge_1.DynatraceNative.startViewInternal; if (typeof fn === 'function') { fn(startViewInternalEvent); } logger.debug(`startViewinternal(${JSON.stringify(startViewInternalEvent)})`); }; const stopView = () => { const fn = DynatraceBridge_1.DynatraceNative === null || DynatraceBridge_1.DynatraceNative === void 0 ? void 0 : DynatraceBridge_1.DynatraceNative.stopViewInternal; if (typeof fn === 'function') { fn(); } logger.debug('stopViewInternal()'); };