performanceDuration.tsx 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import Duration from 'sentry/components/duration';
  2. import {defined} from 'sentry/utils';
  3. interface DurationProps {
  4. abbreviation?: boolean;
  5. }
  6. interface SecondsProps extends DurationProps {
  7. seconds: number;
  8. }
  9. interface MillisecondsProps extends DurationProps {
  10. milliseconds: number;
  11. }
  12. interface NanosecondsProps extends DurationProps {
  13. nanoseconds: number;
  14. }
  15. type PerformanceDurationProps = SecondsProps | MillisecondsProps | NanosecondsProps;
  16. function isMilliseconds(props: PerformanceDurationProps): props is MillisecondsProps {
  17. return defined((props as MillisecondsProps).milliseconds);
  18. }
  19. function isNanoseconds(props: PerformanceDurationProps): props is NanosecondsProps {
  20. return defined((props as NanosecondsProps).nanoseconds);
  21. }
  22. function PerformanceDuration(props: PerformanceDurationProps) {
  23. const normalizedSeconds = isNanoseconds(props)
  24. ? props.nanoseconds / 1_000_000_000
  25. : isMilliseconds(props)
  26. ? props.milliseconds / 1000
  27. : props.seconds;
  28. return (
  29. <Duration
  30. abbreviation={props.abbreviation}
  31. seconds={normalizedSeconds}
  32. fixedDigits={2}
  33. />
  34. );
  35. }
  36. export default PerformanceDuration;