getCurrentUrl.tsx 1.5 KB

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