getReplayEvent.tsx 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import type {ReplayFrame} from 'sentry/utils/replays/types';
  2. export function getPrevReplayFrame({
  3. frames,
  4. targetOffsetMs,
  5. allowExact = false,
  6. }: {
  7. frames: ReplayFrame[];
  8. targetOffsetMs: number;
  9. allowExact?: boolean;
  10. }) {
  11. return frames.reduce<ReplayFrame | undefined>((found, item) => {
  12. if (
  13. item.offsetMs > targetOffsetMs ||
  14. (!allowExact && item.offsetMs === targetOffsetMs)
  15. ) {
  16. return found;
  17. }
  18. if (
  19. (allowExact && item.offsetMs === targetOffsetMs) ||
  20. !found ||
  21. item.offsetMs > found.offsetMs
  22. ) {
  23. return item;
  24. }
  25. return found;
  26. }, undefined);
  27. }
  28. export function getNextReplayFrame({
  29. frames,
  30. targetOffsetMs,
  31. allowExact = false,
  32. }: {
  33. frames: ReplayFrame[];
  34. targetOffsetMs: number;
  35. allowExact?: boolean;
  36. }) {
  37. return frames.reduce<ReplayFrame | undefined>((found, item) => {
  38. if (
  39. item.offsetMs < targetOffsetMs ||
  40. (!allowExact && item.offsetMs === targetOffsetMs)
  41. ) {
  42. return found;
  43. }
  44. if (
  45. (allowExact && item.offsetMs === targetOffsetMs) ||
  46. !found ||
  47. item.offsetMs < found.offsetMs
  48. ) {
  49. return item;
  50. }
  51. return found;
  52. }, undefined);
  53. }