| 1 | /** |
| 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | * |
| 4 | * This source code is licensed under the MIT license found in the |
| 5 | * LICENSE file in the root directory of this source tree. |
| 6 | * |
| 7 | * @flow |
| 8 | */ |
| 9 | |
| 10 | import * as React from 'react'; |
| 11 | import {Fragment, useContext} from 'react'; |
| 12 | |
| 13 | import InspectedElementBadges from '../Components/InspectedElementBadges'; |
| 14 | import {ProfilerContext} from './ProfilerContext'; |
| 15 | import {formatDuration} from './utils'; |
| 16 | import WhatChanged from './WhatChanged'; |
| 17 | import {StoreContext} from '../context'; |
| 18 | |
| 19 | import styles from './HoveredFiberInfo.css'; |
| 20 | |
| 21 | import type {ChartNode} from './FlamegraphChartBuilder'; |
| 22 | |
| 23 | export type TooltipFiberData = { |
| 24 | id: number, |
| 25 | name: string, |
| 26 | }; |
| 27 | |
| 28 | export type Props = { |
| 29 | fiberData: ChartNode, |
| 30 | }; |
| 31 | |
| 32 | export default function HoveredFiberInfo({fiberData}: Props): React.Node { |
| 33 | const {profilerStore} = useContext(StoreContext); |
| 34 | const {rootID, selectedCommitIndex} = useContext(ProfilerContext); |
| 35 | |
| 36 | const {id, name} = fiberData; |
| 37 | const {profilingCache} = profilerStore; |
| 38 | |
| 39 | if (rootID === null || selectedCommitIndex === null) { |
| 40 | return null; |
| 41 | } |
| 42 | |
| 43 | const commitIndices = profilingCache.getFiberCommits({ |
| 44 | fiberID: id, |
| 45 | rootID, |
| 46 | }); |
| 47 | |
| 48 | const {nodes} = profilingCache.getCommitTree({ |
| 49 | rootID, |
| 50 | commitIndex: selectedCommitIndex, |
| 51 | }); |
| 52 | const node = nodes.get(id); |
| 53 | |
| 54 | let renderDurationInfo = null; |
| 55 | let i = 0; |
| 56 | for (i = 0; i < commitIndices.length; i++) { |
| 57 | const commitIndex = commitIndices[i]; |
| 58 | if (selectedCommitIndex === commitIndex) { |
| 59 | const {fiberActualDurations, fiberSelfDurations} = |
| 60 | profilerStore.getCommitData(rootID as any as number, commitIndex); |
| 61 | const actualDuration = fiberActualDurations.get(id) || 0; |
| 62 | const selfDuration = fiberSelfDurations.get(id) || 0; |
| 63 | |
| 64 | renderDurationInfo = ( |
| 65 | <div key={commitIndex} className={styles.CurrentCommit}> |
| 66 | <strong>Duration:</strong> {formatDuration(selfDuration)}ms of{' '} |
| 67 | {formatDuration(actualDuration)}ms |
| 68 | </div> |
| 69 | ); |
| 70 | |
| 71 | break; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | return ( |
| 76 | <Fragment> |
| 77 | <div className={styles.Toolbar}> |
| 78 | <div className={styles.Component}>{name}</div> |
| 79 | |
| 80 | {node != null && ( |
| 81 | <div className={styles.BadgesContainer}> |
| 82 | <InspectedElementBadges |
| 83 | hocDisplayNames={node.hocDisplayNames} |
| 84 | compiledWithForget={node.compiledWithForget} |
| 85 | /> |
| 86 | |
| 87 | {node.compiledWithForget && ( |
| 88 | <div> |
| 89 | ✨ This component has been auto-memoized by the React Compiler. |
| 90 | </div> |
| 91 | )} |
| 92 | </div> |
| 93 | )} |
| 94 | |
| 95 | <div className={styles.Content}> |
| 96 | {renderDurationInfo || <div>Did not client render.</div>} |
| 97 | |
| 98 | <WhatChanged fiberID={id} displayMode="compact" /> |
| 99 | </div> |
| 100 | </div> |
| 101 | </Fragment> |
| 102 | ); |
| 103 | } |