common.tsx 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import styled from '@emotion/styled';
  2. import {t} from 'sentry/locale';
  3. import getDuration from 'sentry/utils/duration/getDuration';
  4. import {TextAlignRight} from 'sentry/views/insights/common/components/textAlign';
  5. type Props = {
  6. compareToDuration: number;
  7. duration: number;
  8. containerProps?: React.DetailedHTMLProps<
  9. React.HTMLAttributes<HTMLSpanElement>,
  10. HTMLSpanElement
  11. >;
  12. };
  13. export function DurationComparisonCell({
  14. duration,
  15. compareToDuration,
  16. containerProps,
  17. }: Props) {
  18. const diff = duration - compareToDuration;
  19. if (isNearAverage(duration, compareToDuration)) {
  20. return <TextAlignRight {...containerProps}>{t('Near Average')}</TextAlignRight>;
  21. }
  22. const readableDiff = getDuration(diff / 1000, 2, true, true);
  23. const labelString = diff > 0 ? `+${readableDiff} above` : `${readableDiff} below`;
  24. return (
  25. <ComparisonLabel {...containerProps} value={diff}>
  26. {labelString}
  27. </ComparisonLabel>
  28. );
  29. }
  30. export const isNearAverage = (duration: number, compareToDuration: number) => {
  31. const maxDiff = 0.03 * compareToDuration;
  32. const diff = Math.abs(duration - compareToDuration);
  33. return diff < maxDiff;
  34. };
  35. const ComparisonLabel = styled('span')<{value: number}>`
  36. color: ${p =>
  37. p.value === 0 ? p.theme.subText : p.value < 0 ? p.theme.green400 : p.theme.red400};
  38. text-align: right;
  39. `;