tagDistributionMeter.tsx 5.9 KB

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