-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathuseJumpButtons.tsx
66 lines (56 loc) · 1.88 KB
/
useJumpButtons.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
import {useCallback, useMemo, useState} from 'react';
import type {IndexRange, SectionRenderedParams} from 'react-virtualized';
import {getNextReplayFrame} from 'sentry/utils/replays/getReplayEvent';
import type {ReplayFrame} from 'sentry/utils/replays/types';
interface Props {
currentTime: number;
frames: ReplayFrame[];
isTable: boolean;
setScrollToRow: (row: number) => void;
}
export default function useJumpButtons({
currentTime,
frames,
isTable,
setScrollToRow,
}: Props) {
const [visibleRange, setVisibleRange] = useState<IndexRange>({
startIndex: 0,
stopIndex: 0,
});
const frameIndex = useMemo(() => {
const frame = getNextReplayFrame({
frames,
targetOffsetMs: currentTime,
allowExact: true,
});
if (!frame) {
return frames.length - 1;
}
const index = frames.indexOf(frame);
// index is -1 at end of replay, so use last index
return index === -1 ? frames.length - 1 : index;
}, [currentTime, frames]);
// Tables have a header row, so we need to adjust for that.
const rowIndex = isTable ? frameIndex + 1 : frameIndex;
const handleClick = useCallback(() => {
// When Jump Down, ensures purple line is visible and index needs to be 1 to jump to top of the list
const jumpDownFurther =
isTable && (rowIndex > visibleRange.stopIndex || rowIndex === 0);
setScrollToRow(rowIndex + (jumpDownFurther ? 1 : 0));
}, [isTable, rowIndex, setScrollToRow, visibleRange]);
const onRowsRendered = setVisibleRange;
const onSectionRendered = useCallback(
({rowStartIndex, rowStopIndex}: SectionRenderedParams) => {
setVisibleRange({startIndex: rowStartIndex, stopIndex: rowStopIndex});
},
[]
);
return {
handleClick,
onRowsRendered,
onSectionRendered,
showJumpDownButton: rowIndex > visibleRange.stopIndex + 1,
showJumpUpButton: rowIndex < visibleRange.startIndex,
};
}