-
Notifications
You must be signed in to change notification settings - Fork 51.1k
Expand file tree
/
Copy pathSuspenseScrubber.js
More file actions
127 lines (119 loc) · 3.67 KB
/
SuspenseScrubber.js
File metadata and controls
127 lines (119 loc) · 3.67 KB
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
/**
* 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 type {SuspenseTimelineStep} from 'react-devtools-shared/src/frontend/types';
import typeof {SyntheticEvent} from 'react-dom-bindings/src/events/SyntheticEvent';
import * as React from 'react';
import {useContext, useRef} from 'react';
import {ElementTypeRoot} from 'react-devtools-shared/src/frontend/types';
import styles from './SuspenseScrubber.css';
import {getClassNameForEnvironment} from './SuspenseEnvironmentColors.js';
import Tooltip from '../Components/reach-ui/tooltip';
import {StoreContext} from '../context';
export default function SuspenseScrubber({
min,
max,
timeline,
value,
highlight,
onBlur,
onChange,
onFocus,
onHoverSegment,
onHoverLeave,
}: {
min: number,
max: number,
timeline: $ReadOnlyArray<SuspenseTimelineStep>,
value: number,
highlight: number,
onBlur?: () => void,
onChange: (index: number) => void,
onFocus?: () => void,
onHoverSegment: (index: number) => void,
onHoverLeave: () => void,
}): React$Node {
const store = useContext(StoreContext);
const inputRef = useRef();
function handleChange(event: SyntheticEvent) {
const newValue = +event.currentTarget.value;
onChange(newValue);
}
function handlePress(index: number, event: SyntheticEvent) {
event.preventDefault();
if (inputRef.current == null) {
throw new Error(
'The input should always be mounted while we can click things.',
);
}
inputRef.current.focus();
onChange(index);
}
const steps = [];
for (let index = min; index <= max; index++) {
const step = timeline[index];
const environment = step.environment;
const element = store.getElementByID(step.id);
const label =
index === min
? // The first step in the timeline is always a Transition (Initial Paint).
element === null || element.type === ElementTypeRoot
? 'Initial Paint'
: 'Transition' +
(environment === null ? '' : ' (' + environment + ')')
: // TODO: Consider adding the name of this specific boundary if this step has only one.
environment === null
? 'Suspense'
: environment;
steps.push(
<Tooltip key={index} label={label}>
<div
className={
styles.SuspenseScrubberStep +
(highlight === index
? ' ' + styles.SuspenseScrubberStepHighlight
: '')
}
onPointerDown={handlePress.bind(null, index)}
onMouseEnter={onHoverSegment.bind(null, index)}>
<div
className={
styles.SuspenseScrubberBead +
(index === min
? // The first step in the timeline is always a Transition (Initial Paint).
' ' + styles.SuspenseScrubberBeadTransition
: '') +
(!step.hasUniqueSuspenders
? ' ' + styles.SuspenseScrubberBeadNotSuspended
: '') +
' ' +
getClassNameForEnvironment(environment) +
(index <= value ? ' ' + styles.SuspenseScrubberBeadSelected : '')
}
/>
</div>
</Tooltip>,
);
}
return (
<div className={styles.SuspenseScrubber} onMouseLeave={onHoverLeave}>
<input
className={styles.SuspenseScrubberInput}
type="range"
min={min}
max={max}
value={value}
onBlur={onBlur}
onChange={handleChange}
onFocus={onFocus}
ref={inputRef}
/>
{steps}
</div>
);
}