scoreBar.tsx 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import styled from '@emotion/styled';
  2. import theme from 'sentry/utils/theme';
  3. type Props = {
  4. score: number;
  5. className?: string;
  6. palette?: Readonly<string[]>;
  7. paletteClassNames?: string[];
  8. radius?: number;
  9. size?: number;
  10. thickness?: number;
  11. vertical?: boolean;
  12. };
  13. const BaseScoreBar = ({
  14. score,
  15. className,
  16. vertical,
  17. size = 40,
  18. thickness = 4,
  19. radius = 3,
  20. palette = theme.similarity.colors,
  21. }: Props) => {
  22. const maxScore = palette.length;
  23. // Make sure score is between 0 and maxScore
  24. const scoreInBounds = score >= maxScore ? maxScore : score <= 0 ? 0 : score;
  25. // Make sure paletteIndex is 0 based
  26. const paletteIndex = scoreInBounds - 1;
  27. // Size of bar, depends on orientation, although we could just apply a transformation via css
  28. const barProps = {
  29. vertical,
  30. thickness,
  31. size,
  32. radius,
  33. };
  34. return (
  35. <div className={className}>
  36. {[...Array(scoreInBounds)].map((_j, i) => (
  37. <Bar {...barProps} key={i} color={palette[paletteIndex]} />
  38. ))}
  39. {[...Array(maxScore - scoreInBounds)].map((_j, i) => (
  40. <Bar key={`empty-${i}`} {...barProps} empty />
  41. ))}
  42. </div>
  43. );
  44. };
  45. const ScoreBar = styled(BaseScoreBar)`
  46. display: flex;
  47. ${p =>
  48. p.vertical
  49. ? `flex-direction: column-reverse;
  50. justify-content: flex-end;`
  51. : 'min-width: 80px;'};
  52. `;
  53. type BarProps = {
  54. radius: number;
  55. size: number;
  56. thickness: number;
  57. color?: string;
  58. empty?: boolean;
  59. vertical?: boolean;
  60. };
  61. const Bar = styled('div')<BarProps>`
  62. border-radius: ${p => p.radius}px;
  63. margin: 2px;
  64. ${p => p.empty && `background-color: ${p.theme.similarity.empty};`};
  65. ${p => p.color && `background-color: ${p.color};`};
  66. width: ${p => (!p.vertical ? p.thickness : p.size)}px;
  67. height: ${p => (!p.vertical ? p.size : p.thickness)}px;
  68. `;
  69. export default ScoreBar;