percentChangeCell.tsx 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import styled from '@emotion/styled';
  2. import {NumberContainer} from 'sentry/utils/discover/styles';
  3. import {formatPercentage} from 'sentry/utils/formatters';
  4. type PercentChangeCellProps = {
  5. deltaValue: number;
  6. colorize?: boolean;
  7. };
  8. export function PercentChangeCell({deltaValue, colorize = true}: PercentChangeCellProps) {
  9. const sign = deltaValue >= 0 ? '+' : '-';
  10. const delta = formatPercentage(Math.abs(deltaValue), 2);
  11. const trendDirection = deltaValue < 0 ? 'good' : deltaValue > 0 ? 'bad' : 'neutral';
  12. return (
  13. <NumberContainer>
  14. <Colorized trendDirection={colorize ? trendDirection : 'neutral'}>
  15. {sign}
  16. {delta}
  17. </Colorized>
  18. </NumberContainer>
  19. );
  20. }
  21. type ColorizedProps = {
  22. children: React.ReactNode;
  23. trendDirection: 'good' | 'bad' | 'neutral';
  24. };
  25. const Colorized = styled('div')<ColorizedProps>`
  26. color: ${p =>
  27. p.trendDirection === 'good'
  28. ? p.theme.successText
  29. : p.trendDirection === 'bad'
  30. ? p.theme.errorText
  31. : p.theme.subText};
  32. `;