-
-
Notifications
You must be signed in to change notification settings - Fork 364
Expand file tree
/
Copy pathNavigationContainer.tsx
More file actions
166 lines (154 loc) · 5.02 KB
/
Copy pathNavigationContainer.tsx
File metadata and controls
166 lines (154 loc) · 5.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import { debug, getClient } from '@sentry/core';
import * as React from 'react';
import { getNavigationContainerComponent } from './reactNavigationImport';
import { getReactNavigationIntegration } from './tracing/reactnavigation';
export type FontStyle = {
fontFamily: string;
fontWeight: 'normal' | 'bold' | '100' | '200' | '300' | '400' | '500' | '600' | '700' | '800' | '900';
};
/**
* Mirrors the `Theme` type from `@react-navigation/native`.
*/
export interface NavigationTheme {
dark: boolean;
colors: {
primary: string;
background: string;
card: string;
text: string;
border: string;
notification: string;
};
fonts: {
regular: FontStyle;
medium: FontStyle;
bold: FontStyle;
heavy: FontStyle;
};
}
/**
* Props accepted by `Sentry.NavigationContainer`.
*
* Mirrors the props of `NavigationContainer` from `@react-navigation/native`
* so that users get autocomplete without requiring a compile-time dependency
* on the library.
*/
export interface SentryNavigationContainerProps {
children: React.ReactNode;
initialState?: {
index?: number;
key?: string;
routeNames?: string[];
routes: Array<{
key?: string;
name: string;
params?: object;
state?: object;
}>;
stale?: true;
type?: string;
};
onStateChange?: (state: Readonly<Record<string, unknown>> | undefined) => void;
onReady?: () => void;
onUnhandledAction?: (action: Readonly<{ type: string; payload?: object; source?: string; target?: string }>) => void;
theme?: NavigationTheme;
direction?: 'ltr' | 'rtl';
linking?: {
enabled?: boolean;
prefixes: string[];
filter?: (url: string) => boolean;
config?: {
path?: string;
screens: Record<string, unknown>;
initialRouteName?: string;
};
getInitialURL?: () => string | null | undefined | Promise<string | null | undefined>;
subscribe?: (listener: (url: string) => void) => undefined | void | (() => void);
getStateFromPath?: (path: string, options?: object) => object | undefined;
getPathFromState?: (state: object, options?: object) => string;
getActionFromState?: (state: object, options?: object) => object | undefined;
};
fallback?: React.ReactNode;
documentTitle?: {
enabled?: boolean;
formatter?: (
options: Record<string, unknown> | undefined,
route: { key: string; name: string; params?: object } | undefined,
) => string;
};
[key: string]: unknown;
}
let _warnedMissing = false;
let _warnedNoClient = false;
let _warnedNoIntegration = false;
/**
* Drop-in replacement for `NavigationContainer` from `@react-navigation/native`
* that automatically wires up Sentry's `reactNavigationIntegration`.
*
* Sentry registers the navigation container before the user-provided `onReady`
* callback fires, so navigation spans are captured from the first route.
*
* If `@react-navigation/native` is not installed, children are rendered directly
* and `onReady` is not called.
*
* @example
* ```jsx
* <Sentry.NavigationContainer>
* <Stack.Navigator>
* ...
* </Stack.Navigator>
* </Sentry.NavigationContainer>
* ```
*/
export const NavigationContainer = React.forwardRef<unknown, SentryNavigationContainerProps>((props, forwardedRef) => {
const { onReady: userOnReady, ...restProps } = props;
const RealNavigationContainer = getNavigationContainerComponent();
const internalRef = React.useRef<unknown>(null);
const mergedRef = React.useCallback(
(instance: unknown) => {
internalRef.current = instance;
if (typeof forwardedRef === 'function') {
forwardedRef(instance);
} else if (forwardedRef != null) {
(forwardedRef as React.MutableRefObject<unknown>).current = instance;
}
},
[forwardedRef],
);
const onReady = React.useCallback(() => {
try {
const client = getClient();
if (!client) {
if (!_warnedNoClient) {
_warnedNoClient = true;
debug.warn(
'[Sentry] NavigationContainer: Sentry is not initialized. Call Sentry.init() before mounting NavigationContainer.',
);
}
} else {
const integration = getReactNavigationIntegration(client);
if (integration) {
integration.registerNavigationContainer(internalRef);
} else if (!_warnedNoIntegration) {
_warnedNoIntegration = true;
debug.log(
'[Sentry] NavigationContainer: reactNavigationIntegration is not registered. Navigation spans will not be captured.',
);
}
}
} catch {
// SDK failures must never break the host app
}
if (typeof userOnReady === 'function') {
(userOnReady as () => void)();
}
}, [userOnReady]);
if (!RealNavigationContainer) {
if (!_warnedMissing) {
_warnedMissing = true;
debug.warn('[Sentry] NavigationContainer requires @react-navigation/native to be installed.');
}
return <>{restProps.children as React.ReactNode}</>;
}
return <RealNavigationContainer {...restProps} ref={mergedRef} onReady={onReady} />;
});