Skip to content

Commit

Permalink
[DevTools] Add Component Highlighting to Profiler (#18745)
Browse files Browse the repository at this point in the history
Co-authored-by: Moji Izadmehr <m.eezadmehr@gmail.com>
Co-authored-by: Brian Vaughn <bvaughn@fb.com>
  • Loading branch information
3 people authored May 18, 2020
1 parent 081b565 commit d897c35
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import NoCommitData from './NoCommitData';
import CommitFlamegraphListItem from './CommitFlamegraphListItem';
import HoveredFiberInfo from './HoveredFiberInfo';
import {scale} from './utils';
import {useHighlightNativeElement} from '../hooks';
import {StoreContext} from '../context';
import {SettingsContext} from '../Settings/SettingsContext';
import Tooltip from './Tooltip';
Expand All @@ -28,7 +29,8 @@ import type {CommitTree} from './types';

export type ItemData = {|
chartData: ChartData,
hoverFiber: (fiberData: TooltipFiberData | null) => void,
onElementMouseEnter: (fiberData: TooltipFiberData) => void,
onElementMouseLeave: () => void,
scaleX: (value: number, fallbackValue: number) => number,
selectedChartNode: ChartNode | null,
selectedChartNodeIndex: number,
Expand Down Expand Up @@ -96,11 +98,16 @@ type Props = {|
|};

function CommitFlamegraph({chartData, commitTree, height, width}: Props) {
const [hoveredFiberData, hoverFiber] = useState<TooltipFiberData | null>(
null,
);
const [
hoveredFiberData,
setHoveredFiberData,
] = useState<TooltipFiberData | null>(null);
const {lineHeight} = useContext(SettingsContext);
const {selectFiber, selectedFiberID} = useContext(ProfilerContext);
const {
highlightNativeElement,
clearHighlightNativeElement,
} = useHighlightNativeElement();

const selectedChartNodeIndex = useMemo<number>(() => {
if (selectedFiberID === null) {
Expand All @@ -123,10 +130,24 @@ function CommitFlamegraph({chartData, commitTree, height, width}: Props) {
return null;
}, [chartData, selectedFiberID, selectedChartNodeIndex]);

const handleElementMouseEnter = useCallback(
({id, name}) => {
highlightNativeElement(id); // Highlight last hovered element.
setHoveredFiberData({id, name}); // Set hovered fiber data for tooltip
},
[highlightNativeElement],
);

const handleElementMouseLeave = useCallback(() => {
clearHighlightNativeElement(); // clear highlighting of element on mouse leave
setHoveredFiberData(null); // clear hovered fiber data for tooltip
}, [clearHighlightNativeElement]);

const itemData = useMemo<ItemData>(
() => ({
chartData,
hoverFiber,
onElementMouseEnter: handleElementMouseEnter,
onElementMouseLeave: handleElementMouseLeave,
scaleX: scale(
0,
selectedChartNode !== null
Expand All @@ -142,7 +163,8 @@ function CommitFlamegraph({chartData, commitTree, height, width}: Props) {
}),
[
chartData,
hoverFiber,
handleElementMouseEnter,
handleElementMouseLeave,
selectedChartNode,
selectedChartNodeIndex,
selectFiber,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ type Props = {
function CommitFlamegraphListItem({data, index, style}: Props) {
const {
chartData,
hoverFiber,
onElementMouseEnter,
onElementMouseLeave,
scaleX,
selectedChartNode,
selectedChartNodeIndex,
Expand All @@ -49,11 +50,11 @@ function CommitFlamegraphListItem({data, index, style}: Props) {

const handleMouseEnter = (nodeData: ChartNodeType) => {
const {id, name} = nodeData;
hoverFiber({id, name});
onElementMouseEnter({id, name});
};

const handleMouseLeave = () => {
hoverFiber(null);
onElementMouseLeave();
};

// List items are absolutely positioned using the CSS "top" attribute.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import HoveredFiberInfo from './HoveredFiberInfo';
import {scale} from './utils';
import {StoreContext} from '../context';
import {SettingsContext} from '../Settings/SettingsContext';
import {useHighlightNativeElement} from '../hooks';
import Tooltip from './Tooltip';

import styles from './CommitRanked.css';
Expand All @@ -28,7 +29,8 @@ import type {CommitTree} from './types';

export type ItemData = {|
chartData: ChartData,
hoverFiber: (fiberData: TooltipFiberData | null) => void,
onElementMouseEnter: (fiberData: TooltipFiberData) => void,
onElementMouseLeave: () => void,
scaleX: (value: number, fallbackValue: number) => number,
selectedFiberID: number | null,
selectedFiberIndex: number,
Expand Down Expand Up @@ -94,28 +96,55 @@ type Props = {|
|};

function CommitRanked({chartData, commitTree, height, width}: Props) {
const [hoveredFiberData, hoverFiber] = useState<TooltipFiberData | null>(
null,
);
const [
hoveredFiberData,
setHoveredFiberData,
] = useState<TooltipFiberData | null>(null);
const {lineHeight} = useContext(SettingsContext);
const {selectedFiberID, selectFiber} = useContext(ProfilerContext);
const {
highlightNativeElement,
clearHighlightNativeElement,
} = useHighlightNativeElement();

const selectedFiberIndex = useMemo(
() => getNodeIndex(chartData, selectedFiberID),
[chartData, selectedFiberID],
);

const handleElementMouseEnter = useCallback(
({id, name}) => {
highlightNativeElement(id); // Highlight last hovered element.
setHoveredFiberData({id, name}); // Set hovered fiber data for tooltip
},
[highlightNativeElement],
);

const handleElementMouseLeave = useCallback(() => {
clearHighlightNativeElement(); // clear highlighting of element on mouse leave
setHoveredFiberData(null); // clear hovered fiber data for tooltip
}, [clearHighlightNativeElement]);

const itemData = useMemo<ItemData>(
() => ({
chartData,
hoverFiber,
onElementMouseEnter: handleElementMouseEnter,
onElementMouseLeave: handleElementMouseLeave,
scaleX: scale(0, chartData.nodes[selectedFiberIndex].value, 0, width),
selectedFiberID,
selectedFiberIndex,
selectFiber,
width,
}),
[chartData, selectedFiberID, selectedFiberIndex, selectFiber, width],
[
chartData,
handleElementMouseEnter,
handleElementMouseLeave,
selectedFiberID,
selectedFiberIndex,
selectFiber,
width,
],
);

// Tooltip used to show summary of fiber info on hover
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ type Props = {
function CommitRankedListItem({data, index, style}: Props) {
const {
chartData,
hoverFiber,
onElementMouseEnter,
onElementMouseLeave,
scaleX,
selectedFiberIndex,
selectFiber,
Expand All @@ -49,11 +50,11 @@ function CommitRankedListItem({data, index, style}: Props) {

const handleMouseEnter = () => {
const {id, name} = node;
hoverFiber({id, name});
onElementMouseEnter({id, name});
};

const handleMouseLeave = () => {
hoverFiber(null);
onElementMouseLeave();
};

// List items are absolutely positioned using the CSS "top" attribute.
Expand Down

0 comments on commit d897c35

Please sign in to comment.