You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FeaturedProjects.tsx 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. import * as React from 'react';
  21. import * as classNames from 'classnames';
  22. import CountUp from 'react-countup';
  23. import { throttle } from 'lodash';
  24. import { FeaturedProject } from '../utils';
  25. import CoverageRating from '../../../../components/ui/CoverageRating';
  26. import DuplicationsRating from '../../../../components/ui/DuplicationsRating';
  27. import OrganizationAvatar from '../../../../components/common/OrganizationAvatar';
  28. import ProjectCardLanguagesContainer from '../../../projects/components/ProjectCardLanguagesContainer';
  29. import Rating from '../../../../components/ui/Rating';
  30. import { formatMeasure } from '../../../../helpers/measures';
  31. import { getMetricName } from '../../../overview/utils';
  32. import { getProjectUrl, getBaseUrl, getPathUrlAsString } from '../../../../helpers/urls';
  33. import './FeaturedProjects.css';
  34. interface Props {
  35. projects: FeaturedProject[];
  36. }
  37. interface State {
  38. reversing: boolean;
  39. slides: Array<{
  40. order: number;
  41. project: FeaturedProject;
  42. }>;
  43. sliding: boolean;
  44. viewable: boolean;
  45. }
  46. export default class FeaturedProjects extends React.PureComponent<Props, State> {
  47. container?: HTMLElement | null;
  48. mounted = false;
  49. constructor(props: Props) {
  50. super(props);
  51. this.state = {
  52. reversing: false,
  53. slides: this.orderProjectsFromProps(),
  54. sliding: false,
  55. viewable: false
  56. };
  57. this.handleScroll = throttle(this.handleScroll, 10);
  58. }
  59. componentDidMount() {
  60. this.mounted = true;
  61. document.addEventListener('scroll', this.handleScroll, true);
  62. }
  63. componentDidUpdate(prevProps: Props) {
  64. if (prevProps.projects !== this.props.projects) {
  65. this.setState({ slides: this.orderProjectsFromProps() });
  66. }
  67. }
  68. componentWillUnmount() {
  69. this.mounted = false;
  70. document.removeEventListener('scroll', this.handleScroll, true);
  71. }
  72. handleScroll = () => {
  73. if (this.container) {
  74. const rect = this.container.getBoundingClientRect();
  75. const windowHeight =
  76. window.innerHeight ||
  77. (document.documentElement ? document.documentElement.clientHeight : 0);
  78. if (rect.top <= windowHeight && rect.top + rect.height >= 0) {
  79. this.setState({ viewable: true });
  80. }
  81. }
  82. };
  83. orderProjectsFromProps = () => {
  84. const { projects } = this.props;
  85. if (projects.length === 0) {
  86. return [];
  87. }
  88. // Last element should be put at the begining for proper carousel animation
  89. return [projects.pop(), ...projects].map((project: FeaturedProject, id) => {
  90. return {
  91. order: id,
  92. project
  93. };
  94. });
  95. };
  96. handlePrevClick = () => {
  97. this.setState(({ slides }) => ({
  98. reversing: true,
  99. sliding: true,
  100. slides: slides.map(slide => {
  101. slide.order = slide.order === slides.length - 1 ? 0 : slide.order + 1;
  102. return slide;
  103. })
  104. }));
  105. setTimeout(() => {
  106. if (this.mounted) {
  107. this.setState({ sliding: false });
  108. }
  109. }, 50);
  110. };
  111. handleNextClick = () => {
  112. this.setState(({ slides }) => ({
  113. reversing: false,
  114. sliding: true,
  115. slides: slides.map(slide => {
  116. slide.order = slide.order === 0 ? slides.length - 1 : slide.order - 1;
  117. return slide;
  118. })
  119. }));
  120. setTimeout(() => {
  121. this.setState({ sliding: false });
  122. }, 50);
  123. };
  124. render() {
  125. const { reversing, sliding, viewable } = this.state;
  126. return (
  127. <div
  128. className="sc-featured-projects sc-big-spacer-bottom"
  129. ref={node => (this.container = node)}>
  130. <button className="js-prev sc-project-button" onClick={this.handlePrevClick} type="button">
  131. <img alt="" src={`${getBaseUrl()}/images/sonarcloud/chevron-left.svg`} />
  132. </button>
  133. <div className="sc-featured-projects-container">
  134. <div
  135. className={classNames('sc-featured-projects-inner', {
  136. reversing,
  137. ready: !sliding
  138. })}>
  139. {this.state.slides.map(slide => (
  140. <ProjectCard
  141. key={slide.project.key}
  142. order={slide.order}
  143. project={slide.project}
  144. viewable={viewable}
  145. />
  146. ))}
  147. </div>
  148. </div>
  149. <button className="js-next sc-project-button" onClick={this.handleNextClick} type="button">
  150. <img alt="" src={`${getBaseUrl()}/images/sonarcloud/chevron-right.svg`} />
  151. </button>
  152. </div>
  153. );
  154. }
  155. }
  156. interface ProjectCardProps {
  157. order: number;
  158. project: FeaturedProject;
  159. viewable: boolean;
  160. }
  161. export function ProjectCard({ project, order, viewable }: ProjectCardProps) {
  162. return (
  163. <div className="sc-project-card-container" style={{ order }}>
  164. <a className="sc-project-card" href={getPathUrlAsString(getProjectUrl(project.key))}>
  165. <div className="sc-project-card-header">
  166. <OrganizationAvatar
  167. className="no-border spacer-bottom"
  168. organization={{
  169. name: project.organizationName,
  170. avatar: project.avatarUrl || undefined
  171. }}
  172. />
  173. <p className="sc-project-card-limited" title={project.organizationName}>
  174. {project.organizationName}
  175. </p>
  176. <h5 className="sc-project-card-limited big-spacer-bottom" title={project.name}>
  177. {project.name}
  178. </h5>
  179. </div>
  180. <ul className="sc-project-card-measures">
  181. <ProjectIssues
  182. metric={project.bugs}
  183. metricKey="bugs"
  184. ratingMetric={project.reliabilityRating}
  185. viewable={viewable}
  186. />
  187. <ProjectIssues
  188. metric={project.vulnerabilities}
  189. metricKey="vulnerabilities"
  190. ratingMetric={project.securityRating}
  191. viewable={viewable}
  192. />
  193. <ProjectIssues
  194. metric={project.codeSmells}
  195. metricKey="code_smells"
  196. ratingMetric={project.maintainabilityRating}
  197. viewable={viewable}
  198. />
  199. <li>
  200. <span>{getMetricName('coverage')}</span>
  201. {project.coverage !== undefined ? (
  202. <div>
  203. {viewable && (
  204. <CountUp
  205. decimal="."
  206. decimals={1}
  207. delay={0}
  208. duration={4}
  209. end={project.coverage}
  210. suffix="%">
  211. {(data: { countUpRef?: React.RefObject<HTMLHeadingElement> }) => (
  212. <h6 className="display-inline-block big-spacer-right" ref={data.countUpRef}>
  213. 0
  214. </h6>
  215. )}
  216. </CountUp>
  217. )}
  218. <CoverageRating value={project.coverage} />
  219. </div>
  220. ) : (
  221. <span className="huge little-spacer-right">—</span>
  222. )}
  223. </li>
  224. <li>
  225. <span>{getMetricName('duplications')}</span>
  226. <div>
  227. {viewable && (
  228. <CountUp
  229. decimal="."
  230. decimals={1}
  231. delay={0}
  232. duration={4}
  233. end={project.duplications}
  234. suffix="%">
  235. {(data: { countUpRef?: React.RefObject<HTMLHeadingElement> }) => (
  236. <h6 className="display-inline-block big-spacer-right" ref={data.countUpRef}>
  237. 0
  238. </h6>
  239. )}
  240. </CountUp>
  241. )}
  242. <DuplicationsRating value={project.duplications} />
  243. </div>
  244. </li>
  245. </ul>
  246. <div className="sc-mention text-left big-spacer-top">
  247. {formatMeasure(project.ncloc, 'SHORT_INT')} lines of code /{' '}
  248. <ProjectCardLanguagesContainer
  249. className="display-inline-block"
  250. distribution={project.languages.join(';')}
  251. />
  252. </div>
  253. </a>
  254. </div>
  255. );
  256. }
  257. interface ProjectIssues {
  258. metricKey: string;
  259. metric: number;
  260. ratingMetric: number;
  261. viewable: boolean;
  262. }
  263. export function ProjectIssues({ metric, metricKey, ratingMetric, viewable }: ProjectIssues) {
  264. const formattedValue = formatMeasure(metric, 'SHORT_INT');
  265. const value = parseFloat(formattedValue);
  266. const suffix = formattedValue.replace(value.toString(), '');
  267. return (
  268. <li>
  269. <span>{getMetricName(metricKey)}</span>
  270. <div>
  271. {viewable && (
  272. <CountUp delay={0} duration={4} end={value} suffix={suffix}>
  273. {(data: { countUpRef?: React.RefObject<HTMLHeadingElement> }) => (
  274. <h6 className="display-inline-block big-spacer-right" ref={data.countUpRef}>
  275. 0
  276. </h6>
  277. )}
  278. </CountUp>
  279. )}
  280. <Rating value={ratingMetric} />
  281. </div>
  282. </li>
  283. );
  284. }