-
Notifications
You must be signed in to change notification settings - Fork 47.9k
/
Copy pathCommitFlamegraphListItem.js
137 lines (119 loc) · 3.72 KB
/
CommitFlamegraphListItem.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/**
* Copyright (c) Facebook, Inc. and its 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 {Fragment, memo, useCallback, useContext} from 'react';
import {areEqual} from 'react-window';
import {barWidthThreshold} from './constants';
import {getGradientColor} from './utils';
import ChartNode from './ChartNode';
import {SettingsContext} from '../Settings/SettingsContext';
import type {ChartNode as ChartNodeType} from './FlamegraphChartBuilder';
import type {ItemData} from './CommitFlamegraph';
type Props = {
data: ItemData,
index: number,
style: Object,
...
};
function CommitFlamegraphListItem({data, index, style}: Props): React.Node {
const {
chartData,
onElementMouseEnter,
onElementMouseLeave,
scaleX,
selectedChartNode,
selectedChartNodeIndex,
selectFiber,
width,
} = data;
const {renderPathNodes, maxSelfDuration, rows} = chartData;
const {lineHeight} = useContext(SettingsContext);
const handleClick = useCallback(
(event: SyntheticMouseEvent<EventTarget>, id: number, name: string) => {
event.stopPropagation();
selectFiber(id, name);
},
[selectFiber],
);
const handleMouseEnter = (nodeData: ChartNodeType) => {
const {id, name} = nodeData;
onElementMouseEnter({id, name});
};
const handleMouseLeave = () => {
onElementMouseLeave();
};
// List items are absolutely positioned using the CSS "top" attribute.
// The "left" value will always be 0.
// Since height is fixed, and width is based on the node's duration,
// We can ignore those values as well.
const top = parseInt(style.top, 10);
const row = rows[index];
const selectedNodeOffset = scaleX(
selectedChartNode !== null ? selectedChartNode.offset : 0,
width,
);
return (
<Fragment>
{row.map(chartNode => {
const {
didRender,
id,
label,
name,
offset,
selfDuration,
treeBaseDuration,
} = chartNode;
const nodeOffset = scaleX(offset, width);
const nodeWidth = scaleX(treeBaseDuration, width);
// Filter out nodes that are too small to see or click.
// This also helps render large trees faster.
if (nodeWidth < barWidthThreshold) {
return null;
}
// Filter out nodes that are outside of the horizontal window.
if (
nodeOffset + nodeWidth < selectedNodeOffset ||
nodeOffset > selectedNodeOffset + width
) {
return null;
}
let color = 'url(#didNotRenderPattern)';
let textColor = 'var(--color-commit-did-not-render-pattern-text)';
if (didRender) {
color = getGradientColor(selfDuration / maxSelfDuration);
textColor = 'var(--color-commit-gradient-text)';
} else if (renderPathNodes.has(id)) {
color = 'var(--color-commit-did-not-render-fill)';
textColor = 'var(--color-commit-did-not-render-fill-text)';
}
return (
<ChartNode
color={color}
height={lineHeight}
isDimmed={index < selectedChartNodeIndex}
key={id}
label={label}
onClick={event => handleClick(event, id, name)}
onMouseEnter={() => handleMouseEnter(chartNode)}
onMouseLeave={handleMouseLeave}
textStyle={{color: textColor}}
width={nodeWidth}
x={nodeOffset - selectedNodeOffset}
y={top}
/>
);
})}
</Fragment>
);
}
export default (memo(
CommitFlamegraphListItem,
areEqual,
): React.ComponentType<Props>);