Skip to content

Commit ef7286b

Browse files
authored
[RN] Expose getPreviousRunInfo API (#242)
* [RN] Expose getPreviousRunInfo API * npm i udpate
1 parent adca14c commit ef7286b

File tree

8 files changed

+214
-1
lines changed

8 files changed

+214
-1
lines changed

examples/expo/src/app/App.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
} from 'react-native';
1717
import {
1818
generateDeviceCode,
19+
getPreviousRunInfo,
1920
getSessionURL,
2021
info,
2122
debug,
@@ -24,6 +25,7 @@ import {
2425
warn,
2526
logScreenView,
2627
logAppLaunchTTI,
28+
type PreviousRunInfo,
2729
setFeatureFlagExposure,
2830
} from '@bitdrift/react-native';
2931
import axios from 'axios';
@@ -108,10 +110,13 @@ const HomeScreen = () => {
108110
null,
109111
);
110112
const [sessionURL, setSessionURL] = useState<string | null>(null);
113+
const [previousRunInfo, setPreviousRunInfo] = useState<PreviousRunInfo>(null);
111114
const [showPickerModal, setShowPickerModal] = useState(false);
112115
const [showErrorPickerModal, setShowErrorPickerModal] = useState(false);
113116

114117
useEffect(() => {
118+
setPreviousRunInfo(getPreviousRunInfo());
119+
115120
getSessionURL()
116121
.then(setSessionURL)
117122
.catch(() => {});
@@ -163,6 +168,9 @@ const HomeScreen = () => {
163168
<Text testID="sdk-status">
164169
{sessionURL ? 'SDK Ready' : 'SDK Not Ready'}
165170
</Text>
171+
<Text testID="previous-run-info" style={styles.previousRunInfoText}>
172+
Previous run info: {JSON.stringify(previousRunInfo)}
173+
</Text>
166174

167175
{!hasAPIKeyConfigured && (
168176
<InfoBlock
@@ -440,6 +448,11 @@ const styles = StyleSheet.create({
440448
fontWeight: 'bold',
441449
marginBottom: 20,
442450
},
451+
previousRunInfoText: {
452+
marginTop: 8,
453+
marginBottom: 8,
454+
fontSize: 12,
455+
},
443456
button: {
444457
backgroundColor: '#00A76F',
445458
padding: 10,

package-lock.json

Lines changed: 157 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/react-native/android/src/main/java/com/bdreactnative/BdReactNativeModule.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ import io.bitdrift.capture.Capture
1818
import io.bitdrift.capture.providers.session.SessionStrategy
1919
import com.facebook.react.bridge.Promise
2020
import okhttp3.HttpUrl.Companion.toHttpUrl
21-
import kotlin.time.Duration
2221
import kotlin.time.DurationUnit
2322
import kotlin.time.toDuration
2423
import io.bitdrift.capture.Configuration
24+
import io.bitdrift.capture.experimental.ExperimentalBitdriftApi
2525
import io.bitdrift.capture.reports.IssueCallbackConfiguration
2626
import io.bitdrift.capture.reports.IssueReportCallback
2727
import java.util.concurrent.ExecutorService
@@ -155,6 +155,16 @@ class BdReactNativeModule internal constructor(context: ReactApplicationContext)
155155
}
156156
}
157157

158+
@OptIn(ExperimentalBitdriftApi::class)
159+
@ReactMethod(isBlockingSynchronousMethod = true)
160+
override fun getPreviousRunInfo(): WritableMap? {
161+
val info = Capture.Logger.getPreviousRunInfo() ?: return null
162+
val map = Arguments.createMap()
163+
map.putBoolean("hasFatallyTerminated", info.hasFatallyTerminated)
164+
info.terminationReason?.let { map.putString("terminationReason", it.name) }
165+
return map
166+
}
167+
158168
@ReactMethod
159169
override fun log(level: Double, message: String, jsFields: ReadableMap?) {
160170
val fields = jsFields?.toHashMap()?.mapValues { it.value.toString() } ?: emptyMap()

packages/react-native/android/src/oldarch/BdReactNativeSpec.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import com.facebook.react.bridge.ReactApplicationContext
1111
import com.facebook.react.bridge.ReactContextBaseJavaModule
1212
import com.facebook.react.bridge.Promise
1313
import com.facebook.react.bridge.ReadableMap
14+
import com.facebook.react.bridge.WritableMap
1415

1516
abstract class BdReactNativeSpec internal constructor(context: ReactApplicationContext):
1617
ReactContextBaseJavaModule(context) {
@@ -23,6 +24,8 @@ abstract class BdReactNativeSpec internal constructor(context: ReactApplicationC
2324

2425
abstract fun getSessionURL(promise: Promise)
2526

27+
abstract fun getPreviousRunInfo(): WritableMap?
28+
2629
abstract fun log(level: Double, message: String, jsFields: ReadableMap?)
2730

2831
abstract fun addField(key: String, value: String)

packages/react-native/ios/BdReactNative.mm

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ - (void)handleIssueReportNotification:(NSNotification *)notification
6767
[CAPRNLogger getSessionURL:resolve rejecter:reject];
6868
}
6969

70+
RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(getPreviousRunInfo) {
71+
return [CAPRNLogger getPreviousRunInfo];
72+
}
73+
7074
RCT_EXPORT_METHOD(logScreenView:(NSString *)screenName)
7175
{
7276
[CAPRNLogger logScreenViewWithScreenName:screenName];

packages/react-native/ios/Logger.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,14 @@ let CAPRNIssueReportDidEmitNotification = Notification.Name("BdReactNative.onBef
133133
resolve(sessionURL)
134134
}
135135

136+
@objc
137+
public static func getPreviousRunInfo() -> [String: Any]? {
138+
guard let info = Capture.Logger.previousRunInfo else {
139+
return nil
140+
}
141+
return ["hasFatallyTerminated": info.hasFatallyTerminated]
142+
}
143+
136144
@objc
137145
public static func logScreenView(screenName: String) {
138146
Capture.Logger.logScreenView(screenName: screenName)

packages/react-native/src/NativeBdReactNative.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ export type InitOptions = {
2828
crashReporting?: CrashReportingOptions;
2929
};
3030

31+
export type PreviousRunInfo = {
32+
hasFatallyTerminated: boolean;
33+
terminationReason?: string;
34+
} | null;
35+
3136
export interface Spec extends TurboModule {
3237
init(
3338
key: string,
@@ -47,6 +52,8 @@ export interface Spec extends TurboModule {
4752

4853
getSessionURL(): Promise<string>;
4954

55+
getPreviousRunInfo(): PreviousRunInfo;
56+
5057
logScreenView(screenName: string): void;
5158

5259
logAppLaunchTTI(tti_ms: number): void;

packages/react-native/src/index.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
import {
1616
InitOptions as NativeInitOptions,
1717
CrashReportingOptions as NativeCrashReportingOptions,
18+
PreviousRunInfo as PreviousRunInfoModel,
1819
SessionStrategy,
1920
} from './NativeBdReactNative';
2021
import NativeBdReactNative from './NativeBdReactNative';
@@ -227,6 +228,16 @@ export async function getSessionURL(): Promise<string> {
227228
return NativeBdReactNative.getSessionURL();
228229
}
229230

231+
export type { PreviousRunInfo } from './NativeBdReactNative';
232+
233+
/**
234+
* Returns information about the previous app run, or `null` when not available.
235+
* Must be called after {@link init}.
236+
*/
237+
export function getPreviousRunInfo(): PreviousRunInfoModel {
238+
return NativeBdReactNative.getPreviousRunInfo();
239+
}
240+
230241
/**
231242
* Logs a screen view event. This can be called to record that a screen was viewed.
232243
* @param screenName The name of the screen that was viewed.

0 commit comments

Comments
 (0)