Skip to content

Commit 2203d90

Browse files
alyezzAli Abdelfattah
andauthored
Support react-navigation v5 (#502)
* Support react-navigation v5 * Add new API for reporting screen changes manually * update readme * update changelog * Add defensive code * Fix CI Co-authored-by: Ali Abdelfattah <[email protected]>
1 parent 1742c94 commit 2203d90

File tree

10 files changed

+149
-95
lines changed

10 files changed

+149
-95
lines changed

.circleci/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ jobs:
100100
test_android:
101101
working_directory: ~/project/InstabugSample
102102
docker:
103-
- image: circleci/android:api-28-node
103+
- image: circleci/android@sha256:793ae9aa1777ba7fc375109978b307ed03a49be468295a8778d588eba0670e4b
104104
environment:
105105
JVM_OPTS: -Xmx3200m
106106
steps:

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
## master
22

3+
* Adds support for react-navigation v5
34
* Fixes an issue with `onReportSubmitHandler` on iOS
45

56
## v9.1.7 (2020-08-10)

InstabugSample/android/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ buildscript {
1414
jcenter()
1515
}
1616
dependencies {
17-
classpath("com.android.tools.build:gradle:3.4.1")
17+
classpath('com.android.tools.build:gradle:3.6.0')
1818
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
1919

2020
// NOTE: Do not place your application dependencies here; they belong
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists

README.md

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -203,24 +203,40 @@ Instabug Repro Steps are enabled by default. It captures a screenshot of each sc
203203
204204
We support the 2 most popular React Native navigation libraries:
205205
206-
* **react-navigation**
207-
1) Set the `onNavigationStateChange` to `Instabug.onNavigationStateChange` in your App wrapper as follows:
208-
209-
```javascript
210-
export default () => (
211-
<App
212-
onNavigationStateChange={ Instabug.onNavigationStateChange } />
213-
);
214-
```
215-
216-
* **react-native-navigation**
217-
1) Register `Instabug.componentDidAppearListener` listener using:
218-
219-
```javascript
220-
Navigation.events().registerComponentDidAppearListener( Instabug.componentDidAppearListener );
221-
```
206+
* **react-navigation**
207+
208+
* **v5**
209+
set the `onStateChange` to `Instabug.onStateChange` in your NavigationContainer as follows:
210+
211+
```javascript
212+
<NavigationContainer
213+
onStateChange={ Instabug.onStateChange } />
214+
```
215+
216+
* **<=v4**
217+
set the `onNavigationStateChange` to `Instabug.onNavigationStateChange` in your App wrapper as follows:
218+
219+
```javascript
220+
export default () => (
221+
<App
222+
onNavigationStateChange={ Instabug.onNavigationStateChange } />
223+
);
224+
```
225+
* **react-native-navigation**
226+
227+
Register `Instabug.componentDidAppearListener` listener using:
228+
```javascript
229+
Navigation.events().registerComponentDidAppearListener( Instabug.componentDidAppearListener );
230+
```
231+
232+
Alternatively, you can report your screen changes manually using the following API
233+
234+
```javascript
235+
Instabug.reportScreenChange('screenName');
236+
```
222237
223238
You can disable Repro Steps using the following API:
239+
224240
```javascript
225241
Instabug.setReproStepsMode(Instabug.reproStepsMode.disabled);
226242
```

android/upload_sourcemap.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
cd ..
33
cd ..
44
cd ..
5-
if [[ -s "$HOME/.nvm/nvm.sh" ]]; then
5+
if [ -s "$HOME/.nvm/nvm.sh" ]; then
66
. "$HOME/.nvm/nvm.sh"
77
elif [[ -x "$(command -v brew)" && -s "$(brew --prefix nvm)/nvm.sh" ]]; then
88
. "$(brew --prefix nvm)/nvm.sh"

