timestampButton.tsx 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import type {MouseEvent} from 'react';
  2. import styled from '@emotion/styled';
  3. import {DateTime} from 'sentry/components/dateTime';
  4. import Duration from 'sentry/components/duration/duration';
  5. import {Tooltip} from 'sentry/components/tooltip';
  6. import {IconPlay} from 'sentry/icons';
  7. import {t} from 'sentry/locale';
  8. import {space} from 'sentry/styles/space';
  9. import {getFormat, getFormattedDate} from 'sentry/utils/dates';
  10. import formatDuration from 'sentry/utils/duration/formatDuration';
  11. import {useReplayPrefs} from 'sentry/utils/replays/playback/providers/replayPreferencesContext';
  12. type Props = {
  13. startTimestampMs: number;
  14. timestampMs: number;
  15. className?: string;
  16. onClick?: (event: MouseEvent) => void;
  17. precision?: 'sec' | 'ms';
  18. };
  19. export default function TimestampButton({
  20. className,
  21. precision = 'sec',
  22. onClick,
  23. startTimestampMs,
  24. timestampMs,
  25. }: Props) {
  26. const [prefs] = useReplayPrefs();
  27. const timestampType = prefs.timestampType;
  28. return (
  29. <Tooltip
  30. title={
  31. <div>
  32. <TooltipTime>
  33. {t(
  34. 'Date: %s',
  35. getFormattedDate(
  36. timestampMs,
  37. `${getFormat({year: true, seconds: true, timeZone: true})}`,
  38. {
  39. local: true,
  40. }
  41. )
  42. )}
  43. </TooltipTime>
  44. <TooltipTime>
  45. {t(
  46. 'Time within replay: %s',
  47. formatDuration({
  48. duration: [Math.abs(timestampMs - startTimestampMs), 'ms'],
  49. precision: 'ms',
  50. style: 'hh:mm:ss.sss',
  51. })
  52. )}
  53. </TooltipTime>
  54. </div>
  55. }
  56. skipWrapper
  57. >
  58. <StyledButton
  59. as={onClick ? 'button' : 'span'}
  60. onClick={onClick}
  61. className={className}
  62. >
  63. <IconPlay size="xs" />
  64. {timestampType === 'absolute' ? (
  65. <DateTime timeOnly seconds date={timestampMs} />
  66. ) : (
  67. <Duration
  68. duration={[Math.abs(timestampMs - startTimestampMs), 'ms']}
  69. precision={precision}
  70. />
  71. )}
  72. </StyledButton>
  73. </Tooltip>
  74. );
  75. }
  76. const TooltipTime = styled('div')`
  77. text-align: left;
  78. `;
  79. const StyledButton = styled('button')`
  80. background: transparent;
  81. border: none;
  82. color: inherit;
  83. font-variant-numeric: tabular-nums;
  84. line-height: 1.2em;
  85. display: flex;
  86. align-items: flex-start;
  87. align-self: baseline;
  88. gap: ${space(0.25)};
  89. padding: 0;
  90. height: 100%;
  91. `;