utils.tsx 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import {relativeTimeInMs} from 'sentry/components/replays/utils';
  2. import {defined} from 'sentry/utils';
  3. export const getInitialTimeOffset = ({
  4. eventTimestamp,
  5. initialTimeOffset,
  6. startTimestampMs,
  7. }: {
  8. eventTimestamp?: string;
  9. initialTimeOffset?: number;
  10. startTimestampMs?: number;
  11. }) => {
  12. if (defined(initialTimeOffset)) {
  13. return initialTimeOffset;
  14. }
  15. // If the user has navigated to the replay from an event, then we want to
  16. // start the video at the time of the event.
  17. if (defined(eventTimestamp) && defined(startTimestampMs)) {
  18. // check if the event timestamp is the correct format
  19. let eventTimestampMs = 0;
  20. if (eventTimestamp.length === 13) {
  21. eventTimestampMs = Number(eventTimestamp);
  22. } else {
  23. eventTimestampMs = new Date(eventTimestamp).getTime();
  24. }
  25. // check if our event timestamp is within the range of the replay
  26. if (eventTimestampMs >= startTimestampMs) {
  27. initialTimeOffset = relativeTimeInMs(eventTimestampMs, startTimestampMs) / 1000;
  28. }
  29. }
  30. // if the event timestamp is not the correct format or no timestamps are provided, default to the start of the replay
  31. if (!defined(initialTimeOffset) || Number.isNaN(initialTimeOffset)) {
  32. initialTimeOffset = 0;
  33. }
  34. return initialTimeOffset;
  35. };