getCurrentUrl.tsx 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import last from 'lodash/last';
  2. import type {Crumb} from 'sentry/types/breadcrumbs';
  3. import {BreadcrumbType, BreadcrumbTypeNavigation} from 'sentry/types/breadcrumbs';
  4. import type {ReplayRecord} from 'sentry/views/replays/types';
  5. function getCurrentUrl(
  6. replayRecord: ReplayRecord,
  7. crumbs: Crumb[],
  8. currentOffsetMS: number
  9. ) {
  10. const startTimestampMs = replayRecord.startedAt.getTime();
  11. const currentTimeMs = startTimestampMs + Math.floor(currentOffsetMS);
  12. const navigationCrumbs = crumbs.filter(
  13. crumb => crumb.type === BreadcrumbType.NAVIGATION
  14. ) as BreadcrumbTypeNavigation[];
  15. const initialUrl = replayRecord.tags.url;
  16. const origin = initialUrl ? new URL(initialUrl).origin : '';
  17. const mostRecentNavigation = last(
  18. navigationCrumbs.filter(({timestamp}) => +new Date(timestamp || 0) < currentTimeMs)
  19. )?.data?.to;
  20. if (!mostRecentNavigation) {
  21. return initialUrl;
  22. }
  23. try {
  24. // If `mostRecentNavigation` has the origin then we can parse it as a URL
  25. const url = new URL(mostRecentNavigation);
  26. return String(url);
  27. } catch {
  28. // Otherwise we need to add the origin manually and hope the suffix makes sense.
  29. return origin + mostRecentNavigation;
  30. }
  31. }
  32. export default getCurrentUrl;