@goliapkg/sentori-react-native 0.7.2 → 0.7.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/android/src/main/java/com/sentori/SentoriModule.kt +10 -0
  2. package/android/src/main/java/com/sentori/SentoriScreenshotCapture.kt +60 -2
  3. package/ios/SentoriModule.swift +10 -0
  4. package/ios/SentoriScreenshotCapture.swift +76 -2
  5. package/lib/capture.d.ts.map +1 -1
  6. package/lib/capture.js +23 -1
  7. package/lib/capture.js.map +1 -1
  8. package/lib/handlers/screenshot.d.ts +3 -2
  9. package/lib/handlers/screenshot.d.ts.map +1 -1
  10. package/lib/handlers/screenshot.js +47 -94
  11. package/lib/handlers/screenshot.js.map +1 -1
  12. package/lib/index.d.ts +4 -5
  13. package/lib/index.d.ts.map +1 -1
  14. package/lib/index.js +4 -5
  15. package/lib/index.js.map +1 -1
  16. package/lib/init.d.ts +8 -4
  17. package/lib/init.d.ts.map +1 -1
  18. package/lib/init.js +5 -0
  19. package/lib/init.js.map +1 -1
  20. package/lib/mask.d.ts +11 -47
  21. package/lib/mask.d.ts.map +1 -1
  22. package/lib/mask.js +27 -116
  23. package/lib/mask.js.map +1 -1
  24. package/lib/native.d.ts +15 -0
  25. package/lib/native.d.ts.map +1 -1
  26. package/lib/native.js +22 -0
  27. package/lib/native.js.map +1 -1
  28. package/lib/navigation.d.ts.map +1 -1
  29. package/lib/navigation.js +33 -9
  30. package/lib/navigation.js.map +1 -1
  31. package/lib/netinfo.d.ts +14 -0
  32. package/lib/netinfo.d.ts.map +1 -0
  33. package/lib/netinfo.js +70 -0
  34. package/lib/netinfo.js.map +1 -0
  35. package/package.json +7 -8
  36. package/src/__tests__/screenshot.test.ts +6 -8
  37. package/src/capture.ts +26 -1
  38. package/src/handlers/screenshot.ts +46 -117
  39. package/src/index.ts +4 -5
  40. package/src/init.ts +13 -4
  41. package/src/mask.ts +41 -0
  42. package/src/native.ts +35 -0
  43. package/src/navigation.ts +37 -9
  44. package/src/netinfo.ts +81 -0
  45. package/src/mask.tsx +0 -150
package/src/capture.ts CHANGED
@@ -9,6 +9,7 @@ import { parseStack } from './stack';
9
9
  import { getTrailBuffer } from './trail';
10
10
  import { enqueue, uploadAttachment } from './transport';
11
11
  import { uuidV7 } from './uuid';
12
+ import { getCachedNetworkType } from './netinfo';
12
13
  import type { App, AttachmentMeta, Device, Event, SentoriError, Tags, User } from './types';
13
14
 
14
15
  export { captureStep, __resetTrailForTests } from './trail';
