Skip to content

Commit 29f7009

Browse files
jenskuhrjorgensenJens Kuhr Jørgensen
andauthored
feat(sdk): Custom touch tracking property instead of accessibilityLabel (#2712)
Co-authored-by: Jens Kuhr Jørgensen <jenjrg@danskebank.dk>
1 parent 30307a0 commit 29f7009

File tree

5 files changed

+36
-16
lines changed

5 files changed

+36
-16
lines changed

CHANGELOG.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
### Breaking changes
88

99
- Message event current stack trace moved from exception to threads ([#2694](https://github.com/getsentry/sentry-react-native/pull/2694))
10+
- `touchEventBoundaryProps.labelName` property instead of default `accessibilityLabel` fallback ([#2712](https://github.com/getsentry/sentry-react-native/pull/2712))
1011

1112
### Fixes
1213

@@ -1088,10 +1089,10 @@ This release is a breaking change an code changes are necessary.
10881089
New way to import and init the SDK:
10891090

10901091
```js
1091-
import * as Sentry from "@sentry/react-native";
1092+
import * as Sentry from '@sentry/react-native';
10921093

10931094
Sentry.init({
1094-
dsn: "DSN",
1095+
dsn: 'DSN',
10951096
});
10961097
```
10971098

@@ -1326,7 +1327,7 @@ We decided to deactivate stack trace merging by default on iOS since it seems to
13261327
To activate it set:
13271328

13281329
```js
1329-
Sentry.config("___DSN___", {
1330+
Sentry.config('___DSN___', {
13301331
deactivateStacktraceMerging: false,
13311332
});
13321333
```

sample/src/App.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,8 @@ const App = () => {
103103
};
104104

105105
// Wrap your app to get more features out of the box such as auto performance monitoring.
106-
export default Sentry.wrap(App);
106+
export default Sentry.wrap(App, {
107+
touchEventBoundaryProps: {
108+
labelName: 'custom-sentry-label-name',
109+
},
110+
});

sample/src/screens/HomeScreen.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ const HomeScreen = (props: Props) => {
172172
onPress={() => {
173173
Sentry.captureException(new Error('Test Error'));
174174
}}
175-
accessibilityLabel="captureException">
175+
custom-sentry-label-name="captureException via custom Sentry label">
176176
<Text style={styles.buttonText}>Capture Exception</Text>
177177
</TouchableOpacity>
178178
<View style={styles.spacer} />

src/js/touchevents.tsx

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ export type TouchEventBoundaryProps = {
3131
* React Node wrapped by TouchEventBoundary.
3232
*/
3333
children?: React.ReactNode;
34+
/**
35+
* Label Name used to identify the touched element.
36+
*/
37+
labelName?: string;
3438
};
3539

3640
const touchEventStyles = StyleSheet.create({
@@ -96,8 +100,8 @@ class TouchEventBoundary extends React.Component<TouchEventBoundaryProps> {
96100
message: activeLabel
97101
? `Touch event within element: ${activeLabel}`
98102
: 'Touch event within component tree',
99-
type: this.props.breadcrumbType
100-
}
103+
type: this.props.breadcrumbType,
104+
};
101105
addBreadcrumb(crumb);
102106

103107
logger.log(`[TouchEvents] ${crumb.message}`);
@@ -159,20 +163,26 @@ class TouchEventBoundary extends React.Component<TouchEventBoundaryProps> {
159163
? `${props[PROP_KEY]}`
160164
: undefined;
161165

166+
// For some reason type narrowing doesn't work as expected with indexing when checking it all in one go in
167+
// the "check-label" if sentence, so we have to assign it to a variable here first
168+
let labelValue;
169+
if (typeof this.props.labelName === 'string')
170+
labelValue = props?.[this.props.labelName];
171+
162172
// Check the label first
163173
if (label && !this._isNameIgnored(label)) {
164174
if (!activeLabel) {
165175
activeLabel = label;
166176
}
167177
componentTreeNames.push(label);
168178
} else if (
169-
typeof props?.accessibilityLabel === 'string' &&
170-
!this._isNameIgnored(props.accessibilityLabel)
179+
typeof labelValue === 'string' &&
180+
!this._isNameIgnored(labelValue)
171181
) {
172182
if (!activeLabel) {
173-
activeLabel = props.accessibilityLabel;
183+
activeLabel = labelValue;
174184
}
175-
componentTreeNames.push(props.accessibilityLabel);
185+
componentTreeNames.push(labelValue);
176186
} else if (currentInst.elementType) {
177187
const { elementType } = currentInst;
178188

test/touchevents.test.tsx

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,12 @@ describe('TouchEventBoundary._onTouchStart', () => {
4646
expect(addBreadcrumb).not.toBeCalled();
4747
});
4848

49-
it('label is preferred over accessibilityLabel and displayName', () => {
49+
it('sentry-label is preferred over labelName and displayName', () => {
5050
const { defaultProps } = TouchEventBoundary;
51-
const boundary = new TouchEventBoundary(defaultProps);
51+
const boundary = new TouchEventBoundary({
52+
...defaultProps,
53+
labelName: 'custom-sentry-label-name',
54+
});
5255

5356
const event = {
5457
_targetInst: {
@@ -66,7 +69,7 @@ describe('TouchEventBoundary._onTouchStart', () => {
6669
return: {
6770
memoizedProps: {
6871
'sentry-label': 'LABEL!',
69-
accessibilityLabel: 'access!',
72+
'custom-sentry-label-name': 'access!',
7073
},
7174
},
7275
},
@@ -92,6 +95,7 @@ describe('TouchEventBoundary._onTouchStart', () => {
9295
const { defaultProps } = TouchEventBoundary;
9396
const boundary = new TouchEventBoundary({
9497
...defaultProps,
98+
labelName: 'custom-sentry-label-name',
9599
ignoreNames: ['View', 'Ignore', /^Connect\(/, new RegExp('^Happy\\(')],
96100
});
97101

@@ -111,7 +115,7 @@ describe('TouchEventBoundary._onTouchStart', () => {
111115
return: {
112116
memoizedProps: {
113117
'sentry-label': 'Ignore',
114-
accessibilityLabel: 'Ignore',
118+
'custom-sentry-label-name': 'Ignore',
115119
},
116120
elementType: {
117121
displayName: 'Styled(View2)',
@@ -151,6 +155,7 @@ describe('TouchEventBoundary._onTouchStart', () => {
151155
const boundary = new TouchEventBoundary({
152156
...defaultProps,
153157
maxComponentTreeSize: 2,
158+
labelName: 'custom-sentry-label-name',
154159
});
155160

156161
const event = {
@@ -164,7 +169,7 @@ describe('TouchEventBoundary._onTouchStart', () => {
164169
},
165170
return: {
166171
memoizedProps: {
167-
accessibilityLabel: 'Connect(View)',
172+
'custom-sentry-label-name': 'Connect(View)',
168173
},
169174
return: {
170175
elementType: {

0 commit comments

Comments
 (0)