scoreCard.tsx 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import styled from '@emotion/styled';
  2. import {Panel} from 'sentry/components/panels';
  3. import QuestionTooltip from 'sentry/components/questionTooltip';
  4. import TextOverflow from 'sentry/components/textOverflow';
  5. import space from 'sentry/styles/space';
  6. import {defined} from 'sentry/utils';
  7. import {Theme} from 'sentry/utils/theme';
  8. type Props = {
  9. title: React.ReactNode;
  10. className?: string;
  11. help?: React.ReactNode;
  12. score?: React.ReactNode;
  13. trend?: React.ReactNode;
  14. trendStatus?: 'good' | 'bad';
  15. };
  16. function ScoreCard({title, score, help, trend, trendStatus, className}: Props) {
  17. return (
  18. <ScorePanel className={className}>
  19. <HeaderTitle>
  20. <Title>{title}</Title>
  21. {help && <QuestionTooltip title={help} size="sm" position="top" />}
  22. </HeaderTitle>
  23. <ScoreWrapper>
  24. <Score>{score ?? '\u2014'}</Score>
  25. {defined(trend) && (
  26. <Trend trendStatus={trendStatus}>
  27. <TextOverflow>{trend}</TextOverflow>
  28. </Trend>
  29. )}
  30. </ScoreWrapper>
  31. </ScorePanel>
  32. );
  33. }
  34. function getTrendColor(p: TrendProps & {theme: Theme}) {
  35. switch (p.trendStatus) {
  36. case 'good':
  37. return p.theme.green300;
  38. case 'bad':
  39. return p.theme.red300;
  40. default:
  41. return p.theme.gray300;
  42. }
  43. }
  44. export const ScorePanel = styled(Panel)`
  45. display: flex;
  46. flex-direction: column;
  47. justify-content: space-between;
  48. padding: ${space(2)} ${space(3)};
  49. min-height: 96px;
  50. `;
  51. export const HeaderTitle = styled('div')`
  52. display: inline-grid;
  53. grid-auto-flow: column;
  54. gap: ${space(1)};
  55. align-items: center;
  56. width: fit-content;
  57. `;
  58. export const Title = styled('div')`
  59. color: ${p => p.theme.headingColor};
  60. ${p => p.theme.overflowEllipsis};
  61. font-weight: 600;
  62. `;
  63. export const ScoreWrapper = styled('div')`
  64. display: flex;
  65. flex-direction: row;
  66. align-items: flex-end;
  67. max-width: 100%;
  68. `;
  69. export const Score = styled('span')`
  70. flex-shrink: 1;
  71. font-size: 32px;
  72. line-height: 1;
  73. color: ${p => p.theme.headingColor};
  74. white-space: nowrap;
  75. `;
  76. type TrendProps = {trendStatus: Props['trendStatus']};
  77. export const Trend = styled('div')<TrendProps>`
  78. color: ${getTrendColor};
  79. margin-left: ${space(1)};
  80. line-height: 1;
  81. overflow: hidden;
  82. `;
  83. export default ScoreCard;