-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathRumRouteComponentWrapper.tsx
83 lines (71 loc) · 2.27 KB
/
RumRouteComponentWrapper.tsx
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
import { isValidElement, useRef } from 'react';
import type { RouteProps, RouteComponentProps } from 'react-router-dom';
import { getGlobalObject } from '../utils/getGlobalObject';
type RumRouteComponentType =
| RouteProps['component']
| RouteProps['children']
| RouteProps['render'];
function isClassComponent(
component: RumRouteComponentType
): component is React.ComponentClass {
return (
typeof component === 'function' && !!component.prototype?.isReactComponent
);
}
function isFunctionComponent(
component: RumRouteComponentType
): component is React.FunctionComponent<any> {
return (
typeof component === 'function' &&
component.hasOwnProperty('props') &&
isValidElement(component)
);
}
function isReactRouterComponent(
component: RumRouteComponentType
): component is RouteProps['component'] {
return isClassComponent(component) || isFunctionComponent(component);
}
export const withRum = (component: RumRouteComponentType) =>
function RumView(props: RouteComponentProps) {
useRef(
(() => {
if (!component) {
return;
}
const globalObj = getGlobalObject<Window>();
if (!globalObj.DD_RUM) {
console.warn(
'@datadog/rum-react-integration: Datadog RUM SDK is not initialized.'
);
return;
}
if (!globalObj.DD_RUM?.startView) {
console.warn(
'@datadog/rum-react-integration: Manual tracking not supported. Try updating the Datadog RUM SDK.'
);
return;
}
const manualTracking = !!globalObj.DD_RUM?.getInitConfiguration()
?.trackViewsManually;
if (!manualTracking) {
console.warn(
'@datadog/rum-react-integration: The trackViewsManually flag in RUM initialization must be set to %ctrue%c.',
'color:green',
'color:default'
);
return;
}
globalObj.DD_RUM.startView(props.match.path);
})()
);
if (!component) {
return <>{component}</>;
} else if (isReactRouterComponent(component)) {
const Component = component;
return <Component {...props} />;
} else if (component instanceof Function) {
return <>{component(props)}</>;
}
return <>{component}</>;
};