useJumpButtons.tsx 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import {useCallback, useMemo, useState} from 'react';
  2. import type {IndexRange, SectionRenderedParams} from 'react-virtualized';
  3. import {getNextReplayFrame} from 'sentry/utils/replays/getReplayEvent';
  4. import type {ReplayFrame} from 'sentry/utils/replays/types';
  5. interface Props {
  6. currentTime: number;
  7. frames: ReplayFrame[];
  8. isTable: boolean;
  9. setScrollToRow: (row: number) => void;
  10. }
  11. export default function useJumpButtons({
  12. currentTime,
  13. frames,
  14. isTable,
  15. setScrollToRow,
  16. }: Props) {
  17. const [visibleRange, setVisibleRange] = useState<IndexRange>({
  18. startIndex: 0,
  19. stopIndex: 0,
  20. });
  21. const frameIndex = useMemo(() => {
  22. const frame = getNextReplayFrame({
  23. frames,
  24. targetOffsetMs: currentTime,
  25. allowExact: true,
  26. });
  27. const index = frames.findIndex(spanFrame => frame === spanFrame);
  28. // index is -1 at end of replay, so use last index
  29. return index === -1 ? frames.length - 1 : index;
  30. }, [currentTime, frames]);
  31. // Tables have a header row, so we need to adjust for that.
  32. const rowIndex = isTable ? frameIndex + 1 : frameIndex;
  33. const handleClick = useCallback(() => {
  34. // When Jump Down, ensures purple line is visible and index needs to be 1 to jump to top of the list
  35. const jumpDownFurther =
  36. isTable && (rowIndex > visibleRange.stopIndex || rowIndex === 0);
  37. setScrollToRow(rowIndex + (jumpDownFurther ? 1 : 0));
  38. }, [isTable, rowIndex, setScrollToRow, visibleRange]);
  39. const onRowsRendered = setVisibleRange;
  40. const onSectionRendered = useCallback(
  41. ({rowStartIndex, rowStopIndex}: SectionRenderedParams) => {
  42. setVisibleRange({startIndex: rowStartIndex, stopIndex: rowStopIndex});
  43. },
  44. []
  45. );
  46. return {
  47. handleClick,
  48. onRowsRendered,
  49. onSectionRendered,
  50. showJumpDownButton: rowIndex > visibleRange.stopIndex + 1,
  51. showJumpUpButton: rowIndex < visibleRange.startIndex,
  52. };
  53. }