getCurrentUrl.tsx 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import first from 'lodash/first';
  2. import last from 'lodash/last';
  3. import type {
  4. BreadcrumbFrame,
  5. NavigationFrame,
  6. SpanFrame,
  7. } from 'sentry/utils/replays/types';
  8. import stripOrigin from 'sentry/utils/url/stripOrigin';
  9. function getCurrentUrl(
  10. frames: undefined | (BreadcrumbFrame | SpanFrame)[],
  11. currentOffsetMS: number
  12. ) {
  13. const framesBeforeCurrentOffset = frames?.filter(
  14. frame => frame.offsetMs < currentOffsetMS
  15. );
  16. const mostRecentFrame = last(framesBeforeCurrentOffset) ?? first(frames);
  17. if (!mostRecentFrame) {
  18. return '';
  19. }
  20. if ('category' in mostRecentFrame && mostRecentFrame.category === 'replay.init') {
  21. return stripOrigin(mostRecentFrame.message ?? '');
  22. }
  23. if (
  24. 'op' in mostRecentFrame &&
  25. [
  26. 'navigation.navigate',
  27. 'navigation.reload',
  28. 'navigation.back_forward',
  29. 'navigation.push',
  30. ].includes(mostRecentFrame.op)
  31. ) {
  32. // navigation.push will have the pathname while the other `navigate.*`
  33. // operations will have a full url.
  34. return stripOrigin((mostRecentFrame as NavigationFrame).description);
  35. }
  36. throw new Error('Unknown frame type in getCurrentUrl');
  37. }
  38. export default getCurrentUrl;