-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathgridLines.tsx
227 lines (193 loc) · 6.25 KB
/
gridLines.tsx
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import {useCallback} from 'react';
import styled from '@emotion/styled';
import {mergeRefs} from '@react-aria/utils';
import moment from 'moment-timezone';
import {updateDateTime} from 'sentry/actionCreators/pageFilters';
import {DateTime} from 'sentry/components/dateTime';
import {space} from 'sentry/styles/space';
import useRouter from 'sentry/utils/useRouter';
import {useTimelineCursor} from './timelineCursor';
import {useTimelineZoom} from './timelineZoom';
import type {TimeWindowConfig} from './types';
interface Props {
timeWindowConfig: TimeWindowConfig;
/**
* Render additional UI components inside of the grid lines overlay
*/
additionalUi?: React.ReactNode;
/**
* Enable zoom selection
*/
allowZoom?: boolean;
className?: string;
/**
* Enable the timeline cursor
*/
showCursor?: boolean;
/**
* Enabling causes the cursor tooltip to stick to the top of the viewport.
*/
stickyCursor?: boolean;
}
interface TimeMarker {
date: Date;
/**
* Props to pass to the DateTime component
*/
dateTimeProps: TimeWindowConfig['dateTimeProps'];
/**
* The position in pixels of the tick
*/
position: number;
}
/**
* Aligns the given date to the start of a unit (minute, hour, day) based on
* the minuteInterval size. This will align to the right side of the boundary
*
* 01:53:43 (10m interval) => 01:54:00
* 01:32:00 (2hr interval) => 02:00:00
*/
function alignDateToBoundary(date: moment.Moment, minuteInterval: number) {
if (minuteInterval < 60) {
return date.minute(date.minutes() - (date.minutes() % minuteInterval)).seconds(0);
}
if (minuteInterval < 60 * 24) {
return date.startOf('hour');
}
return date.startOf('day');
}
function getTimeMarkersFromConfig(config: TimeWindowConfig) {
const {start, end, elapsedMinutes, intervals, dateTimeProps, timelineWidth} = config;
const {referenceMarkerInterval, minimumMarkerInterval, normalMarkerInterval} =
intervals;
const msPerPixel = (elapsedMinutes * 60 * 1000) / timelineWidth;
// The first marker will always be the starting time. This always renders the
// full date and time
const markers: TimeMarker[] = [
{
date: start,
position: 0,
dateTimeProps: {timeZone: true},
},
];
// The mark after the first mark will be aligned to a boundary to make it
// easier to understand the rest of the marks
const currentMark = alignDateToBoundary(moment(start), normalMarkerInterval);
// The first label is larger since we include the date, time, and timezone.
while (currentMark.isBefore(moment(start).add(referenceMarkerInterval, 'minutes'))) {
currentMark.add(normalMarkerInterval, 'minute');
}
// Generate time markers which represent location of grid lines/time labels.
// Stop adding markers once there's no more room for more markers
while (moment(currentMark).add(minimumMarkerInterval, 'minutes').isBefore(end)) {
const position = (currentMark.valueOf() - start.valueOf()) / msPerPixel;
markers.push({date: currentMark.toDate(), position, dateTimeProps});
currentMark.add(normalMarkerInterval, 'minutes');
}
return markers;
}
export function GridLineLabels({timeWindowConfig, className}: Props) {
const markers = getTimeMarkersFromConfig(timeWindowConfig);
return (
<LabelsContainer aria-hidden className={className}>
{markers.map(({date, position, dateTimeProps}) => (
<TimeLabelContainer key={date.getTime()} left={position}>
<TimeLabel date={date} {...dateTimeProps} />
</TimeLabelContainer>
))}
</LabelsContainer>
);
}
export function GridLineOverlay({
timeWindowConfig,
showCursor,
additionalUi,
stickyCursor,
allowZoom,
className,
}: Props) {
const router = useRouter();
const {start, timelineWidth, dateLabelFormat} = timeWindowConfig;
const msPerPixel = (timeWindowConfig.elapsedMinutes * 60 * 1000) / timelineWidth;
const dateFromPosition = useCallback(
(position: number) => moment(start.getTime() + msPerPixel * position),
[msPerPixel, start]
);
const makeCursorLabel = useCallback(
(position: number) => dateFromPosition(position).format(dateLabelFormat),
[dateFromPosition, dateLabelFormat]
);
const handleZoom = useCallback(
(startX: number, endX: number) =>
updateDateTime(
{
start: dateFromPosition(startX).startOf('minute').toDate(),
end: dateFromPosition(endX).add(1, 'minute').startOf('minute').toDate(),
},
router
),
[dateFromPosition, router]
);
const {
selectionContainerRef,
timelineSelector,
isActive: selectionIsActive,
} = useTimelineZoom<HTMLDivElement>({enabled: !!allowZoom, onSelect: handleZoom});
const {cursorContainerRef, timelineCursor} = useTimelineCursor<HTMLDivElement>({
enabled: !!showCursor && !selectionIsActive,
sticky: stickyCursor,
labelText: makeCursorLabel,
});
const overlayRef = mergeRefs(cursorContainerRef, selectionContainerRef);
const markers = getTimeMarkersFromConfig(timeWindowConfig);
// Skip first gridline, this will be represented as a border on the
// LabelsContainer
markers.shift();
return (
<Overlay aria-hidden ref={overlayRef} className={className}>
{timelineCursor}
{timelineSelector}
{additionalUi}
<GridLineContainer>
{markers.map(({date, position}) => (
<Gridline key={date.getTime()} left={position} />
))}
</GridLineContainer>
</Overlay>
);
}
const Overlay = styled('div')`
height: 100%;
width: 100%;
position: absolute;
`;
const GridLineContainer = styled('div')`
position: relative;
height: 100%;
z-index: 1;
pointer-events: none;
`;
const LabelsContainer = styled('div')`
height: 50px;
box-shadow: -1px 0 0 ${p => p.theme.translucentInnerBorder};
position: relative;
align-self: stretch;
`;
const Gridline = styled('div')<{left: number}>`
position: absolute;
left: ${p => p.left}px;
border-left: 1px solid ${p => p.theme.translucentInnerBorder};
height: 100%;
`;
const TimeLabelContainer = styled(Gridline)`
display: flex;
height: 100%;
align-items: center;
border-left: none;
`;
const TimeLabel = styled(DateTime)`
font-variant-numeric: tabular-nums;
font-size: ${p => p.theme.fontSizeSmall};
color: ${p => p.theme.subText};
margin-left: ${space(1)};
`;