UNPKG

@dynatrace/react-native-plugin

Version:

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

59 lines (58 loc) 2.47 kB
"use strict"; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.wrapOnValueChange = exports.Switch = void 0; const react_native_1 = require("react-native"); const React = require("react"); const ConfigurationHandler_1 = require("../../../lib/core/configuration/ConfigurationHandler"); const Dynatrace_1 = require("../../../lib/core/Dynatrace"); const ComponentUtil_1 = require("../../jsx/components/ComponentUtil"); exports.Switch = React.forwardRef((_a, ref) => { var { onValueChange } = _a, rest = __rest(_a, ["onValueChange"]); const shouldWrap = (0, ComponentUtil_1.isDynatraceIgnored)(rest) || !onValueChange; const wrappedOnValueChange = shouldWrap ? onValueChange : (0, exports.wrapOnValueChange)(onValueChange, rest); return (React.createElement(react_native_1.Switch, Object.assign({ ref: ref, onValueChange: wrappedOnValueChange }, rest))); }); const wrapOnValueChange = (onValueChange, switchProps) => (value) => { const switchName = findSwitchName(switchProps); const action = Dynatrace_1.Dynatrace.enterAutoAction(`Touch on ${switchName} to ${value}`); let isSync = true; try { const returnValue = onValueChange(value); if (returnValue instanceof Promise) { isSync = false; return Promise.resolve(returnValue).finally(() => { action.leaveAction(); }); } } finally { if (isSync) { action.leaveAction(); } } }; exports.wrapOnValueChange = wrapOnValueChange; const findSwitchName = (switchProps) => { var _a; if (switchProps.dtActionName) { return switchProps.dtActionName; } if (ConfigurationHandler_1.ConfigurationHandler.isConfigurationAvailable() && ConfigurationHandler_1.ConfigurationHandler.isActionNamePrivacyEnabled()) { return 'Switch'; } return (_a = switchProps.accessibilityLabel) !== null && _a !== void 0 ? _a : 'Switch'; };