getCurrentUrl.tsx 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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 parseUrl(url: string) {
  6. try {
  7. return new URL(url);
  8. } catch {
  9. return undefined;
  10. }
  11. }
  12. function getCurrentUrl(
  13. replayRecord: ReplayRecord,
  14. crumbs: Crumb[],
  15. currentOffsetMS: number
  16. ) {
  17. const startTimestampMs = replayRecord.startedAt.getTime();
  18. const currentTimeMs = startTimestampMs + Math.floor(currentOffsetMS);
  19. const navigationCrumbs = crumbs.filter(
  20. crumb => crumb.type === BreadcrumbType.NAVIGATION
  21. ) as BreadcrumbTypeNavigation[];
  22. const initialUrl = replayRecord.urls[0];
  23. const origin = parseUrl(initialUrl)?.origin || initialUrl;
  24. const mostRecentNavigation = last(
  25. navigationCrumbs.filter(({timestamp}) => +new Date(timestamp || 0) <= currentTimeMs)
  26. )?.data?.to;
  27. if (!mostRecentNavigation) {
  28. return origin;
  29. }
  30. const parsed = parseUrl(mostRecentNavigation);
  31. if (parsed) {
  32. // If `mostRecentNavigation` has the origin then we can parse it as a URL and return it
  33. return String(parsed);
  34. }
  35. // Otherwise we need to add the origin manually and hope the suffix makes sense.
  36. return origin + mostRecentNavigation;
  37. }
  38. export default getCurrentUrl;