-
Notifications
You must be signed in to change notification settings - Fork 49.6k
[DevTools] feat: show changed hooks names in the Profiler tab #31398
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+327
−59
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0197f26
refactor: move InspectedElementContext to enable reuse of hook names …
piotrski 03a62bc
feat(profiler): display names of changed hooks in the Profiler tab
piotrski e5ea078
feat(profiler): add display mode option to preserve hook change summa…
piotrski 47b662a
types(profiler): fix Flow types in HookChangeSummary
piotrski e24324f
refactor(profiler): simplify key handling, subHooks filtering, and ho…
piotrski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
packages/react-devtools-shared/src/devtools/views/Profiler/HookChangeSummary.css
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
.LoadHookNamesToggle, | ||
.ToggleError { | ||
padding: 2px; | ||
background: none; | ||
border: none; | ||
cursor: pointer; | ||
position: relative; | ||
bottom: -0.2em; | ||
margin-block: -1em; | ||
} | ||
|
||
.ToggleError { | ||
color: var(--color-error-text); | ||
} | ||
|
||
.Hook { | ||
list-style-type: none; | ||
margin: 0; | ||
padding-left: 0.5rem; | ||
line-height: 1.125rem; | ||
|
||
font-family: var(--font-family-monospace); | ||
font-size: var(--font-size-monospace-normal); | ||
} | ||
|
||
.Hook .Hook { | ||
padding-left: 1rem; | ||
} | ||
|
||
.Name { | ||
color: var(--color-dim); | ||
flex: 0 0 auto; | ||
cursor: default; | ||
} | ||
|
||
.PrimitiveHookName { | ||
color: var(--color-text); | ||
flex: 0 0 auto; | ||
cursor: default; | ||
} | ||
|
||
.Name:after { | ||
color: var(--color-text); | ||
content: ': '; | ||
margin-right: 0.5rem; | ||
} | ||
|
||
.PrimitiveHookNumber { | ||
background-color: var(--color-primitive-hook-badge-background); | ||
color: var(--color-primitive-hook-badge-text); | ||
font-size: var(--font-size-monospace-small); | ||
margin-right: 0.25rem; | ||
border-radius: 0.125rem; | ||
padding: 0.125rem 0.25rem; | ||
} | ||
|
||
.HookName { | ||
color: var(--color-component-name); | ||
} |
207 changes: 207 additions & 0 deletions
207
packages/react-devtools-shared/src/devtools/views/Profiler/HookChangeSummary.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,207 @@ | ||
/** | ||
* 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 | ||
*/ | ||
|
||
import * as React from 'react'; | ||
import { | ||
useContext, | ||
useMemo, | ||
useCallback, | ||
memo, | ||
useState, | ||
useEffect, | ||
} from 'react'; | ||
import styles from './HookChangeSummary.css'; | ||
import ButtonIcon from '../ButtonIcon'; | ||
import {InspectedElementContext} from '../Components/InspectedElementContext'; | ||
import {StoreContext} from '../context'; | ||
|
||
import { | ||
getAlreadyLoadedHookNames, | ||
getHookSourceLocationKey, | ||
} from 'react-devtools-shared/src/hookNamesCache'; | ||
import Toggle from '../Toggle'; | ||
import type {HooksNode} from 'react-debug-tools/src/ReactDebugHooks'; | ||
import type {ChangeDescription} from './types'; | ||
|
||
// $FlowFixMe: Flow doesn't know about Intl.ListFormat | ||
const hookListFormatter = new Intl.ListFormat('en', { | ||
style: 'long', | ||
type: 'conjunction', | ||
}); | ||
|
||
type HookProps = { | ||
hook: HooksNode, | ||
hookNames: Map<string, string> | null, | ||
}; | ||
|
||
const Hook: React.AbstractComponent<HookProps> = memo(({hook, hookNames}) => { | ||
const hookSource = hook.hookSource; | ||
const hookName = useMemo(() => { | ||
if (!hookSource || !hookNames) return null; | ||
const key = getHookSourceLocationKey(hookSource); | ||
return hookNames.get(key) || null; | ||
}, [hookSource, hookNames]); | ||
|
||
return ( | ||
<ul className={styles.Hook}> | ||
<li> | ||
{hook.id !== null && ( | ||
<span className={styles.PrimitiveHookNumber}> | ||
{String(hook.id + 1)} | ||
</span> | ||
)} | ||
<span | ||
className={hook.id !== null ? styles.PrimitiveHookName : styles.Name}> | ||
{hook.name} | ||
{hookName && <span className={styles.HookName}>({hookName})</span>} | ||
</span> | ||
{hook.subHooks?.map((subHook, index) => ( | ||
<Hook key={hook.id} hook={subHook} hookNames={hookNames} /> | ||
))} | ||
</li> | ||
</ul> | ||
); | ||
}); | ||
|
||
const shouldKeepHook = ( | ||
hook: HooksNode, | ||
hooksArray: Array<number>, | ||
): boolean => { | ||
if (hook.id !== null && hooksArray.includes(hook.id)) { | ||
return true; | ||
} | ||
const subHooks = hook.subHooks; | ||
if (subHooks == null) { | ||
return false; | ||
} | ||
|
||
return subHooks.some(subHook => shouldKeepHook(subHook, hooksArray)); | ||
}; | ||
|
||
const filterHooks = ( | ||
hook: HooksNode, | ||
hooksArray: Array<number>, | ||
): HooksNode | null => { | ||
if (!shouldKeepHook(hook, hooksArray)) { | ||
return null; | ||
} | ||
|
||
const subHooks = hook.subHooks; | ||
if (subHooks == null) { | ||
return hook; | ||
} | ||
|
||
const filteredSubHooks = subHooks | ||
.map(subHook => filterHooks(subHook, hooksArray)) | ||
.filter(Boolean); | ||
return filteredSubHooks.length > 0 | ||
? {...hook, subHooks: filteredSubHooks} | ||
: hook; | ||
}; | ||
|
||
type Props = {| | ||
fiberID: number, | ||
hooks: $PropertyType<ChangeDescription, 'hooks'>, | ||
state: $PropertyType<ChangeDescription, 'state'>, | ||
displayMode?: 'detailed' | 'compact', | ||
|}; | ||
|
||
const HookChangeSummary: React.AbstractComponent<Props> = memo( | ||
({hooks, fiberID, state, displayMode = 'detailed'}: Props) => { | ||
const {parseHookNames, toggleParseHookNames, inspectedElement} = useContext( | ||
InspectedElementContext, | ||
); | ||
const store = useContext(StoreContext); | ||
|
||
const [parseHookNamesOptimistic, setParseHookNamesOptimistic] = | ||
useState<boolean>(parseHookNames); | ||
|
||
useEffect(() => { | ||
setParseHookNamesOptimistic(parseHookNames); | ||
}, [inspectedElement?.id, parseHookNames]); | ||
|
||
const handleOnChange = useCallback(() => { | ||
setParseHookNamesOptimistic(!parseHookNames); | ||
toggleParseHookNames(); | ||
}, [toggleParseHookNames, parseHookNames]); | ||
|
||
const element = fiberID !== null ? store.getElementByID(fiberID) : null; | ||
const hookNames = | ||
element != null ? getAlreadyLoadedHookNames(element) : null; | ||
|
||
const filteredHooks = useMemo(() => { | ||
if (!hooks || !inspectedElement?.hooks) return null; | ||
return inspectedElement.hooks | ||
.map(hook => filterHooks(hook, hooks)) | ||
.filter(Boolean); | ||
}, [inspectedElement?.hooks, hooks]); | ||
|
||
const hookParsingFailed = parseHookNames && hookNames === null; | ||
|
||
if (!hooks?.length) { | ||
return <span>No hooks changed</span>; | ||
} | ||
|
||
if ( | ||
inspectedElement?.id !== element?.id || | ||
filteredHooks?.length !== hooks.length || | ||
displayMode === 'compact' | ||
) { | ||
const hookIds = hooks.map(hookId => String(hookId + 1)); | ||
const hookWord = hookIds.length === 1 ? '• Hook' : '• Hooks'; | ||
return ( | ||
<span> | ||
{hookWord} {hookListFormatter.format(hookIds)} changed | ||
</span> | ||
); | ||
} | ||
|
||
let toggleTitle: string; | ||
if (hookParsingFailed) { | ||
toggleTitle = 'Hook parsing failed'; | ||
} else if (parseHookNamesOptimistic) { | ||
toggleTitle = 'Parsing hook names ...'; | ||
} else { | ||
toggleTitle = 'Parse hook names (may be slow)'; | ||
} | ||
|
||
if (filteredHooks == null) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<div> | ||
{filteredHooks.length > 1 ? '• Hooks changed:' : '• Hook changed:'} | ||
{(!parseHookNames || hookParsingFailed) && ( | ||
<Toggle | ||
className={ | ||
hookParsingFailed | ||
? styles.ToggleError | ||
: styles.LoadHookNamesToggle | ||
} | ||
isChecked={parseHookNamesOptimistic} | ||
isDisabled={parseHookNamesOptimistic || hookParsingFailed} | ||
onChange={handleOnChange} | ||
title={toggleTitle}> | ||
<ButtonIcon type="parse-hook-names" /> | ||
</Toggle> | ||
)} | ||
{filteredHooks.map(hook => ( | ||
<Hook | ||
key={`${inspectedElement?.id ?? 'unknown'}-${hook.id}`} | ||
hook={hook} | ||
hookNames={hookNames} | ||
/> | ||
))} | ||
</div> | ||
); | ||
}, | ||
); | ||
|
||
export default HookChangeSummary; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.