tagDistributionMeter.tsx 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. import {Fragment} from 'react';
  2. import isPropValid from '@emotion/is-prop-valid';
  3. import styled from '@emotion/styled';
  4. import {LocationDescriptor} from 'history';
  5. import {TagSegment} from 'sentry/actionCreators/events';
  6. import Link from 'sentry/components/links/link';
  7. import Tooltip from 'sentry/components/tooltip';
  8. import Version from 'sentry/components/version';
  9. import {t} from 'sentry/locale';
  10. import space from 'sentry/styles/space';
  11. import {percent} from 'sentry/utils';
  12. type Props = {
  13. segments: TagSegment[];
  14. title: string;
  15. totalValues: number;
  16. hasError?: boolean;
  17. isLoading?: boolean;
  18. onTagClick?: (title: string, value: TagSegment) => void;
  19. renderEmpty?: () => React.ReactNode;
  20. renderError?: () => React.ReactNode;
  21. renderLoading?: () => React.ReactNode;
  22. showReleasePackage?: boolean;
  23. };
  24. type SegmentValue = {
  25. index: number;
  26. onClick: () => void;
  27. to: LocationDescriptor;
  28. };
  29. function TagDistributionMeter({
  30. isLoading = false,
  31. hasError = false,
  32. renderLoading = () => null,
  33. renderEmpty = () => <p>{t('No recent data.')}</p>,
  34. renderError = () => null,
  35. showReleasePackage = false,
  36. segments,
  37. title,
  38. totalValues,
  39. onTagClick,
  40. }: Props) {
  41. function renderTitle() {
  42. if (!Array.isArray(segments) || segments.length <= 0) {
  43. return (
  44. <Title>
  45. <TitleType>{title}</TitleType>
  46. </Title>
  47. );
  48. }
  49. const largestSegment = segments[0];
  50. const pct = percent(largestSegment.count, totalValues);
  51. const pctLabel = Math.floor(pct);
  52. const renderLabel = () => {
  53. switch (title) {
  54. case 'release':
  55. return (
  56. <Label>
  57. <Version
  58. version={largestSegment.name}
  59. anchor={false}
  60. tooltipRawVersion
  61. withPackage={showReleasePackage}
  62. truncate
  63. />
  64. </Label>
  65. );
  66. default:
  67. return <Label>{largestSegment.name || t('n/a')}</Label>;
  68. }
  69. };
  70. return (
  71. <Title>
  72. <TitleType>{title}</TitleType>
  73. <TitleDescription>
  74. {renderLabel()}
  75. {isLoading || hasError ? null : <Percent>{pctLabel}%</Percent>}
  76. </TitleDescription>
  77. </Title>
  78. );
  79. }
  80. function renderSegments() {
  81. if (isLoading) {
  82. return renderLoading();
  83. }
  84. if (hasError) {
  85. return <SegmentBar>{renderError()}</SegmentBar>;
  86. }
  87. if (totalValues === 0) {
  88. return <SegmentBar>{renderEmpty()}</SegmentBar>;
  89. }
  90. return (
  91. <SegmentBar>
  92. {segments.map((value, index) => {
  93. const pct = percent(value.count, totalValues);
  94. const pctLabel = Math.floor(pct);
  95. const renderTooltipValue = () => {
  96. switch (title) {
  97. case 'release':
  98. return (
  99. <Version
  100. version={value.name}
  101. anchor={false}
  102. withPackage={showReleasePackage}
  103. />
  104. );
  105. default:
  106. return value.name || t('n/a');
  107. }
  108. };
  109. const tooltipHtml = (
  110. <Fragment>
  111. <div className="truncate">{renderTooltipValue()}</div>
  112. {pctLabel}%
  113. </Fragment>
  114. );
  115. const segmentProps: SegmentValue = {
  116. index,
  117. to: value.url,
  118. onClick: () => onTagClick?.(title, value),
  119. };
  120. return (
  121. <div
  122. data-test-id={`tag-${title}-segment-${value.value}`}
  123. key={value.value}
  124. style={{width: pct + '%'}}
  125. >
  126. <Tooltip title={tooltipHtml} containerDisplayMode="block">
  127. {value.isOther ? (
  128. <OtherSegment aria-label={t('Other')} />
  129. ) : (
  130. <Segment
  131. aria-label={t(
  132. 'Add the %s %s segment tag to the search query',
  133. title,
  134. value.value
  135. )}
  136. {...segmentProps}
  137. />
  138. )}
  139. </Tooltip>
  140. </div>
  141. );
  142. })}
  143. </SegmentBar>
  144. );
  145. }
  146. const totalVisible = segments.reduce((sum, value) => sum + value.count, 0);
  147. const hasOther = totalVisible < totalValues;
  148. if (hasOther) {
  149. segments.push({
  150. isOther: true,
  151. name: t('Other'),
  152. value: 'other',
  153. count: totalValues - totalVisible,
  154. url: '',
  155. });
  156. }
  157. return (
  158. <TagSummary>
  159. {renderTitle()}
  160. {renderSegments()}
  161. </TagSummary>
  162. );
  163. }
  164. export default TagDistributionMeter;
  165. const COLORS = [
  166. '#3A3387',
  167. '#5F40A3',
  168. '#8C4FBD',
  169. '#B961D3',
  170. '#DE76E4',
  171. '#EF91E8',
  172. '#F7B2EC',
  173. '#FCD8F4',
  174. '#FEEBF9',
  175. ];
  176. const TagSummary = styled('div')`
  177. margin-bottom: ${space(1)};
  178. `;
  179. const SegmentBar = styled('div')`
  180. display: flex;
  181. overflow: hidden;
  182. border-radius: ${p => p.theme.borderRadius};
  183. `;
  184. const Title = styled('div')`
  185. display: flex;
  186. font-size: ${p => p.theme.fontSizeSmall};
  187. justify-content: space-between;
  188. margin-bottom: ${space(0.25)};
  189. line-height: 1.1;
  190. `;
  191. const TitleType = styled('div')`
  192. color: ${p => p.theme.textColor};
  193. font-weight: bold;
  194. ${p => p.theme.overflowEllipsis};
  195. `;
  196. const TitleDescription = styled('div')`
  197. display: flex;
  198. color: ${p => p.theme.gray300};
  199. text-align: right;
  200. `;
  201. const Label = styled('div')`
  202. ${p => p.theme.overflowEllipsis};
  203. max-width: 150px;
  204. `;
  205. const Percent = styled('div')`
  206. font-weight: bold;
  207. font-variant-numeric: tabular-nums;
  208. padding-left: ${space(0.5)};
  209. color: ${p => p.theme.textColor};
  210. `;
  211. const OtherSegment = styled('span')`
  212. display: block;
  213. width: 100%;
  214. height: 16px;
  215. color: inherit;
  216. outline: none;
  217. background-color: ${COLORS[COLORS.length - 1]};
  218. `;
  219. const Segment = styled(Link, {shouldForwardProp: isPropValid})<SegmentValue>`
  220. display: block;
  221. width: 100%;
  222. height: 16px;
  223. color: inherit;
  224. outline: none;
  225. background-color: ${p => COLORS[p.index]};
  226. border-radius: 0;
  227. `;