|
7 | 7 | * @flow |
8 | 8 | */ |
9 | 9 |
|
| 10 | +import type {SchedulingEvent} from 'react-devtools-timeline/src/types'; |
| 11 | + |
10 | 12 | import * as React from 'react'; |
11 | 13 | import {isStateUpdateEvent} from 'react-devtools-timeline/src/utils/flow'; |
12 | 14 | import Button from '../Button'; |
13 | 15 | import ButtonIcon from '../ButtonIcon'; |
14 | 16 | import ViewSourceContext from '../Components/ViewSourceContext'; |
15 | 17 | import {useContext, useMemo} from 'react'; |
16 | | -import {ProfilerContext} from './ProfilerContext'; |
| 18 | +import {TimelineContext} from 'react-devtools-timeline/src/TimelineContext'; |
17 | 19 | import {stackToComponentSources} from 'react-devtools-shared/src/devtools/utils'; |
18 | 20 |
|
19 | 21 | import styles from './SidebarEventInfo.css'; |
20 | 22 |
|
21 | 23 | export type Props = {||}; |
22 | 24 |
|
23 | | -export default function SidebarEventInfo(_: Props) { |
24 | | - const {profilingData, selectedCommitIndex} = useContext(ProfilerContext); |
| 25 | +function SchedulingEventInfo({eventInfo}: {eventInfo: SchedulingEvent}) { |
25 | 26 | const {viewUrlSourceFunction} = useContext(ViewSourceContext); |
26 | 27 |
|
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; |
36 | 31 |
|
37 | | - const event = schedulingEvents[selectedCommitIndex]; |
38 | | - if (!isStateUpdateEvent(event)) { |
39 | | - return {}; |
| 32 | + const viewSource = source => { |
| 33 | + if (viewUrlSourceFunction != null && source != null) { |
| 34 | + viewUrlSourceFunction(...source); |
40 | 35 | } |
| 36 | + }; |
41 | 37 |
|
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; |
62 | 44 |
|
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 | +} |
77 | 68 |
|
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 ? ( |
79 | 73 | <> |
80 | 74 | <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} |
84 | 78 | </> |
85 | | - ); |
| 79 | + ) : null; |
86 | 80 | } |
0 commit comments