Skip to content

Commit 44918a6

Browse files
refactoring ts interfaces / setting dependencies for hooks
1 parent 02a52fa commit 44918a6

File tree

10 files changed

+78
-96
lines changed

10 files changed

+78
-96
lines changed

package-lock.json

Lines changed: 11 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/node/instrumentation-react-native-navigation/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ export default App;
107107

108108
### React Native Navigation from Wix
109109

110-
If you are using `wix/react-native-navigation` you are also able to track navigation changes by importing and implement the `NativeNavigationTracker` component. The purpose in here is to wrap the entire application with the exposed component.
110+
If you are using `wix/react-native-navigation` you are also able to track navigation changes by importing and implement the `NativeNavigationTracker` component. The purpose in here is to wrap the entry view with the exposed component.
111111

112112
```javascript
113113
import {FC, useRef} from 'react';
@@ -156,7 +156,7 @@ const HomeScreen: FC = () => {
156156
return (
157157
<NativeNavigationTracker ref={navigationRef} provider={provider} config={config}>
158158
{/* content of the app goes here */}
159-
</NavigationTracker>
159+
</NativeNavigationTracker>
160160
);
161161
};
162162

plugins/node/instrumentation-react-native-navigation/src/components/NativeNavigationTracker.tsx

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,18 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
import React, { forwardRef, ReactNode } from 'react';
17-
import { TracerProvider } from '@opentelemetry/api';
16+
import * as React from 'react';
17+
import { forwardRef } from 'react';
1818

1919
import useTracerRef from '../utils/hooks/useTracerRef';
20+
import { TrackerProps } from '../types/navigation';
2021
import useNativeNavigationTracker, {
21-
NativeNavRef,
22+
NativeNavRef as NativeNavigationTrackerRef,
2223
} from '../hooks/useNativeNavigationTracker';
23-
import { NavigationTrackerConfig } from '../types/navigation';
24-
25-
type NativeNavigationTrackerRef = NativeNavRef;
26-
27-
interface NativeNavigationTrackerProps {
28-
children: ReactNode;
29-
// selected provider, should be configured by the app consumer
30-
provider?: TracerProvider;
31-
config?: NavigationTrackerConfig;
32-
}
3324

3425
const NativeNavigationTracker = forwardRef<
3526
NativeNavigationTrackerRef,
36-
NativeNavigationTrackerProps
27+
TrackerProps
3728
>(({ children, provider, config }, ref) => {
3829
// Initializing a Trace instance
3930
const tracer = useTracerRef(provider, config);

plugins/node/instrumentation-react-native-navigation/src/components/NavigationTracker.tsx

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,25 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
import React, { forwardRef, ReactNode } from 'react';
17-
import { TracerProvider } from '@opentelemetry/api';
16+
import * as React from 'react';
17+
import { forwardRef } from 'react';
1818

1919
import useTracerRef from '../utils/hooks/useTracerRef';
20-
import useNavigationTracker, { NavRef } from '../hooks/useNavigationTracker';
21-
import { NavigationTrackerConfig } from '../types/navigation';
20+
import { TrackerProps } from '../types/navigation';
21+
import useNavigationTracker, {
22+
NavRef as NavigationTrackerRef,
23+
} from '../hooks/useNavigationTracker';
2224

23-
type NavigationTrackerRef = NavRef;
25+
const NavigationTracker = forwardRef<NavigationTrackerRef, TrackerProps>(
26+
({ children, provider, config }, ref) => {
27+
// Initializing a Trace instance
28+
const tracer = useTracerRef(provider, config);
2429

25-
interface NavigationTrackerProps {
26-
children: ReactNode;
27-
// selected provider, configured by the app consumer if global tracer is not enough
28-
provider?: TracerProvider;
29-
config?: NavigationTrackerConfig;
30-
}
30+
useNavigationTracker(ref, tracer, config);
3131

32-
const NavigationTracker = forwardRef<
33-
NavigationTrackerRef,
34-
NavigationTrackerProps
35-
>(({ children, provider, config }, ref) => {
36-
// Initializing a Trace instance
37-
const tracer = useTracerRef(provider, config);
38-
39-
useNavigationTracker(ref, tracer, config);
40-
41-
return <>{children}</>;
42-
});
32+
return <>{children}</>;
33+
}
34+
);
4335

4436
NavigationTracker.displayName = 'NavigationTracker';
4537

plugins/node/instrumentation-react-native-navigation/src/hooks/useAppStateListener.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@
1515
*/
1616
import { AppState, AppStateStatus } from 'react-native';
1717
import { MutableRefObject, useCallback, useEffect } from 'react';
18+
import { Attributes } from '@opentelemetry/api';
19+
1820
import { spanCreatorAppState } from '../utils/spanFactory';
1921
import { TracerRef } from '../utils/hooks/useTracerRef';
2022
import { SpanRef } from '../utils/hooks/useSpanRef';
21-
import { Attributes } from '@opentelemetry/api';
2223

2324
const useAppStateListener = (
2425
tracer: TracerRef,
@@ -39,7 +40,7 @@ const useAppStateListener = (
3940

4041
appStateHandler(view?.current, currentState);
4142
},
42-
[span, tracer, attributes]
43+
[tracer, span, attributes, view]
4344
);
4445

4546
/**

plugins/node/instrumentation-react-native-navigation/src/hooks/useNativeNavigationTracker.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,35 +13,31 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
1716
import { ForwardedRef, useEffect, useMemo, useRef } from 'react';
1817

1918
import { spanCreator, spanEnd } from '../utils/spanFactory';
2019
import { TracerRef } from '../utils/hooks/useTracerRef';
2120
import useSpanRef from '../utils/hooks/useSpanRef';
22-
import {
23-
INativeNavigationContainer,
24-
NavigationTrackerConfig,
25-
} from '../types/navigation';
21+
import useConsole from '../utils/hooks/useConsole';
22+
import { INativeNavigationContainer, TrackerConfig } from '../types/navigation';
2623

2724
import useAppStateListener from './useAppStateListener';
28-
import useConsole from '../utils/hooks/useConsole';
2925

3026
export type NativeNavRef = INativeNavigationContainer;
3127

3228
const useNativeNavigationTracker = (
3329
ref: ForwardedRef<NativeNavRef>,
3430
tracer: TracerRef,
35-
config?: NavigationTrackerConfig
31+
config?: TrackerConfig
3632
) => {
3733
const navigationElRef = useMemo(() => {
3834
const isMutableRef = ref !== null && typeof ref !== 'function';
3935
return isMutableRef ? ref?.current : undefined;
4036
}, [ref]);
4137

4238
const { attributes: customAttributes, debug } = config ?? {};
43-
const console = useConsole(!!debug);
4439

40+
const console = useRef(useConsole(!!debug));
4541
const view = useRef<string | null>(null);
4642
const span = useSpanRef();
4743

@@ -50,7 +46,7 @@ const useNativeNavigationTracker = (
5046
*/
5147
const initNativeNavigationSpan = useMemo(
5248
() => spanCreator(tracer, span, view, customAttributes),
53-
[customAttributes]
49+
[customAttributes, span, tracer]
5450
);
5551

5652
/**
@@ -59,7 +55,7 @@ const useNativeNavigationTracker = (
5955
*/
6056
useEffect(() => {
6157
if (!navigationElRef) {
62-
console.warn(
58+
console.current.warn(
6359
'Navigation ref is not available. Make sure this is properly configured.'
6460
);
6561

@@ -69,7 +65,7 @@ const useNativeNavigationTracker = (
6965

7066
navigationElRef.registerComponentDidAppearListener(({ componentName }) => {
7167
if (!componentName) {
72-
console.warn(
68+
console.current.warn(
7369
'Navigation component name is not available. Make sure this is properly configured.'
7470
);
7571

@@ -83,7 +79,7 @@ const useNativeNavigationTracker = (
8379
navigationElRef.registerComponentDidDisappearListener(
8480
({ componentName }) => {
8581
if (!componentName) {
86-
console.warn(
82+
console.current.warn(
8783
'Navigation component name is not available. Make sure this is properly configured.'
8884
);
8985

plugins/node/instrumentation-react-native-navigation/src/hooks/useNavigationTracker.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,30 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
1716
import { ForwardedRef, useEffect, useMemo, useRef } from 'react';
1817

1918
import { spanCreator, spanEnd } from '../utils/spanFactory';
2019
import { TracerRef } from '../utils/hooks/useTracerRef';
2120
import useSpanRef from '../utils/hooks/useSpanRef';
22-
import {
23-
INavigationContainer,
24-
NavigationTrackerConfig,
25-
} from '../types/navigation';
21+
import useConsole from '../utils/hooks/useConsole';
22+
import { INavigationContainer, TrackerConfig } from '../types/navigation';
2623

2724
import useAppStateListener from './useAppStateListener';
28-
import useConsole from '../utils/hooks/useConsole';
2925

3026
export type NavRef = INavigationContainer;
3127

3228
const useNavigationTracker = (
3329
ref: ForwardedRef<NavRef>,
3430
tracer: TracerRef,
35-
config?: NavigationTrackerConfig
31+
config?: TrackerConfig
3632
) => {
3733
const navigationElRef = useMemo(() => {
3834
const isMutableRef = ref !== null && typeof ref !== 'function';
3935
return isMutableRef ? ref?.current : undefined;
4036
}, [ref]);
4137

4238
const { attributes: customAttributes, debug } = config ?? {};
43-
const console = useConsole(!!debug);
39+
const console = useRef(useConsole(!!debug));
4440

4541
const span = useSpanRef();
4642
const view = useRef<string | null>(null);
@@ -50,7 +46,7 @@ const useNavigationTracker = (
5046
*/
5147
const initNavigationSpan = useMemo(
5248
() => spanCreator(tracer, span, view, customAttributes),
53-
[customAttributes]
49+
[customAttributes, span, tracer]
5450
);
5551

5652
/**
@@ -59,7 +55,7 @@ const useNavigationTracker = (
5955
*/
6056
useEffect(() => {
6157
if (!navigationElRef) {
62-
console.warn(
58+
console.current.warn(
6359
'Navigation ref is not available. Make sure this is properly configured.'
6460
);
6561

@@ -71,7 +67,7 @@ const useNavigationTracker = (
7167
const { name: routeName } = navigationElRef.getCurrentRoute() ?? {};
7268

7369
if (!routeName) {
74-
console.warn(
70+
console.current.warn(
7571
'Navigation route name is not available. Make sure this is properly configured.'
7672
);
7773

plugins/node/instrumentation-react-native-navigation/src/types/navigation.ts

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,35 +13,30 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
import { Attributes, TracerOptions } from '@opentelemetry/api';
17-
import { useNavigationContainerRef as useReactNativeNavigationContainerRef } from '@react-navigation/native';
18-
import { useNavigationContainerRef as useReactNativeNavigationExpoContainerRef } from 'expo-router';
19-
import { Navigation } from 'react-native-navigation';
16+
import { EventsRegistry } from 'react-native-navigation';
17+
import { ReactNode } from 'react';
18+
import { EventConsumer, EventMapBase, Route } from '@react-navigation/native';
19+
import { Attributes, TracerOptions, TracerProvider } from '@opentelemetry/api';
2020

21-
/*
22-
* Copyright The OpenTelemetry Authors
23-
*
24-
* Licensed under the Apache License, Version 2.0 (the "License");
25-
* you may not use this file except in compliance with the License.
26-
* You may obtain a copy of the License at
27-
*
28-
* https://www.apache.org/licenses/LICENSE-2.0
29-
*
30-
* Unless required by applicable law or agreed to in writing, software
31-
* distributed under the License is distributed on an "AS IS" BASIS,
32-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
33-
* See the License for the specific language governing permissions and
34-
* limitations under the License.
35-
*/
36-
37-
export type INativeNavigationContainer = ReturnType<typeof Navigation.events>;
21+
export type INativeNavigationContainer = Pick<
22+
EventsRegistry,
23+
'registerComponentDidDisappearListener' | 'registerComponentDidAppearListener'
24+
>;
3825

39-
export type INavigationContainer =
40-
| ReturnType<typeof useReactNativeNavigationContainerRef>
41-
| ReturnType<typeof useReactNativeNavigationExpoContainerRef>['current'];
26+
export type INavigationContainer = Pick<
27+
EventConsumer<EventMapBase>,
28+
'addListener'
29+
> & { getCurrentRoute: () => Route<string> | undefined };
4230

43-
export interface NavigationTrackerConfig {
31+
export interface TrackerConfig {
4432
attributes?: Attributes;
4533
tracerOptions?: TracerOptions;
4634
debug?: boolean; // enabling `debug` mode will print console messages (info and warns). useful for debugging
4735
}
36+
37+
export interface TrackerProps {
38+
children: ReactNode;
39+
// selected provider, should be configured by the app consumer
40+
provider?: TracerProvider;
41+
config?: TrackerConfig;
42+
}

plugins/node/instrumentation-react-native-navigation/src/utils/hooks/useTracerRef.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,17 @@
1515
*/
1616
import { MutableRefObject, useEffect, useRef } from 'react';
1717
import { trace, Tracer, TracerProvider } from '@opentelemetry/api';
18-
/** @knipignore */
18+
1919
import { PACKAGE_NAME, PACKAGE_VERSION } from '../../version';
20+
import { TrackerConfig } from '../../types/navigation';
21+
2022
import useConsole from './useConsole';
21-
import { NavigationTrackerConfig } from '../../types/navigation';
2223

2324
export type TracerRef = MutableRefObject<Tracer | null>;
2425

2526
const useTracerRef = (
2627
provider?: TracerProvider,
27-
config?: NavigationTrackerConfig
28+
config?: TrackerConfig
2829
): TracerRef => {
2930
const { debug, tracerOptions } = config ?? {};
3031
const tracerRef = useRef<Tracer | null>(null);
@@ -44,7 +45,7 @@ const useTracerRef = (
4445
trace.getTracer(PACKAGE_NAME, PACKAGE_VERSION);
4546
}
4647

47-
// this is useful in cases where the provider is passed but it's still `null` or `undefined` (given a re-render or something specific of the lyfecycle of the app that implements the library)
48+
// this is useful in cases where the provider is passed but it's still `null` or `undefined` (given a re-render or something specific of the lifecycle of the app that implements the library)
4849
if (
4950
tracerRef.current !== null &&
5051
provider !== undefined &&
@@ -58,7 +59,7 @@ const useTracerRef = (
5859

5960
console.info('Updated TracerProvider. Switching to the new instance.');
6061
}
61-
}, [provider, tracerOptions]);
62+
}, [console, provider, tracerOptions]);
6263

6364
return tracerRef;
6465
};

plugins/node/instrumentation-react-native-navigation/src/utils/spanFactory.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
*/
1616
import { AppStateStatus } from 'react-native';
1717
import { MutableRefObject } from 'react';
18+
import { Attributes } from '@opentelemetry/api';
1819

1920
import { TracerRef } from './hooks/useTracerRef';
2021
import { SpanRef } from './hooks/useSpanRef';
21-
import { Attributes } from '@opentelemetry/api';
2222

2323
const ATTRIBUTES = {
2424
initialView: 'view.launch',
@@ -68,7 +68,7 @@ const spanEnd = (
6868

6969
span.current.end();
7070

71-
// make sure we destroy any existent span
71+
// make sure we destroy any existing span
7272
span.current = null;
7373
}
7474
};

0 commit comments

Comments
 (0)