tagDistributionMeter.tsx 6.1 KB

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