-
-
Notifications
You must be signed in to change notification settings - Fork 4k
/
contexts.jsx
219 lines (193 loc) · 5.4 KB
/
contexts.jsx
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import PropTypes from 'prop-types';
import React from 'react';
import {objectIsEmpty, toTitleCase, defined} from 'app/utils';
import EventDataSection from 'app/components/events/eventDataSection';
import plugins from 'app/plugins';
import {t} from 'app/locale';
const CONTEXT_TYPES = {
default: require('app/components/events/contexts/default').default,
app: require('app/components/events/contexts/app/app').default,
device: require('app/components/events/contexts/device/device').default,
os: require('app/components/events/contexts/operatingSystem/operatingSystem').default,
runtime: require('app/components/events/contexts/runtime/runtime').default,
user: require('app/components/events/contexts/user/user').default,
gpu: require('app/components/events/contexts/gpu/gpu').default,
trace: require('app/components/events/contexts/trace/trace').default,
'redux.state': require('app/components/events/contexts/redux').default,
};
function getContextComponent(type) {
return CONTEXT_TYPES[type] || plugins.contexts[type] || CONTEXT_TYPES.default;
}
function getSourcePlugin(pluginContexts, contextType) {
if (CONTEXT_TYPES[contextType]) {
return null;
}
for (const plugin of pluginContexts) {
if (plugin.contexts.indexOf(contextType) >= 0) {
return plugin;
}
}
return null;
}
class ContextChunk extends React.Component {
static propTypes = {
event: PropTypes.object.isRequired,
group: PropTypes.object,
type: PropTypes.string.isRequired,
alias: PropTypes.string.isRequired,
value: PropTypes.object.isRequired,
};
constructor(...args) {
super(...args);
this.state = {
isLoading: false,
};
}
UNSAFE_componentWillMount() {
this.syncPlugin();
}
componentDidUpdate(prevProps) {
if (
prevProps.type !== this.props.type ||
prevProps?.group?.id !== this.props?.group?.id
) {
this.syncPlugin();
}
}
syncPlugin = () => {
const {group, type, alias} = this.props;
// If we don't have a grouped event we can't sync with plugins.
if (!group) {
return;
}
// Search using `alias` first because old plugins rely on it and type is set to "default"
// e.g. sessionstack
const sourcePlugin =
type === 'default'
? getSourcePlugin(group.pluginContexts, alias) ||
getSourcePlugin(group.pluginContexts, type)
: getSourcePlugin(group.pluginContexts, type);
if (!sourcePlugin) {
this.setState({
pluginLoading: false,
});
} else {
this.setState(
{
pluginLoading: true,
},
() => {
plugins.load(sourcePlugin, () => {
this.setState({pluginLoading: false});
});
}
);
}
};
getTitle = () => {
const {value, alias, type} = this.props;
if (defined(value.title)) {
return value.title;
}
if (!defined(type)) {
return toTitleCase(alias);
}
switch (type) {
case 'app':
return t('App');
case 'device':
return t('Device');
case 'os':
return t('Operating System');
case 'user':
return t('User');
case 'gpu':
return t('Graphics Processing Unit');
case 'runtime':
return t('Runtime');
case 'trace':
return t('Trace Details');
case 'default':
return toTitleCase(alias);
default:
return toTitleCase(type);
}
};
renderSectionTitle = () => {
const {alias, type} = this.props;
return (
<React.Fragment>
{this.getTitle()}
{defined(type) && type !== 'default' && alias !== type && (
<small>({alias})</small>
)}
</React.Fragment>
);
};
render() {
// if we are currently loading the plugin, just render nothing for now.
if (this.state.pluginLoading) {
return null;
}
const evt = this.props.event;
const {type, alias, value = {}} = this.props;
const Component =
type === 'default'
? getContextComponent(alias) || getContextComponent(type)
: getContextComponent(type);
const isObjectValueEmpty = Object.values(value).filter(v => defined(v)).length === 0;
// this can happen if the component does not exist
if (!Component || isObjectValueEmpty) {
return null;
}
return (
<EventDataSection
event={evt}
key={`context-${alias}`}
type={`context-${alias}`}
title={this.renderSectionTitle()}
>
<Component alias={alias} event={evt} data={value} />
</EventDataSection>
);
}
}
class ContextsInterface extends React.Component {
static propTypes = {
event: PropTypes.object.isRequired,
group: PropTypes.object,
};
render() {
const group = this.props.group;
const evt = this.props.event;
const children = [];
if (!objectIsEmpty(evt.user)) {
children.push(
<ContextChunk
group={group}
event={evt}
type="user"
alias="user"
value={evt.user}
key="user"
/>
);
}
let value = null;
for (const key in evt.contexts) {
value = evt.contexts[key];
children.push(
<ContextChunk
group={group}
event={evt}
type={value.type}
alias={key}
value={value}
key={key}
/>
);
}
return <React.Fragment>{children}</React.Fragment>;
}
}
export default ContextsInterface;