getCurrentUrl.tsx 1.5 KB

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