Skip to content

Commit d5ea483

Browse files
committed
fix bugs
1 parent cd80d32 commit d5ea483

File tree

5 files changed

+75
-68
lines changed

5 files changed

+75
-68
lines changed

packages/react-devtools-shared/src/devtools/views/Profiler/SidebarEventInfo.js

Lines changed: 48 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -7,80 +7,74 @@
77
* @flow
88
*/
99

10+
import type {SchedulingEvent} from 'react-devtools-timeline/src/types';
11+
1012
import * as React from 'react';
1113
import {isStateUpdateEvent} from 'react-devtools-timeline/src/utils/flow';
1214
import Button from '../Button';
1315
import ButtonIcon from '../ButtonIcon';
1416
import ViewSourceContext from '../Components/ViewSourceContext';
1517
import {useContext, useMemo} from 'react';
16-
import {ProfilerContext} from './ProfilerContext';
18+
import {TimelineContext} from 'react-devtools-timeline/src/TimelineContext';
1719
import {stackToComponentSources} from 'react-devtools-shared/src/devtools/utils';
1820

1921
import styles from './SidebarEventInfo.css';
2022

2123
export type Props = {||};
2224

23-
export default function SidebarEventInfo(_: Props) {
24-
const {profilingData, selectedCommitIndex} = useContext(ProfilerContext);
25+
function SchedulingEventInfo({eventInfo}: {eventInfo: SchedulingEvent}) {
2526
const {viewUrlSourceFunction} = useContext(ViewSourceContext);
2627

27-
const {stack} = useMemo(() => {
28-
if (
29-
selectedCommitIndex == null ||
30-
profilingData == null ||
31-
profilingData.timelineData.length === 0
32-
) {
33-
return {};
34-
}
35-
const {schedulingEvents} = profilingData.timelineData[0];
28+
const componentStack = eventInfo.componentStack
29+
? stackToComponentSources(eventInfo.componentStack)
30+
: null;
3631

37-
const event = schedulingEvents[selectedCommitIndex];
38-
if (!isStateUpdateEvent(event)) {
39-
return {};
32+
const viewSource = source => {
33+
if (viewUrlSourceFunction != null && source != null) {
34+
viewUrlSourceFunction(...source);
4035
}
36+
};
4137

42-
let componentStack = null;
43-
if (event.componentStack) {
44-
componentStack = stackToComponentSources(event.componentStack);
45-
}
46-
47-
return {
48-
stack: componentStack,
49-
};
50-
}, [profilingData, selectedCommitIndex]);
51-
52-
let components;
53-
if (stack) {
54-
components = stack.map(([displayName, source], index) => {
55-
const hasSource = source != null;
56-
57-
const onClick = () => {
58-
if (viewUrlSourceFunction != null && source != null) {
59-
viewUrlSourceFunction(...source);
60-
}
61-
};
38+
return (
39+
<div className={styles.Content} tabIndex={0}>
40+
{componentStack ? (
41+
<ol className={styles.List}>
42+
{componentStack.map(([displayName, source], index) => {
43+
const hasSource = source != null;
6244

63-
return (
64-
<li key={index} className={styles.ListItem} data-source={hasSource}>
65-
<label className={styles.Label}>
66-
<Button className={styles.Button} onClick={onClick}>
67-
{displayName}
68-
</Button>
69-
{hasSource && (
70-
<ButtonIcon className={styles.Source} type="view-source" />
71-
)}
72-
</label>
73-
</li>
74-
);
75-
});
76-
}
45+
return (
46+
<li
47+
key={index}
48+
className={styles.ListItem}
49+
data-source={hasSource}>
50+
<label className={styles.Label}>
51+
<Button
52+
className={styles.Button}
53+
onClick={() => viewSource(source)}>
54+
{displayName}
55+
</Button>
56+
{hasSource && (
57+
<ButtonIcon className={styles.Source} type="view-source" />
58+
)}
59+
</label>
60+
</li>
61+
);
62+
})}
63+
</ol>
64+
) : null}
65+
</div>
66+
);
67+
}
7768

78-
return (
69+
export default function SidebarEventInfo(_: Props) {
70+
const {selectedEvent} = useContext(TimelineContext);
71+
// (TODO) Refactor in next PR so this supports multiple types of events
72+
return selectedEvent ? (
7973
<>
8074
<div className={styles.Toolbar}>Event Component Tree</div>
81-
<div className={styles.Content} tabIndex={0}>
82-
<ol className={styles.List}>{components}</ol>
83-
</div>
75+
{selectedEvent.schedulingEvent ? (
76+
<SchedulingEventInfo eventInfo={selectedEvent.schedulingEvent} />
77+
) : null}
8478
</>
85-
);
79+
) : null;
8680
}

packages/react-devtools-timeline/src/CanvasPage.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
import type {Point} from './view-base';
1111
import type {
12-
ReactHoverContextInfo,
12+
ReactEventInfo,
1313
TimelineData,
1414
ReactMeasure,
1515
ViewState,
@@ -63,7 +63,7 @@ import useContextMenu from 'react-devtools-shared/src/devtools/ContextMenu/useCo
6363
import {getBatchRange} from './utils/getBatchRange';
6464
import {MAX_ZOOM_LEVEL, MIN_ZOOM_LEVEL} from './view-base/constants';
6565
import {TimelineSearchContext} from './TimelineSearchContext';
66-
import {ProfilerContext} from 'react-devtools-shared/src/devtools/views/Profiler/ProfilerContext';
66+
import {TimelineContext} from './TimelineContext';
6767

6868
import styles from './CanvasPage.css';
6969

@@ -132,7 +132,7 @@ const zoomToBatch = (
132132
viewState.updateHorizontalScrollState(scrollState);
133133
};
134134

135-
const EMPTY_CONTEXT_INFO: ReactHoverContextInfo = {
135+
const EMPTY_CONTEXT_INFO: ReactEventInfo = {
136136
componentMeasure: null,
137137
flamechartStackFrame: null,
138138
measure: null,
@@ -162,10 +162,7 @@ function AutoSizedCanvas({
162162

163163
const [isContextMenuShown, setIsContextMenuShown] = useState<boolean>(false);
164164
const [mouseLocation, setMouseLocation] = useState<Point>(zeroPoint); // DOM coordinates
165-
const [
166-
hoveredEvent,
167-
setHoveredEvent,
168-
] = useState<ReactHoverContextInfo | null>(null);
165+
const [hoveredEvent, setHoveredEvent] = useState<ReactEventInfo | null>(null);
169166

170167
const resetHoveredEvent = useCallback(
171168
() => setHoveredEvent(EMPTY_CONTEXT_INFO),
@@ -529,7 +526,7 @@ function AutoSizedCanvas({
529526
ref: canvasRef,
530527
});
531528

532-
const {selectCommitIndex} = useContext(ProfilerContext);
529+
const {selectEvent} = useContext(TimelineContext);
533530

534531
useEffect(() => {
535532
const {current: userTimingMarksView} = userTimingMarksViewRef;
@@ -566,8 +563,11 @@ function AutoSizedCanvas({
566563
});
567564
}
568565
};
569-
schedulingEventsView.onClick = (schedulingEvent, eventIndex) => {
570-
selectCommitIndex(eventIndex);
566+
schedulingEventsView.onClick = schedulingEvent => {
567+
selectEvent({
568+
...EMPTY_CONTEXT_INFO,
569+
schedulingEvent,
570+
});
571571
};
572572
}
573573

packages/react-devtools-timeline/src/EventTooltip.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import type {
1313
NativeEvent,
1414
NetworkMeasure,
1515
ReactComponentMeasure,
16-
ReactHoverContextInfo,
16+
ReactEventInfo,
1717
ReactMeasure,
1818
TimelineData,
1919
SchedulingEvent,
@@ -35,7 +35,7 @@ type Props = {|
3535
canvasRef: {|current: HTMLCanvasElement | null|},
3636
data: TimelineData,
3737
height: number,
38-
hoveredEvent: ReactHoverContextInfo | null,
38+
hoveredEvent: ReactEventInfo | null,
3939
origin: Point,
4040
width: number,
4141
|};

packages/react-devtools-timeline/src/TimelineContext.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import type {
2323
TimelineData,
2424
SearchRegExpStateChangeCallback,
2525
ViewState,
26+
ReactEventInfo,
2627
} from './types';
2728
import type {RefObject} from 'shared/ReactTypes';
2829

@@ -121,6 +122,8 @@ function TimelineContextController({children}: Props) {
121122
return state;
122123
}, [file]);
123124

125+
const [selectedEvent, selectEvent] = useState<ReactEventInfo | null>(null);
126+
124127
const value = useMemo(
125128
() => ({
126129
file,
@@ -129,8 +132,18 @@ function TimelineContextController({children}: Props) {
129132
searchInputContainerRef,
130133
setFile,
131134
viewState,
135+
selectEvent,
136+
selectedEvent,
132137
}),
133-
[file, inMemoryTimelineData, isTimelineSupported, setFile, viewState],
138+
[
139+
file,
140+
inMemoryTimelineData,
141+
isTimelineSupported,
142+
setFile,
143+
viewState,
144+
selectEvent,
145+
selectedEvent,
146+
],
134147
);
135148

136149
return (

packages/react-devtools-timeline/src/types.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ export type TimelineDataExport = {|
240240
thrownErrors: ThrownError[],
241241
|};
242242

243-
export type ReactHoverContextInfo = {|
243+
export type ReactEventInfo = {|
244244
componentMeasure: ReactComponentMeasure | null,
245245
flamechartStackFrame: FlamechartStackFrame | null,
246246
measure: ReactMeasure | null,

0 commit comments

Comments
 (0)