index.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,12 @@ export function onNavigationStateChange(
233233
currentState: any,
234234
action: any
235235
): void;
236+
export function onStateChange(
237+
state: any
238+
): void;
239+
export function reportScreenChange(
240+
screenName: string
241+
): void;
236242
export function componentDidAppearListener(componentObj:
237243
{ componentId: any, componentName: any, passProps: any }
238244
): void;

index.js

Lines changed: 55 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,18 @@ const InstabugModule = {
5656
* @param {invocationEvent} invocationEvent The event that invokes
5757
* the SDK's UI.
5858
*/
59-
start: function(token, invocationEvent) {
59+
start: function (token, invocationEvent) {
6060
if (Platform.OS === 'ios') {
6161
Instabug.startWithToken(token, invocationEvent);
6262
}
6363
_isFirstScreen = true;
6464
_currentScreen = firstScreen;
65-
setTimeout(function() {
65+
setTimeout(function () {
6666
if (_currentScreen == firstScreen) {
6767
Instabug.reportScreenChange(firstScreen);
6868
_currentScreen = null;
6969
}
70-
}, 1000);
70+
}, 1000);
7171
},
7272

7373
/**
@@ -190,7 +190,7 @@ const InstabugModule = {
190190
* notifications are enabled or disabled.
191191
*/
192192
setPushNotificationsEnabled(isPushNotificationEnabled) {
193-
Replies.setPushNotificationsEnabled(isPushNotificationEnabled);
193+
Replies.setPushNotificationsEnabled(isPushNotificationEnabled);
194194
},
195195

196196
/* istanbul ignore next */
@@ -221,7 +221,7 @@ const InstabugModule = {
221221
* floating button.
222222
*/
223223
setFloatingButtonEdge(floatingButtonEdge, offsetFromTop) {
224-
BugReporting.setFloatingButtonEdge(floatingButtonEdge, offsetFromTop);
224+
BugReporting.setFloatingButtonEdge(floatingButtonEdge, offsetFromTop);
225225
},
226226

227227
/**
@@ -757,28 +757,28 @@ const InstabugModule = {
757757
// handled js crash
758758
if (Platform.OS === 'android') {
759759
IBGEventEmitter.addListener(Instabug, InstabugConstants.SEND_HANDLED_CRASH, async jsonObject => {
760-
try {
761-
let report = await Instabug.getReport();
762-
const { tags, consoleLogs, instabugLogs, userAttributes, fileAttachments } = report;
763-
const reportObj = new Report(tags, consoleLogs, instabugLogs, userAttributes, fileAttachments);
764-
preSendingHandler(reportObj);
765-
Instabug.sendHandledJSCrash(JSON.stringify(jsonObject));
766-
} catch (e) {
767-
console.error(e);
768-
}
760+
try {
761+
let report = await Instabug.getReport();
762+
const { tags, consoleLogs, instabugLogs, userAttributes, fileAttachments } = report;
763+
const reportObj = new Report(tags, consoleLogs, instabugLogs, userAttributes, fileAttachments);
764+
preSendingHandler(reportObj);
765+
Instabug.sendHandledJSCrash(JSON.stringify(jsonObject));
766+
} catch (e) {
767+
console.error(e);
768+
}
769769
});
770770
}
771771

772772
if (Platform.OS === 'android') {
773773
IBGEventEmitter.addListener(Instabug, InstabugConstants.SEND_UNHANDLED_CRASH, async (jsonObject) => {
774-
775-
let report = await Instabug.getReport();
776-
const { tags, consoleLogs, instabugLogs, userAttributes, fileAttachments } = report;
777-
const reportObj = new Report(tags, consoleLogs, instabugLogs, userAttributes, fileAttachments);
778-
preSendingHandler(reportObj);
779-
Instabug.sendJSCrash(JSON.stringify(jsonObject));
774+
775+
let report = await Instabug.getReport();
776+
const { tags, consoleLogs, instabugLogs, userAttributes, fileAttachments } = report;
777+
const reportObj = new Report(tags, consoleLogs, instabugLogs, userAttributes, fileAttachments);
778+
preSendingHandler(reportObj);
779+
Instabug.sendJSCrash(JSON.stringify(jsonObject));
780780
});
781-
}
781+
}
782782

783783
Instabug.setPreSendingHandler(preSendingHandler);
784784
},
@@ -788,25 +788,44 @@ const InstabugModule = {
788788
},
789789

790790
onNavigationStateChange(prevState, currentState, action) {
791-
const currentScreen = InstabugUtils.getActiveRouteName(currentState);
792-
const prevScreen = InstabugUtils.getActiveRouteName(prevState);
791+
const currentScreen = InstabugUtils.getActiveRouteName(currentState);
792+
const prevScreen = InstabugUtils.getActiveRouteName(prevState);
793793

794-
if (prevScreen !== currentScreen) {
795-
if (_currentScreen != null && _currentScreen != firstScreen) {
796-
Instabug.reportScreenChange(_currentScreen);
794+
if (prevScreen !== currentScreen) {
795+
if (_currentScreen != null && _currentScreen != firstScreen) {
796+
Instabug.reportScreenChange(_currentScreen);
797+
_currentScreen = null;
798+
}
799+
_currentScreen = currentScreen;
800+
setTimeout(function () {
801+
if (_currentScreen == currentScreen) {
802+
Instabug.reportScreenChange(currentScreen);
797803
_currentScreen = null;
798804
}
799-
_currentScreen = currentScreen;
800-
setTimeout(function() {
801-
if (_currentScreen == currentScreen) {
802-
Instabug.reportScreenChange(currentScreen);
803-
_currentScreen = null;
804-
}
805-
}, 1000);
805+
}, 1000);
806+
}
807+
},
808+
809+
onStateChange(state) {
810+
const currentScreen = InstabugUtils.getFullRoute(state);
811+
if (_currentScreen != null && _currentScreen != firstScreen) {
812+
Instabug.reportScreenChange(_currentScreen);
813+
_currentScreen = null;
814+
}
815+
_currentScreen = currentScreen;
816+
setTimeout(function () {
817+
if (_currentScreen == currentScreen) {
818+
Instabug.reportScreenChange(currentScreen);
819+
_currentScreen = null;
806820
}
821+
}, 1000);
822+
},
823+
824+
reportScreenChange(screenName) {
825+
Instabug.reportScreenChange(screenName);
807826
},
808827

809-
componentDidAppearListener({componentId, componentName, passProps}) {
828+
componentDidAppearListener({ componentId, componentName, passProps }) {
810829
if (_isFirstScreen) {
811830
_lastScreen = componentName;
812831
_isFirstScreen = false;
@@ -816,9 +835,9 @@ const InstabugModule = {
816835
Instabug.reportScreenChange(componentName);
817836
_lastScreen = componentName;
818837
}
819-
},
838+
},
839+
820840

821-
822841
/**
823842
* The event used to invoke the feedback form
824843
* @readonly

ios/upload_sourcemap.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#!/bin/sh
22
cd ${PROJECT_DIR}
33
cd ..
4-
if [[ -s "$HOME/.nvm/nvm.sh" ]]; then
4+
if [ -s "$HOME/.nvm/nvm.sh" ]; then
55
. "$HOME/.nvm/nvm.sh"
6-
elif [[ -x "$(command -v brew)" && -s "$(brew --prefix nvm)/nvm.sh" ]]; then
6+
elif [ -x "$(command -v brew)" && -s "$(brew --prefix nvm)/nvm.sh" ]; then
77
. "$(brew --prefix nvm)/nvm.sh"
88
fi
99
export NODE_BINARY=node

utils/InstabugUtils.js

Lines changed: 49 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
'use strict';
2-
import {NativeModules, Platform} from 'react-native';
3-
let {Instabug} = NativeModules;
2+
import { NativeModules, Platform } from 'react-native';
3+
let { Instabug } = NativeModules;
44
import IBGEventEmitter from './IBGEventEmitter';
55
import InstabugConstants from './InstabugConstants';
66
import parseErrorStackLib from '../../react-native/Libraries/Core/Devtools/parseErrorStack.js';
77

88
export const parseErrorStack = (error) => {
9-
return parseErrorStackLib(error);
9+
return parseErrorStackLib(error);
1010
};
1111

1212
const originalHandler = global.ErrorUtils.getGlobalHandler();
@@ -29,55 +29,67 @@ export const getActiveRouteName = (navigationState) => {
2929
return route.routeName;
3030
}
3131

32+
function getFullRoute(state) {
33+
try {
34+
if (!state.routes[state.index].state) {
35+
return state.routes[state.index].name;
36+
}
37+
return getFullRoute(state.routes[state.index].state);
38+
} catch (e) {
39+
return "";
40+
}
41+
}
42+
3243
export const isOnReportHandlerSet = () => {
3344
return _isOnReportHandlerSet;
3445
};
3546

3647
export const captureJsErrors = () => {
37-
if (!process.env.JEST_WORKER_ID) {
38-
if (__DEV__) {
39-
return;
40-
}
48+
if (!process.env.JEST_WORKER_ID) {
49+
if (__DEV__) {
50+
return;
4151
}
52+
}
4253

43-
function errorHandler(e, isFatal) {
44-
let jsStackTrace = parseErrorStackLib(e);
54+
function errorHandler(e, isFatal) {
55+
let jsStackTrace = parseErrorStackLib(e);
4556

46-
//JSON object to be sent to the native SDK
47-
var jsonObject = {
48-
message: e.name + " - " + e.message,
49-
os: Platform.OS,
50-
platform: 'react_native',
51-
exception: jsStackTrace
52-
}
53-
54-
if(Platform.OS === 'android') {
55-
if (_isOnReportHandlerSet) {
56-
IBGEventEmitter.emit( InstabugConstants.SEND_UNHANDLED_CRASH, jsonObject);
57-
} else {
58-
Instabug.sendJSCrash(JSON.stringify(jsonObject));
59-
}
57+
//JSON object to be sent to the native SDK
58+
var jsonObject = {
59+
message: e.name + " - " + e.message,
60+
os: Platform.OS,
61+
platform: 'react_native',
62+
exception: jsStackTrace
63+
}
64+
65+
if (Platform.OS === 'android') {
66+
if (_isOnReportHandlerSet) {
67+
IBGEventEmitter.emit(InstabugConstants.SEND_UNHANDLED_CRASH, jsonObject);
6068
} else {
61-
Instabug.sendJSCrash(jsonObject);
69+
Instabug.sendJSCrash(JSON.stringify(jsonObject));
6270
}
71+
} else {
72+
Instabug.sendJSCrash(jsonObject);
73+
}
6374

64-
if (originalHandler && !process.env.JEST_WORKER_ID) {
65-
if (Platform.OS === 'ios') {
75+
if (originalHandler && !process.env.JEST_WORKER_ID) {
76+
if (Platform.OS === 'ios') {
77+
originalHandler(e, isFatal);
78+
} else {
79+
setTimeout(() => {
6680
originalHandler(e, isFatal);
67-
} else {
68-
setTimeout(() => {
69-
originalHandler(e, isFatal);
70-
}, 500);
71-
}
81+
}, 500);
7282
}
7383
}
74-
global.ErrorUtils.setGlobalHandler(errorHandler);
84+
}
85+
global.ErrorUtils.setGlobalHandler(errorHandler);
7586
};
7687

7788
export default {
78-
parseErrorStack,
79-
captureJsErrors,
80-
setOnReportHandler,
81-
isOnReportHandlerSet,
82-
getActiveRouteName
89+
parseErrorStack,
90+
captureJsErrors,
91+
setOnReportHandler,
92+
isOnReportHandlerSet,
93+
getActiveRouteName,
94+
getFullRoute
8395
};

0 commit comments

Comments
 (0)