getBreadcrumb.tsx 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import {Crumb} from 'sentry/types/breadcrumbs';
  2. export function getPrevBreadcrumb({
  3. crumbs,
  4. targetTimestampMs,
  5. allowExact = false,
  6. }: {
  7. crumbs: Crumb[];
  8. targetTimestampMs: number;
  9. allowExact?: boolean;
  10. }) {
  11. return crumbs.reduce<Crumb | undefined>((prev, crumb) => {
  12. const crumbTimestampMS = +new Date(crumb.timestamp || '');
  13. if (
  14. crumbTimestampMS > targetTimestampMs ||
  15. (!allowExact && crumbTimestampMS === targetTimestampMs)
  16. ) {
  17. return prev;
  18. }
  19. if (!prev || crumbTimestampMS > +new Date(prev.timestamp || '')) {
  20. return crumb;
  21. }
  22. return prev;
  23. }, undefined);
  24. }
  25. export function getNextBreadcrumb({
  26. crumbs,
  27. targetTimestampMs,
  28. allowExact = false,
  29. }: {
  30. crumbs: Crumb[];
  31. targetTimestampMs: number;
  32. allowExact?: boolean;
  33. }) {
  34. return crumbs.reduce<Crumb | undefined>((found, crumb) => {
  35. const crumbTimestampMS = +new Date(crumb.timestamp || '');
  36. if (
  37. crumbTimestampMS < targetTimestampMs ||
  38. (!allowExact && crumbTimestampMS === targetTimestampMs)
  39. ) {
  40. return found;
  41. }
  42. if (!found || crumbTimestampMS < +new Date(found.timestamp || '')) {
  43. return crumb;
  44. }
  45. return found;
  46. }, undefined);
  47. }