getCurrentUrl.tsx 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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 {EventTransaction} from 'sentry/types/event';
  5. import {EventTag} from 'sentry/types/event';
  6. function findUrlTag(tags: EventTag[]) {
  7. return tags.find(tag => tag.key === 'url');
  8. }
  9. function getCurrentUrl(
  10. event: EventTransaction,
  11. crumbs: Crumb[],
  12. currentOffsetMS: number
  13. ) {
  14. const startTimestampMs = event.startTimestamp * 1000;
  15. const currentTimeMs = startTimestampMs + Math.floor(currentOffsetMS);
  16. const navigationCrumbs = crumbs.filter(
  17. crumb => crumb.type === BreadcrumbType.NAVIGATION
  18. ) as BreadcrumbTypeNavigation[];
  19. const initialUrl = findUrlTag(event.tags)?.value || '';
  20. const origin = initialUrl ? new URL(initialUrl).origin : '';
  21. const mostRecentNavigation = last(
  22. navigationCrumbs.filter(({timestamp}) => +new Date(timestamp || 0) < currentTimeMs)
  23. )?.data?.to;
  24. if (!mostRecentNavigation) {
  25. return initialUrl;
  26. }
  27. try {
  28. // If `mostRecentNavigation` has the origin then we can parse it as a URL
  29. const url = new URL(mostRecentNavigation);
  30. return String(url);
  31. } catch {
  32. // Otherwise we need to add the origin manually and hope the suffix makes sense.
  33. return origin + mostRecentNavigation;
  34. }
  35. }
  36. export default getCurrentUrl;