@@ -214,17 +215,41 @@ const errorToObject = (error: Error): SentoriError => {
214
215
  const collectDevice = (): Device => {
215
216
  let os: Device['os'] = 'other';
216
217
  let osVersion = '0';
218
+ let locale: string | undefined;
219
+ const networkType = getCachedNetworkType();
217
220
  try {
218
221
  const RN = require('react-native') as {
222
+ NativeModules: {
223
+ I18nManager?: { localeIdentifier?: string };
224
+ SettingsManager?: {
225
+ settings?: { AppleLanguages?: string[]; AppleLocale?: string };
226
+ };
227
+ };
219
228
  Platform: { OS: string; Version: string | number };
220
229
  };
221
230
  const rnOS = RN.Platform.OS;
222
231
  os = rnOS === 'ios' || rnOS === 'android' || rnOS === 'web' ? rnOS : 'other';
223
232
  osVersion = String(RN.Platform.Version);
233
+ // v0.8.0-a — RN reads user locale through native modules. These
234
+ // are stable RN-internal modules (SettingsManager since 0.4,
235
+ // I18nManager since 0.16) so we can read them directly without
236
+ // an extra peer dep. iOS returns e.g. "en_US"; Android returns
237
+ // e.g. "en_US" via `getDefault().toString()`. `AppleLocale` is
238
+ // the format the user picked in Settings; `AppleLanguages[0]`
239
+ // is the resolved language priority — prefer the former.
240
+ if (rnOS === 'ios') {
241
+ const s = RN.NativeModules.SettingsManager?.settings;
242
+ locale = s?.AppleLocale ?? s?.AppleLanguages?.[0];
243
+ } else if (rnOS === 'android') {
244
+ locale = RN.NativeModules.I18nManager?.localeIdentifier;
245
+ }
224
246
  } catch {
225
247
  // not in RN runtime (jest, bun test)
226
248
  }
227
- return { os, osVersion };
249
+ const device: Device = { os, osVersion };
250
+ if (locale) device.locale = locale;
251
+ if (networkType) device.networkType = networkType;
252
+ return device;
228
253
  };
229
254
 
230
255
  const collectApp = (release: string): App => {
@@ -1,63 +1,32 @@
1
- // Phase 42 sub-D.03/04 — capture a screenshot of the current view tree
2
- // on `captureException`. Off-main-thread, best-effort, opt-in.
1
+ // v0.7.3 — capture a screenshot of the current view tree on
2
+ // `captureException`. Off-main-thread, best-effort, opt-in.
3
3
  //
4
- // Performance contract (sub-D.04):
5
- // - Yield one paint via `requestAnimationFrame` before snapshotting
6
- // so the screenshot reflects post-error UI state, not the frame
7
- // that was already half-laid-out.
8
- // - Capped output: 480 px on the longest edge, JPEG q=70. Typical
9
- // payload 30-80 KB; multipart hard cap is 500 KB.
10
- // - On any failure we silently return null. The error event still
11
- // goes to the server; the user just doesn't see a thumbnail.
4
+ // JS owns the registry of which regions to redact: the host app
5
+ // passes a thunk via `sentori.registerMaskQuery(() => string[])` that
6
+ // returns the `nativeID`s currently mounted as masked. We call it
7
+ // once per capture and forward the list to the native module, which
8
+ // renders the bitmap and paints black rectangles over the matching
9
+ // subviews in a single pass.
12
10
  //
13
- // `react-native-view-shot` is an OPTIONAL peer. We `require()` it
14
- // lazily so apps that don't install it never pay the bundle cost
15
- // or fail at import time. Without it, `captureScreenshot()` returns
16
- // `null` immediately.
11
+ // History: pre-v0.7.3 went through `react-native-view-shot` (peer
12
+ // dep) and used a JS-side overlay-opacity trick (`<MaskRegion>` /
13
+ // `setMaskedNode`) to hide PII before snapshotting. That design put
14
+ // the SDK on the render path; a single SDK bug could break the host
15
+ // app's UI. v0.7.3 cuts that coupling — the SDK no longer ships
16
+ // React components, and the screenshot path runs entirely through
17
+ // the native module already used for native-crash captures.
17
18
  //
18
- // 2026-05-15 — dropped `InteractionManager.runAfterInteractions`.
19
- // RN's docs mark `InteractionManager` as deprecated and recommend
20
- // `requestIdleCallback`, but `requestIdleCallback` doesn't actually
21
- // exist in RN (it's a web API), so the deprecation print is
22
- // unactionable for SDK consumers. The defensive "wait for the active
23
- // gesture batch to drain" semantics it provided is not reachable
24
- // from a screenshot triggered on captureException by the time an
25
- // error fires, the user is between actions, not mid-gesture — so
26
- // removing it has no observable effect except silencing the warning.
27
- // The `requestAnimationFrame` calls below still guarantee one paint
28
- // commit before captureRef snapshots.
29
-
30
- import { engageMasks } from '../mask';
31
-
32
- type CaptureRef = (
33
- // Phase 42: the lib accepts a React ref or — when we pass `undefined` —
34
- // shoots the root window. We always go for the root (no per-component
35
- // ref) so the screenshot lines up with what the user just saw.
36
- refOrUndefined: undefined,
37
- opts: {
38
- format?: 'jpg' | 'png' | 'webm';
39
- quality?: number;
40
- result?: 'base64' | 'data-uri' | 'tmpfile';
41
- width?: number;
42
- height?: number;
43
- },
44
- ) => Promise<string>;
45
-
46
- type ViewShotModule = { captureRef?: CaptureRef; default?: { captureRef?: CaptureRef } };
47
-
48
- function loadCaptureRef(): CaptureRef | null {
49
- try {
50
- // eslint-disable-next-line @typescript-eslint/no-require-imports
51
- const mod = require('react-native-view-shot') as ViewShotModule;
52
- return mod.captureRef ?? mod.default?.captureRef ?? null;
53
- } catch {
54
- return null;
55
- }
56
- }
19
+ // Performance:
20
+ // - Yield one paint via `requestAnimationFrame` before the native
21
+ // call so post-error UI state has committed.
22
+ // - 480 px on the longest edge, JPEG q=70 (iOS) / WEBP_LOSSY q=70
23
+ // (Android 11+). Typical payload 30-80 KB; multipart hard cap
24
+ // is 500 KB.
25
+ // - On any failure we silently return null. The error event still
26
+ // goes to the server; the user just doesn't see a thumbnail.
57
27
 
58
- const MAX_LONG_EDGE_PX = 480;
59
- const WEBP_QUALITY = 0.7;
60
- const CAPTURE_TIMEOUT_MS = 1500;
28
+ import { getRegisteredMaskQuery } from '../mask';
29
+ import { captureNativeScreenshotWithMask } from '../native';
61
30
 
62
31
  /** What `captureScreenshot()` hands back when it succeeds. */
63
32
  export type ScreenshotBlob = {
@@ -67,74 +36,34 @@ export type ScreenshotBlob = {
67
36
 
68
37
  /**
69
38
  * Take one screenshot, yielding the JS thread first. Returns null on
70
- * any error (missing peer dep, native side refused, timeout, etc.).
71
- * Caller is responsible for opt-in checks (`config.screenshotsEnabled`).
39
+ * any error (no native module bound, native side refused, capture
40
+ * timed out, etc.). Caller is responsible for opt-in checks
41
+ * (`config.screenshotsEnabled`).
72
42
  */
73
43
  export async function captureScreenshot(): Promise<ScreenshotBlob | null> {
74
- const captureRef = loadCaptureRef();
75
- if (!captureRef) return null;
76
-
77
44
  // Yield one paint frame so the post-error UI has committed before
78
- // we ask the OS to snapshot it. The previous
79
- // `InteractionManager.runAfterInteractions` step was removed: see
80
- // file header — its replacement (`requestIdleCallback`) doesn't
81
- // exist in RN, and on captureException the user is between actions
82
- // anyway, so the gesture-batch-drain semantics never came into
83
- // play in practice.
84
- await new Promise<void>((resolve) => {
85
- requestAnimationFrame(() => resolve());
86
- });
87
-
88
- // Phase 48 sub-B — flip every registered MaskRegion overlay to
89
- // opacity 1 (black covers the children) and every imperative
90
- // setMaskedNode ref to opacity 0 (subtree disappears). Held for
91
- // exactly one frame's worth of capture, then restored.
92
- const restoreMasks = engageMasks();
93
- // Yield one more frame so the overlay paint reaches the screen
94
- // before captureRef snapshots. Without this the overlay opacity
95
- // change is queued but the screenshotter may see the previous
96
- // frame.
45
+ // we ask the OS to snapshot it.
97
46
  await new Promise<void>((resolve) => {
98
47
  requestAnimationFrame(() => resolve());
99
48
  });
100
49
 
101
- try {
102
- const base64 = await withTimeout(
103
- captureRef(undefined, {
104
- format: 'jpg',
105
- quality: WEBP_QUALITY,
106
- result: 'base64',
107
- // Long-edge cap. RN view-shot scales preserving aspect ratio
108
- // when only one dimension is set.
109
- width: MAX_LONG_EDGE_PX,
110
- }),
111
- CAPTURE_TIMEOUT_MS,
112
- );
113
- restoreMasks();
114
- if (!base64) return null;
115
- // view-shot doesn't ship a WebP encoder on every RN version.
116
- // JPEG q=70 fits the budget too (typical 40-100 KB) and every
117
- // version handles it identically. We can swap to WebP once the
118
- // RN minimum we support has it everywhere.
119
- return { base64, mediaType: 'image/jpeg' };
120
- } catch {
121
- restoreMasks();
122
- return null;
50
+ // Read the consumer-supplied mask query once per capture. If
51
+ // the host never called `registerMaskQuery`, no mask is applied
52
+ // and the full screenshot ships — sane default: SDK does nothing
53
+ // unless told to.
54
+ const query = getRegisteredMaskQuery();
55
+ let maskedIds: string[] = [];
56
+ if (query) {
57
+ try {
58
+ maskedIds = query();
59
+ } catch {
60
+ // A throwing query is the host's bug, not ours; skip mask
61
+ // rather than skip the screenshot.
62
+ maskedIds = [];
63
+ }
123
64
  }
124
- }
125
65
 
126
- function withTimeout<T>(p: Promise<T>, ms: number): Promise<T | null> {
127
- return new Promise((resolve) => {
128
- const t = setTimeout(() => resolve(null as unknown as T), ms);
129
- p.then(
130
- (v) => {
131
- clearTimeout(t);
132
- resolve(v);
133
- },
134
- () => {
135
- clearTimeout(t);
136
- resolve(null as unknown as T);
137
- },
138
- );
139
- });
66
+ const result = await captureNativeScreenshotWithMask(maskedIds);
67
+ if (!result) return null;
68
+ return { base64: result.base64, mediaType: result.mediaType };
140
69
  }
package/src/index.ts CHANGED
@@ -2,7 +2,7 @@ import { init } from './init';
2
2
  import { addBreadcrumb } from './breadcrumbs';
3
3
  import { setUser, getUser, captureError, captureException, captureStep } from './capture';
4
4
  import { ErrorBoundary } from './error-boundary';
5
- import { MaskRegion, setMaskedNode, unsetMaskedNode } from './mask';
5
+ import { clearMaskQuery, registerMaskQuery } from './mask';
6
6
  import {
7
7
  endSession,
8
8
  markSessionCrashed,
@@ -18,9 +18,8 @@ export const sentori = {
18
18
  captureException,
19
19
  captureStep,
20
20
  ErrorBoundary,
21
- MaskRegion,
22
- setMaskedNode,
23
- unsetMaskedNode,
21
+ registerMaskQuery,
22
+ clearMaskQuery,
24
23
  startSession,
25
24
  endSession,
26
25
  markSessionCrashed,
@@ -38,7 +37,7 @@ export {
38
37
  setUser,
39
38
  } from './capture';
40
39
  export { ErrorBoundary } from './error-boundary';
41
- export { MaskRegion, setMaskedNode, unsetMaskedNode } from './mask';
40
+ export { clearMaskQuery, registerMaskQuery } from './mask';
42
41
  export {
43
42
  startAnrWatchdog,
44
43
  stopAnrWatchdog,
package/src/init.ts CHANGED
@@ -4,6 +4,7 @@ import { installLifecycleHandler } from './handlers/lifecycle';
4
4
  import { installPromiseHandler } from './handlers/promise';
5
5
  import { installNetworkHandler } from './handlers/network';
6
6
  import { drainNativePending, setNativeConfig } from './native';
7
+ import { startNetworkTypeWatch } from './netinfo';
7
8
  import { startSession } from './session-tracker';
8
9
  import {
9
10
  drainOfflineQueue,
@@ -33,10 +34,14 @@ export type InitOptions = {
33
34
  * foreground (`AppState` → `active`), ends it on background.
34
35
  * Drives crash-free rate. Set `false` to opt out. */
35
36
  sessions?: boolean;
36
- /** Phase 42 sub-D.07: capture a screenshot of the current screen
37
- * on `captureException`. Opt-in requires `react-native-view-shot`
38
- * installed and `<MaskRegion>` placed over any sensitive UI. The
39
- * image is webp q=70 480 px max, < 100 KB typical. */
37
+ /** Capture a screenshot of the current screen on
38
+ * `captureException`. Opt-in. The capture runs through the
39
+ * bundled native module no extra peer dep required since
40
+ * v0.7.3. To redact PII regions, register a mask query via
41
+ * `sentori.registerMaskQuery(() => string[])` and put
42
+ * `nativeID="..."` on the `<View>`s the SDK should black out.
43
+ * The image is webp q=70 / jpeg q=70 at 480 px max, < 100 KB
44
+ * typical. */
40
45
  screenshot?: boolean;
41
46
  /** Phase 46: record the last N steps (route changes, custom
42
47
  * breadcrumbs) leading up to a crash. On `captureException`
@@ -91,6 +96,10 @@ export const init = (options: InitOptions): void => {
91
96
  });
92
97
 
93
98
  startTransport();
99
+ // v0.8.0-c — start watching network class. No-op if NetInfo isn't
100
+ // installed; events just won't carry `device.networkType` in that
101
+ // case.
102
+ startNetworkTypeWatch();
94
103
 
95
104
  const capture = options.capture ?? {};
96
105
  if (capture.globalErrors !== false) installGlobalHandler();
package/src/mask.ts ADDED
@@ -0,0 +1,41 @@
1
+ // v0.7.3 — mask discovery via consumer-supplied query callback.
2
+ //
3
+ // The SDK does not own a registry of masked regions. The host app
4
+ // keeps its own (e.g. a `Set<string>` updated as `<Maskable>`
5
+ // components mount/unmount) and hands the SDK a thunk that returns
6
+ // the current list of native-IDs to redact. The SDK calls the thunk
7
+ // once per screenshot capture — cheap, called rarely, only on error.
8
+ //
9
+ // Why this shape: a logging SDK should never live on the render
10
+ // path. Earlier iterations exported `<MaskRegion>` (React component)
11
+ // and `setMaskedNode` (imperative ref helper), which forced every
12
+ // PII-bearing UI file to import from the SDK and put SDK bugs in
13
+ // the user's render tree. This module is JS-only — no React, no JSX,
14
+ // no native module touch — so swapping or removing the SDK doesn't
15
+ // affect rendering.
16
+
17
+ type MaskQuery = () => string[];
18
+
19
+ let _query: MaskQuery | null = null;
20
+
21
+ /**
22
+ * Register a callback the SDK calls right before each screenshot
23
+ * capture. Return the native-IDs (the `nativeID` prop on the RN
24
+ * `<View>`) that should be blacked-out in the captured image.
25
+ *
26
+ * Idempotent: a second call replaces the first. Pass `null` (or
27
+ * call `clearMaskQuery`) to detach.
28
+ */
29
+ export function registerMaskQuery(query: MaskQuery): void {
30
+ _query = query;
31
+ }
32
+
33
+ /** Unregister. Mostly for tests / teardown. */
34
+ export function clearMaskQuery(): void {
35
+ _query = null;
36
+ }
37
+
38
+ /** Internal — read by `handlers/screenshot.ts` at capture time. */
39
+ export function getRegisteredMaskQuery(): MaskQuery | null {
40
+ return _query;
41
+ }
package/src/native.ts CHANGED
@@ -11,6 +11,18 @@ type SentoriNativeModule = {
11
11
  release: string
12
12
  token: string
13
13
  }) => void
14
+ /**
15
+ * v0.7.3 — JS-triggered screenshot with consumer-supplied mask IDs.
16
+ * `maskedIds` are RN `nativeID` strings; native walks the view
17
+ * tree, finds each subview by identifier, and paints a black
18
+ * rectangle over its frame in the captured bitmap. Resolves to
19
+ * `null` if there's no key window / API < 24 (Android) / render
20
+ * timed out. Replaced the previous `react-native-view-shot`
21
+ * peer-dep path.
22
+ */
23
+ captureScreenshotWithMask?: (
24
+ maskedIds: string[],
25
+ ) => Promise<null | { base64: string; mediaType: string }>
14
26
  /**
15
27
  * Phase 22 sub-D / sub-E: cross-platform main-thread watchdog.
16
28
  * Android: 5 s / 1 s defaults (matches the OS ANR threshold).
@@ -114,3 +126,26 @@ export function stopAnrWatchdog(): void {
114
126
  // ignore
115
127
  }
116
128
  }
129
+
130
+ /**
131
+ * v0.7.3 — drives the native screenshot path. JS side passes the
132
+ * current list of mask `nativeID`s (read from the consumer's
133
+ * registered mask query); native renders + redacts.
134
+ *
135
+ * Returns `null` on every failure mode: no native module bound
136
+ * (jest, bun test, web), method missing (older native build still
137
+ * deployed), capture failed (no key window, timed out, etc.).
138
+ * Callers must treat `null` as "no screenshot this round" — the
139
+ * error event still ships, just without a thumbnail.
140
+ */
141
+ export async function captureNativeScreenshotWithMask(
142
+ maskedIds: string[],
143
+ ): Promise<null | { base64: string; mediaType: string }> {
144
+ const n = native()
145
+ if (!n?.captureScreenshotWithMask) return null
146
+ try {
147
+ return await n.captureScreenshotWithMask(maskedIds)
148
+ } catch {
149
+ return null
150
+ }
151
+ }
package/src/navigation.ts CHANGED
@@ -54,6 +54,9 @@ export type NavigationRefLike = {
54
54
  export function useTraceNavigation(navigationRef: NavigationRefLike): void {
55
55
  // Latest route name we've observed.
56
56
  const lastRouteRef = useRef<null | string>(null);
57
+ // Wall-clock ms when the last screen was entered. Drives dwell time
58
+ // attached to the leaving span + the next captureStep breadcrumb.
59
+ const lastRouteEnteredAtRef = useRef<null | number>(null);
57
60
  // Span for the screen the user is currently on. Finished when the
58
61
  // next screen is entered (or on unmount).
59
62
  const openSpanRef = useRef<null | SpanHandle>(null);
@@ -65,7 +68,11 @@ export function useTraceNavigation(navigationRef: NavigationRefLike): void {
65
68
  // Each screen gets its own trace root — detach from whatever the
66
69
  // previous screen's span was (we keep it active, so without
67
70
  // `parent: null` the new one would nest under it).
68
- const openScreenSpan = (from: null | string, to: string) => {
71
+ const openScreenSpan = (
72
+ from: null | string,
73
+ to: string,
74
+ prevDwellMs: null | number,
75
+ ) => {
69
76
  const span = startSpan('react.navigation', {
70
77
  name: from ? `${from} → ${to}` : to,
71
78
  parent: null,
@@ -74,33 +81,54 @@ export function useTraceNavigation(navigationRef: NavigationRefLike): void {
74
81
  openSpanRef.current = span;
75
82
  setActiveSpan(span);
76
83
  lastRouteRef.current = to;
77
- // Phase 46 — record the screen transition into the session trail.
78
- // No-op when sessionTrail isn't enabled (the buffer just grows
79
- // unbounded until cleared by captureException, but stays cheap).
84
+ lastRouteEnteredAtRef.current = Date.now();
85
+ // v0.8.0-b dwell on the previous screen surfaces in the
86
+ // session trail. The leaving span's `durationMs` already
87
+ // carries the same number, but the trail is the most-glanced
88
+ // surface so we duplicate it as breadcrumb data. No bytes wasted:
89
+ // a breadcrumb without sessionTrail enabled is a no-op.
80
90
  captureStep(`screen:${to}`, {
81
- breadcrumb: { type: 'navigation', message: from ? `${from} → ${to}` : to },
91
+ breadcrumb: {
92
+ data: prevDwellMs !== null ? { dwellMsPrev: prevDwellMs } : undefined,
93
+ message: from ? `${from} → ${to}` : to,
94
+ type: 'navigation',
95
+ },
82
96
  });
83
97
  };
84
98
 
99
+ const finishOpenSpanWithDwell = (): null | number => {
100
+ const span = openSpanRef.current;
101
+ const enteredAt = lastRouteEnteredAtRef.current;
102
+ if (!span) return null;
103
+ const dwellMs = enteredAt !== null ? Math.max(0, Date.now() - enteredAt) : null;
104
+ span.finish({
105
+ status: 'ok',
106
+ // Tag values are strings on the wire — cast at finish-time.
107
+ tags: dwellMs !== null ? { 'nav.dwell_ms': String(dwellMs) } : undefined,
108
+ });
109
+ return dwellMs;
110
+ };
111
+
85
112
  // Open a span for the initial screen so its requests are grouped
86
113
  // too (auth / config / first data load are usually the busiest
87
114
  // screen of a session).
88
115
  const initial = navigationRef.getCurrentRoute()?.name ?? null;
89
- if (initial !== null) openScreenSpan(null, initial);
116
+ if (initial !== null) openScreenSpan(null, initial, null);
90
117
  else lastRouteRef.current = null;
91
118
 
92
119
  const unsubscribe = navigationRef.addListener('state', () => {
93
120
  const next = navigationRef.getCurrentRoute()?.name ?? null;
94
121
  const prev = lastRouteRef.current;
95
122
  if (next === null || next === prev) return;
96
- openSpanRef.current?.finish({ status: 'ok' });
97
- openScreenSpan(prev, next);
123
+ const dwellMs = finishOpenSpanWithDwell();
124
+ openScreenSpan(prev, next, dwellMs);
98
125
  });
99
126
 
100
127
  return () => {
101
128
  unsubscribe();
102
- openSpanRef.current?.finish({ status: 'ok' });
129
+ finishOpenSpanWithDwell();
103
130
  openSpanRef.current = null;
131
+ lastRouteEnteredAtRef.current = null;
104
132
  setActiveSpan(null);
105
133
  };
106
134
  }, [navigationRef]);
package/src/netinfo.ts ADDED
@@ -0,0 +1,81 @@
1
+ // v0.8.0-c — cached read of the current network class.
2
+ //
3
+ // `@react-native-community/netinfo` is an OPTIONAL peer dep. If the
4
+ // host app has it installed (most production RN apps do — it's a
5
+ // standard library), we subscribe at SDK init time and cache the
6
+ // latest network state. `collectDevice()` reads the cache
7
+ // synchronously at capture time. If the peer isn't installed, the
8
+ // cache stays `undefined` and `device.networkType` is omitted —
9
+ // no warning, no crash.
10
+ //
11
+ // We collapse NetInfo's enum into the smaller set the protocol
12
+ // allows (see `Device.networkType` in `sdk/core/src/types.ts`):
13
+ // `wifi`, `2g/3g/4g/slow-2g`, `offline`, `unknown`. 5G collapses
14
+ // into `4g` because the schema doesn't have a 5g slot yet; the
15
+ // information loss is acceptable for an analytics dimension.
16
+
17
+ import type { Device } from '@goliapkg/sentori-core';
18
+
19
+ type NetworkType = Device['networkType'];
20
+
21
+ type NetInfoState = {
22
+ details?: { cellularGeneration?: null | string };
23
+ isConnected?: boolean | null;
24
+ type?: string;
25
+ };
26
+
27
+ type NetInfoModule = {
28
+ addEventListener?: (cb: (state: NetInfoState) => void) => () => void;
29
+ default?: { addEventListener?: (cb: (state: NetInfoState) => void) => () => void };
30
+ };
31
+
32
+ let _cached: NetworkType;
33
+ let _started = false;
34
+ let _unsubscribe: (() => void) | null = null;
35
+
36
+ function mapState(state: NetInfoState): NetworkType {
37
+ if (state.isConnected === false) return 'offline';
38
+ if (state.type === 'wifi' || state.type === 'ethernet') return 'wifi';
39
+ if (state.type === 'cellular') {
40
+ const gen = state.details?.cellularGeneration;
41
+ if (gen === '2g' || gen === '3g' || gen === '4g') return gen;
42
+ if (gen === '5g') return '4g';
43
+ return 'unknown';
44
+ }
45
+ if (state.type === 'none' || state.type === 'unknown') return 'unknown';
46
+ return undefined;
47
+ }
48
+
49
+ /**
50
+ * Idempotent — subscribe to NetInfo state changes and cache the
51
+ * latest network class. Called once from `init()`. Pure no-op if
52
+ * the peer isn't installed or we're not in an RN runtime.
53
+ */
54
+ export function startNetworkTypeWatch(): void {
55
+ if (_started) return;
56
+ _started = true;
57
+ try {
58
+ // eslint-disable-next-line @typescript-eslint/no-require-imports
59
+ const mod = require('@react-native-community/netinfo') as NetInfoModule;
60
+ const add = mod.addEventListener ?? mod.default?.addEventListener;
61
+ if (typeof add !== 'function') return;
62
+ _unsubscribe = add((state) => {
63
+ _cached = mapState(state);
64
+ });
65
+ } catch {
66
+ // not installed — leave cache undefined
67
+ }
68
+ }
69
+
70
+ /** Synchronous read at capture time. */
71
+ export function getCachedNetworkType(): NetworkType {
72
+ return _cached;
73
+ }
74
+
75
+ /** Test-only. */
76
+ export function __resetNetworkTypeForTests(): void {
77
+ _unsubscribe?.();
78
+ _unsubscribe = null;
79
+ _started = false;
80
+ _cached = undefined;
81
+ }