/** * 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 { forwardRef, useCallback, useContext, useLayoutEffect, useMemo, useRef, useState, } from 'react'; import AutoSizer from 'react-virtualized-auto-sizer'; import {FixedSizeList} from 'react-window'; import {ProfilerContext} from './ProfilerContext'; import NoCommitData from './NoCommitData'; import CommitFlamegraphListItem from './CommitFlamegraphListItem'; import HoveredFiberInfo from './HoveredFiberInfo'; import {scale} from './utils'; import {createRegExp} from '../utils'; import {useHighlightHostInstance} from '../hooks'; import {StoreContext} from '../context'; import {SettingsContext} from '../Settings/SettingsContext'; import Tooltip from './Tooltip'; import styles from './CommitFlamegraph.css'; import type {TooltipFiberData} from './HoveredFiberInfo'; import type {ChartData, ChartNode} from './FlamegraphChartBuilder'; import type {CommitTree} from './types'; export type ItemData = { chartData: ChartData, currentSearchMatchID: number | null, matchedFiberIDs: Set, onElementMouseEnter: (fiberData: TooltipFiberData) => void, onElementMouseLeave: () => void, scaleX: (value: number, fallbackValue: number) => number, searchRegExp: RegExp | null, selectedChartNode: ChartNode | null, selectedChartNodeIndex: number, selectFiber: (id: number | null, name: string | null) => void, width: number, }; export default function CommitFlamegraphAutoSizer(_: {}): React.Node { const {profilerStore} = useContext(StoreContext); const {rootID, selectedCommitIndex, selectFiber} = useContext(ProfilerContext); const {profilingCache} = profilerStore; const deselectCurrentFiber = useCallback( (event: $FlowFixMe) => { event.stopPropagation(); selectFiber(null, null); }, [selectFiber], ); let commitTree: CommitTree | null = null; let chartData: ChartData | null = null; if (selectedCommitIndex !== null) { commitTree = profilingCache.getCommitTree({ commitIndex: selectedCommitIndex, rootID: rootID as any as number, }); chartData = profilingCache.getFlamegraphChartData({ commitIndex: selectedCommitIndex, commitTree, rootID: rootID as any as number, }); } if (commitTree != null && chartData != null && chartData.depth > 0) { return (
{({height, width}) => ( // Force Flow types to avoid checking for `null` here because there's no static proof that // by the time this render prop function is called, the values of the `let` variables have not changed. )}
); } else { return ; } } type Props = { chartData: ChartData, commitTree: CommitTree, height: number, width: number, }; function CommitFlamegraph({chartData, commitTree, height, width}: Props) { const [hoveredFiberData, setHoveredFiberData] = useState(null); const {lineHeight} = useContext(SettingsContext); const {selectFiber, selectedFiberID, searchText, searchResults, searchIndex} = useContext(ProfilerContext); const {highlightHostInstance, clearHighlightHostInstance} = useHighlightHostInstance(); // Search highlighting: the regexp to highlight, the set of matching fibers, // and the id of the current match (highlighted more prominently). const searchRegExp = useMemo( () => (searchText === '' ? null : createRegExp(searchText)), [searchText], ); const matchedFiberIDs = useMemo( () => new Set(searchResults.map(result => result.id)), [searchResults], ); const currentSearchMatchID = searchIndex >= 0 && searchIndex < searchResults.length ? searchResults[searchIndex].id : null; const selectedChartNodeIndex = useMemo(() => { if (selectedFiberID === null) { return 0; } // The selected node might not be in the tree for this commit, // so it's important that we have a fallback plan. const depth = chartData.idToDepthMap.get(selectedFiberID); return depth !== undefined ? depth - 1 : 0; }, [chartData, selectedFiberID]); const selectedChartNode = useMemo(() => { if (selectedFiberID !== null) { return ( chartData.rows[selectedChartNodeIndex].find( chartNode => chartNode.id === selectedFiberID, ) || null ); } return null; }, [chartData, selectedFiberID, selectedChartNodeIndex]); const handleElementMouseEnter = useCallback( ({id, name}: $FlowFixMe) => { highlightHostInstance(id); // Highlight last hovered element. setHoveredFiberData({id, name}); // Set hovered fiber data for tooltip }, [highlightHostInstance], ); const handleElementMouseLeave = useCallback(() => { clearHighlightHostInstance(); // clear highlighting of element on mouse leave setHoveredFiberData(null); // clear hovered fiber data for tooltip }, [clearHighlightHostInstance]); const itemData = useMemo( () => ({ chartData, currentSearchMatchID, matchedFiberIDs, onElementMouseEnter: handleElementMouseEnter, onElementMouseLeave: handleElementMouseLeave, scaleX: scale( 0, selectedChartNode !== null ? selectedChartNode.treeBaseDuration : chartData.baseDuration, 0, width, ), searchRegExp, selectedChartNode, selectedChartNodeIndex, selectFiber, width, }), [ chartData, currentSearchMatchID, matchedFiberIDs, handleElementMouseEnter, handleElementMouseLeave, searchRegExp, selectedChartNode, selectedChartNodeIndex, selectFiber, width, ], ); // Tooltip used to show summary of fiber info on hover const tooltipLabel = useMemo( () => hoveredFiberData !== null ? ( ) : null, [hoveredFiberData], ); // Scroll the selected fiber's row into view when the selection changes (e.g. // when navigating between search results). Selection is driven externally // (search nav in ProfilerContext, or a node click) and selectedChartNodeIndex // is derived here — no local event handler sets it — so we sync the imperative // scroll in a layout effect, which runs before paint to avoid a frame where // the scroll position lags the selection. const listRef = useRef(null); const itemIsSelected = selectedFiberID !== null; useLayoutEffect(() => { // selectedChartNodeIndex falls back to 0 when nothing is selected, so only // scroll when a fiber is actually selected. if (itemIsSelected && listRef.current !== null) { listRef.current.scrollToItem(selectedChartNodeIndex, 'smart'); } }, [itemIsSelected, selectedChartNodeIndex]); return ( {CommitFlamegraphListItem} ); } const InnerElementType = forwardRef(({children, ...rest}, ref) => ( {children} ));