-
Notifications
You must be signed in to change notification settings - Fork 48.1k
/
Copy pathDevTools.js
332 lines (305 loc) · 11.8 KB
/
DevTools.js
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
// Reach styles need to come before any component styles.
// This makes overriding the styles simpler.
import '@reach/menu-button/styles.css';
import '@reach/tooltip/styles.css';
import * as React from 'react';
import {useCallback, useEffect, useLayoutEffect, useMemo, useRef} from 'react';
import Store from '../store';
import {
BridgeContext,
ContextMenuContext,
StoreContext,
OptionsContext,
} from './context';
import Components from './Components/Components';
import Profiler from './Profiler/Profiler';
import TabBar from './TabBar';
import {SettingsContextController} from './Settings/SettingsContext';
import {TreeContextController} from './Components/TreeContext';
import ViewElementSourceContext from './Components/ViewElementSourceContext';
import FetchFileWithCachingContext from './Components/FetchFileWithCachingContext';
import HookNamesModuleLoaderContext from 'react-devtools-shared/src/devtools/views/Components/HookNamesModuleLoaderContext';
import {ProfilerContextController} from './Profiler/ProfilerContext';
import {TimelineContextController} from 'react-devtools-timeline/src/TimelineContext';
import {ModalDialogContextController} from './ModalDialog';
import ReactLogo from './ReactLogo';
import UnsupportedBridgeProtocolDialog from './UnsupportedBridgeProtocolDialog';
import UnsupportedVersionDialog from './UnsupportedVersionDialog';
import WarnIfLegacyBackendDetected from './WarnIfLegacyBackendDetected';
import {useLocalStorage} from './hooks';
import ThemeProvider from './ThemeProvider';
import {LOCAL_STORAGE_DEFAULT_TAB_KEY} from '../../constants';
import {logEvent} from '../../Logger';
import styles from './DevTools.css';
import './root.css';
import type {FetchFileWithCaching} from './Components/FetchFileWithCachingContext';
import type {HookNamesModuleLoaderFunction} from 'react-devtools-shared/src/devtools/views/Components/HookNamesModuleLoaderContext';
import type {FrontendBridge} from 'react-devtools-shared/src/bridge';
import type {BrowserTheme} from 'react-devtools-shared/src/frontend/types';
import type {Source} from 'react-devtools-shared/src/shared/types';
export type TabID = 'components' | 'profiler';
export type ViewElementSource = (
source: Source,
symbolicatedSource: Source | null,
) => void;
export type ViewAttributeSource = (
id: number,
path: Array<string | number>,
) => void;
export type CanViewElementSource = (
source: Source,
symbolicatedSource: Source | null,
) => boolean;
export type Props = {
bridge: FrontendBridge,
browserTheme?: BrowserTheme,
canViewElementSourceFunction?: ?CanViewElementSource,
defaultTab?: TabID,
enabledInspectedElementContextMenu?: boolean,
showTabBar?: boolean,
store: Store,
warnIfLegacyBackendDetected?: boolean,
warnIfUnsupportedVersionDetected?: boolean,
viewAttributeSourceFunction?: ?ViewAttributeSource,
viewElementSourceFunction?: ?ViewElementSource,
readOnly?: boolean,
hideSettings?: boolean,
hideToggleErrorAction?: boolean,
hideToggleSuspenseAction?: boolean,
hideLogAction?: boolean,
hideViewSourceAction?: boolean,
// This property is used only by the web extension target.
// The built-in tab UI is hidden in that case, in favor of the browser's own panel tabs.
// This is done to save space within the app.
// Because of this, the extension needs to be able to change which tab is active/rendered.
overrideTab?: TabID,
// To avoid potential multi-root trickiness, the web extension uses portals to render tabs.
// The root <DevTools> app is rendered in the top-level extension window,
// but individual tabs (e.g. Components, Profiling) can be rendered into portals within their browser panels.
componentsPortalContainer?: Element,
profilerPortalContainer?: Element,
// Loads and parses source maps for function components
// and extracts hook "names" based on the variables the hook return values get assigned to.
// Not every DevTools build can load source maps, so this property is optional.
fetchFileWithCaching?: ?FetchFileWithCaching,
// TODO (Webpack 5) Hopefully we can remove this prop after the Webpack 5 migration.
hookNamesModuleLoaderFunction?: ?HookNamesModuleLoaderFunction,
};
const componentsTab = {
id: ('components': TabID),
icon: 'components',
label: 'Components',
title: 'React Components',
};
const profilerTab = {
id: ('profiler': TabID),
icon: 'profiler',
label: 'Profiler',
title: 'React Profiler',
};
const tabs = [componentsTab, profilerTab];
export default function DevTools({
bridge,
browserTheme = 'light',
canViewElementSourceFunction,
componentsPortalContainer,
defaultTab = 'components',
enabledInspectedElementContextMenu = false,
fetchFileWithCaching,
hookNamesModuleLoaderFunction,
overrideTab,
profilerPortalContainer,
showTabBar = false,
store,
warnIfLegacyBackendDetected = false,
warnIfUnsupportedVersionDetected = false,
viewAttributeSourceFunction,
viewElementSourceFunction,
readOnly,
hideSettings,
hideToggleErrorAction,
hideToggleSuspenseAction,
hideLogAction,
hideViewSourceAction,
}: Props): React.Node {
const [currentTab, setTab] = useLocalStorage<TabID>(
LOCAL_STORAGE_DEFAULT_TAB_KEY,
defaultTab,
);
let tab = currentTab;
if (overrideTab != null) {
tab = overrideTab;
}
const selectTab = useCallback(
(tabId: TabID) => {
// We show the TabBar when DevTools is NOT rendered as a browser extension.
// In this case, we want to capture when people select tabs with the TabBar.
// When DevTools is rendered as an extension, we capture this event when
// the browser devtools panel changes.
if (showTabBar === true) {
if (tabId === 'components') {
logEvent({event_name: 'selected-components-tab'});
} else {
logEvent({event_name: 'selected-profiler-tab'});
}
}
setTab(tabId);
},
[setTab, showTabBar],
);
const options = useMemo(
() => ({
readOnly: readOnly || false,
hideSettings: hideSettings || false,
hideToggleErrorAction: hideToggleErrorAction || false,
hideToggleSuspenseAction: hideToggleSuspenseAction || false,
hideLogAction: hideLogAction || false,
hideViewSourceAction: hideViewSourceAction || false,
}),
[
readOnly,
hideSettings,
hideToggleErrorAction,
hideToggleSuspenseAction,
hideLogAction,
hideViewSourceAction,
],
);
const viewElementSource = useMemo(
() => ({
canViewElementSourceFunction: canViewElementSourceFunction || null,
viewElementSourceFunction: viewElementSourceFunction || null,
}),
[canViewElementSourceFunction, viewElementSourceFunction],
);
const contextMenu = useMemo(
() => ({
isEnabledForInspectedElement: enabledInspectedElementContextMenu,
viewAttributeSourceFunction: viewAttributeSourceFunction || null,
}),
[enabledInspectedElementContextMenu, viewAttributeSourceFunction],
);
const devToolsRef = useRef<HTMLElement | null>(null);
useEffect(() => {
if (!showTabBar) {
return;
}
const div = devToolsRef.current;
if (div === null) {
return;
}
const ownerWindow = div.ownerDocument.defaultView;
const handleKeyDown = (event: KeyboardEvent) => {
if (event.ctrlKey || event.metaKey) {
switch (event.key) {
case '1':
selectTab(tabs[0].id);
event.preventDefault();
event.stopPropagation();
break;
case '2':
selectTab(tabs[1].id);
event.preventDefault();
event.stopPropagation();
break;
}
}
};
ownerWindow.addEventListener('keydown', handleKeyDown);
return () => {
ownerWindow.removeEventListener('keydown', handleKeyDown);
};
}, [showTabBar]);
useLayoutEffect(() => {
return () => {
try {
// Shut the Bridge down synchronously (during unmount).
bridge.shutdown();
} catch (error) {
// Attempting to use a disconnected port.
}
};
}, [bridge]);
useEffect(() => {
logEvent({event_name: 'loaded-dev-tools'});
}, []);
return (
<BridgeContext.Provider value={bridge}>
<StoreContext.Provider value={store}>
<OptionsContext.Provider value={options}>
<ContextMenuContext.Provider value={contextMenu}>
<ModalDialogContextController>
<SettingsContextController
browserTheme={browserTheme}
componentsPortalContainer={componentsPortalContainer}
profilerPortalContainer={profilerPortalContainer}>
<ViewElementSourceContext.Provider value={viewElementSource}>
<HookNamesModuleLoaderContext.Provider
value={hookNamesModuleLoaderFunction || null}>
<FetchFileWithCachingContext.Provider
value={fetchFileWithCaching || null}>
<TreeContextController>
<ProfilerContextController>
<TimelineContextController>
<ThemeProvider>
<div
className={styles.DevTools}
ref={devToolsRef}
data-react-devtools-portal-root={true}>
{showTabBar && (
<div className={styles.TabBar}>
<ReactLogo />
<span className={styles.DevToolsVersion}>
{process.env.DEVTOOLS_VERSION}
</span>
<div className={styles.Spacer} />
<TabBar
currentTab={tab}
id="DevTools"
selectTab={selectTab}
tabs={tabs}
type="navigation"
/>
</div>
)}
<div
className={styles.TabContent}
hidden={tab !== 'components'}>
<Components
portalContainer={componentsPortalContainer}
/>
</div>
<div
className={styles.TabContent}
hidden={tab !== 'profiler'}>
<Profiler
portalContainer={profilerPortalContainer}
/>
</div>
</div>
</ThemeProvider>
</TimelineContextController>
</ProfilerContextController>
</TreeContextController>
</FetchFileWithCachingContext.Provider>
</HookNamesModuleLoaderContext.Provider>
</ViewElementSourceContext.Provider>
</SettingsContextController>
<UnsupportedBridgeProtocolDialog />
{warnIfLegacyBackendDetected && <WarnIfLegacyBackendDetected />}
{warnIfUnsupportedVersionDetected && <UnsupportedVersionDialog />}
</ModalDialogContextController>
</ContextMenuContext.Provider>
</OptionsContext.Provider>
</StoreContext.Provider>
</BridgeContext.Provider>
);
}