mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
7ff4d057b6
<!-- Thanks for submitting a pull request! We appreciate you spending the time to work on these changes. Please provide enough information so that others can review your pull request. The three fields below are mandatory. Before submitting a pull request, please make sure the following is done: 1. Fork [the repository](https://github.com/facebook/react) and create your branch from `main`. 2. Run `yarn` in the repository root. 3. If you've fixed a bug or added code that should be tested, add tests! 4. Ensure the test suite passes (`yarn test`). Tip: `yarn test --watch TestName` is helpful in development. 5. Run `yarn test --prod` to test in the production environment. It supports the same options as `yarn test`. 6. If you need a debugger, run `yarn test --debug --watch TestName`, open `chrome://inspect`, and press "Inspect". 7. Format your code with [prettier](https://github.com/prettier/prettier) (`yarn prettier`). 8. Make sure your code lints (`yarn lint`). Tip: `yarn linc` to only check changed files. 9. Run the [Flow](https://flowtype.org/) type checks (`yarn flow`). 10. If you haven't already, complete the CLA. Learn more about contributing: https://reactjs.org/docs/how-to-contribute.html --> ## Summary This PR adds support for displaying the names of changed hooks directly in the Profiler tab, making it easier to identify specific updates. A `HookChangeSummary` component has been introduced to show these hook names, with a `displayMode` prop that toggles between `“compact”` for tooltips and `“detailed”` for more in-depth views. This keeps tooltip summaries concise while allowing for a full breakdown where needed. This functionality also respects the `“Always parse hook names from source”` setting from the Component inspector, as it uses the same caching mechanism already in place for the Components tab. Additionally, even without hook names parsed, the Profiler will now display hook types (like `State`, `Callback`, etc.) based on data from `inspectedElement`. To enable this across the DevTools, `InspectedElementContext` has been moved higher in the component tree, allowing it to be shared between the Profiler and Components tabs. This update allows hook name data to be reused across tabs without duplication. Additionally, a `getAlreadyLoadedHookNames` helper function was added to efficiently access cached hook names, reducing the need for repeated fetching when displaying changes. These changes improve the ability to track specific hook updates within the Profiler tab, making it clearer to see what’s changed. ### Before Previously, the Profiler tab displayed only the IDs of changed hooks, as shown below: <img width="350" alt="Screenshot 2024-11-01 at 12 02 21_cropped" src="https://github.com/user-attachments/assets/7a5f5f67-f1c8-4261-9ba3-1c76c9a88af3"> ### After (without hook names parsed) When hook names aren’t parsed, custom hooks and hook types are displayed based on the inspectedElement data: <img width="350" alt="Screenshot 2024-11-01 at 12 03 09_cropped" src="https://github.com/user-attachments/assets/ed857a6d-e6ef-4e5b-982c-bf30c2d8a7e2"> ### After (with hook names parsed) Once hook names are fully parsed, the Profiler tab provides a complete breakdown of specific hooks that have changed: <img width="350" alt="Screenshot 2024-11-01 at 12 03 14_cropped" src="https://github.com/user-attachments/assets/1ddfcc35-7474-4f4d-a084-f4e9f993a5bf"> This should resolve #21856 🎉
338 lines
12 KiB
JavaScript
338 lines
12 KiB
JavaScript
/**
|
|
* 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 {InspectedElementContextController} from './Components/InspectedElementContext';
|
|
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>
|
|
<InspectedElementContextController>
|
|
<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>
|
|
</InspectedElementContextController>
|
|
</TimelineContextController>
|
|
</ProfilerContextController>
|
|
</TreeContextController>
|
|
</FetchFileWithCachingContext.Provider>
|
|
</HookNamesModuleLoaderContext.Provider>
|
|
</ViewElementSourceContext.Provider>
|
|
</SettingsContextController>
|
|
<UnsupportedBridgeProtocolDialog />
|
|
{warnIfLegacyBackendDetected && <WarnIfLegacyBackendDetected />}
|
|
{warnIfUnsupportedVersionDetected && <UnsupportedVersionDialog />}
|
|
</ModalDialogContextController>
|
|
</ContextMenuContext.Provider>
|
|
</OptionsContext.Provider>
|
|
</StoreContext.Provider>
|
|
</BridgeContext.Provider>
|
|
);
|
|
}
|