@dynatrace/react-native-plugin
Version:
This plugin gives you the ability to use the Dynatrace Mobile agent in your react native application.
152 lines (151 loc) • 6.02 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TouchableHelper = void 0;
const ConfigurationHandler_1 = require("../../../lib/core/configuration/ConfigurationHandler");
const IDynatraceProperties_1 = require("../IDynatraceProperties");
const TypesUtil_1 = require("../../model/TypesUtil");
const TouchableHelper = (Dynatrace, Logger) => ({
attachOnPress(longPress, props, children, type) {
const origFunction = longPress && this._isLongPress(props)
? props.onLongPress
: props.onPress;
const nameOfAction = this._findActionName(props, children);
const wrappedFunction = (event) => {
if (nameOfAction == null) {
Logger.debug('Skipping creation of action as no name was found!');
if (origFunction != null) {
return origFunction(event);
}
}
else if (origFunction != null &&
origFunction._dtWrapped !== undefined &&
origFunction._dtWrapped === true) {
Logger.debug(`Skip wrapping of ${nameOfAction} onPress as it is already wrapped!`);
return origFunction(event);
}
else if (!ConfigurationHandler_1.ConfigurationHandler.isConfigurationAvailable()) {
Logger.info('React Native plugin has not been started yet! Touch will not be reported!');
if (origFunction != null) {
return origFunction(event);
}
}
else {
let finalNameOfAction = nameOfAction;
if (!(0, IDynatraceProperties_1.isDynatraceNaming)(props) &&
ConfigurationHandler_1.ConfigurationHandler.isActionNamePrivacyEnabled()) {
finalNameOfAction = (0, TypesUtil_1.getNameForType)(type);
}
const action = Dynatrace.enterAutoAction(`Touch on ${finalNameOfAction}`);
if (origFunction != null) {
let isSyncError = true;
try {
const returnValue = origFunction(event);
if (_isPromise(returnValue)) {
isSyncError = false;
return Promise.resolve(returnValue).finally(() => {
action.leaveAction();
});
}
else {
action.leaveAction();
}
isSyncError = false;
}
finally {
if (isSyncError) {
action.leaveAction();
}
}
}
else {
action.leaveAction();
}
}
};
wrappedFunction._dtWrapped = true;
return wrappedFunction;
},
_findActionName(props, children) {
if ((0, IDynatraceProperties_1.isDynatraceNaming)(props)) {
return props.dtActionName;
}
else if (this._isPropsButton(props) && props.title != null) {
return props.title;
}
else if (props.accessibilityLabel != null) {
return props.accessibilityLabel;
}
else if (children != null && children.length > 0) {
if (children.length === 1 && typeof children[0] === 'string') {
return children[0];
}
else {
return this._walkChildrenToFindText(children);
}
}
else if (props.children != null) {
if (typeof props.children === 'string') {
return props.children;
}
else {
return this._walkChildrenToFindText(props.children);
}
}
return null;
},
_isPropsButton: (props) => props.title != null,
_isImageButton: (props) => props.source != null,
_isLongPress: (props) => props.onLongPress != null,
_isImageURISourceType: (source) => source.uri != null,
_isIconButton: (element) => {
const type = element.type;
return type != null && type.name === 'Icon';
},
_walkTreeToFindText(element) {
if (element == null || element.props == null) {
return null;
}
else if (this._isImageButton(element.props)) {
if (this._isImageURISourceType(element.props.source)) {
return 'Image Button: ' + element.props.source.uri;
}
else {
return 'Image Button';
}
}
else if (this._isIconButton(element) && element.props.name != null) {
return 'Icon Button: ' + element.props.name;
}
return this._walkChildrenToFindText(element.props.children);
},
_walkChildrenToFindText(children) {
if (typeof children === 'string') {
return children;
}
else if (Array.isArray(children)) {
for (const child of children) {
if (this._isReactElement(child)) {
const name = this._walkTreeToFindText(child);
if (name != null) {
return name;
}
}
else if (typeof child === 'string') {
return child;
}
else if (Array.isArray(child)) {
return this._walkChildrenToFindText(child);
}
}
return null;
}
else {
return this._walkTreeToFindText(children);
}
},
_isReactElement: (node) => node != null && node.props != null,
});
exports.TouchableHelper = TouchableHelper;
const _isPromise = (object) => object != null &&
typeof object.then === 'function' &&
typeof object.catch === 'function';