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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2018 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 { Link } from 'react-router';
  25. import { FeaturedProject } from '../utils';
  26. import CoverageRating from '../../../../components/ui/CoverageRating';
  27. import DuplicationsRating from '../../../../components/ui/DuplicationsRating';
  28. import Level from '../../../../components/ui/Level';
  29. import OrganizationAvatar from '../../../../components/common/OrganizationAvatar';
  30. import ProjectCardLanguagesContainer from '../../../projects/components/ProjectCardLanguagesContainer';
  31. import Rating from '../../../../components/ui/Rating';
  32. import { formatMeasure } from '../../../../helpers/measures';
  33. import { getMetricName } from '../../../overview/utils';
  34. import { getProjectUrl, getBaseUrl } from '../../../../helpers/urls';
  35. import './FeaturedProjects.css';
  36. interface Props {
  37. projects: FeaturedProject[];
  38. }
  39. interface State {
  40. reversing: boolean;
  41. slides: Array<{
  42. order: number;
  43. project: FeaturedProject;
  44. }>;
  45. sliding: boolean;
  46. translate: number;
  47. viewable: boolean;
  48. }
  49. export default class FeaturedProjects extends React.PureComponent<Props, State> {
  50. container?: HTMLElement | null;
  51. mounted = false;
  52. constructor(props: Props) {
  53. super(props);
  54. this.state = {
  55. reversing: false,
  56. slides: this.orderProjectsFromProps(),
  57. sliding: false,
  58. translate: 0,
  59. viewable: false
  60. };
  61. this.handleScroll = throttle(this.handleScroll, 10);
  62. }
  63. componentDidMount() {
  64. this.mounted = true;
  65. document.addEventListener('scroll', this.handleScroll, true);
  66. }
  67. componentDidUpdate(prevProps: Props) {
  68. if (prevProps.projects !== this.props.projects) {
  69. this.setState({ slides: this.orderProjectsFromProps() });
  70. }
  71. }
  72. componentWillUnmount() {
  73. this.mounted = false;
  74. document.removeEventListener('scroll', this.handleScroll, true);
  75. }
  76. handleScroll = () => {
  77. if (this.container) {
  78. const rect = this.container.getBoundingClientRect();
  79. const windowHeight =
  80. window.innerHeight ||
  81. (document.documentElement ? document.documentElement.clientHeight : 0);
  82. if (rect.top <= windowHeight && rect.top + rect.height >= 0) {
  83. this.setState({ viewable: true });
  84. }
  85. }
  86. };
  87. orderProjectsFromProps = () => {
  88. const { projects } = this.props;
  89. // Last element should be put at the begining for proper carousel animation
  90. return [projects.pop(), ...projects].map((project: FeaturedProject, id) => {
  91. return {
  92. order: id,
  93. project
  94. };
  95. });
  96. };
  97. handlePrevClick = () => {
  98. this.setState(({ slides }) => ({
  99. reversing: true,
  100. sliding: true,
  101. slides: slides.map(slide => {
  102. slide.order = slide.order === slides.length - 1 ? 0 : slide.order + 1;
  103. return slide;
  104. })
  105. }));
  106. setTimeout(() => {
  107. if (this.mounted) {
  108. this.setState({ sliding: false });
  109. }
  110. }, 50);
  111. };
  112. handleNextClick = () => {
  113. this.setState(({ slides }) => ({
  114. reversing: false,
  115. sliding: true,
  116. slides: slides.map(slide => {
  117. slide.order = slide.order === 0 ? slides.length - 1 : slide.order - 1;
  118. return slide;
  119. })
  120. }));
  121. setTimeout(() => {
  122. this.setState({ sliding: false });
  123. }, 50);
  124. };
  125. render() {
  126. const { reversing, sliding, viewable } = this.state;
  127. return (
  128. <div
  129. className="sc-featured-projects sc-big-spacer-bottom"
  130. ref={node => (this.container = node)}>
  131. <button className="js-prev sc-project-button" onClick={this.handlePrevClick} type="button">
  132. <img alt="" src={`${getBaseUrl()}/images/sonarcloud/chevron-left.svg`} />
  133. </button>
  134. <div className="sc-featured-projects-container">
  135. <div
  136. className={classNames('sc-featured-projects-inner', {
  137. reversing,
  138. ready: !sliding
  139. })}>
  140. {this.state.slides.map(slide => (
  141. <ProjectCard
  142. key={slide.project.key}
  143. order={slide.order}
  144. project={slide.project}
  145. viewable={viewable}
  146. />
  147. ))}
  148. </div>
  149. </div>
  150. <button className="js-next sc-project-button" onClick={this.handleNextClick} type="button">
  151. <img alt="" src={`${getBaseUrl()}/images/sonarcloud/chevron-right.svg`} />
  152. </button>
  153. </div>
  154. );
  155. }
  156. }
  157. interface ProjectCardProps {
  158. order: number;
  159. project: FeaturedProject;
  160. viewable: boolean;
  161. }
  162. export function ProjectCard({ project, order, viewable }: ProjectCardProps) {
  163. return (
  164. <div className="sc-project-card-container" style={{ order }}>
  165. <Link className="sc-project-card" to={getProjectUrl(project.key)}>
  166. <div className="sc-project-card-header">
  167. <OrganizationAvatar
  168. className="no-border spacer-bottom"
  169. organization={{
  170. name: project.organizationName,
  171. avatar: project.avatarUrl || undefined
  172. }}
  173. />
  174. <p className="sc-project-card-limited" title={project.organizationName}>
  175. {project.organizationName}
  176. </p>
  177. <h5 className="sc-project-card-limited big-spacer-bottom" title={project.name}>
  178. {project.name}
  179. </h5>
  180. <Level level={project.gateStatus} />
  181. </div>
  182. <ul className="sc-project-card-measures">
  183. <ProjectIssues
  184. metric={project.bugs}
  185. metricKey="bugs"
  186. ratingMetric={project.reliabilityRating}
  187. viewable={viewable}
  188. />
  189. <ProjectIssues
  190. metric={project.vulnerabilities}
  191. metricKey="vulnerabilities"
  192. ratingMetric={project.securityRating}
  193. viewable={viewable}
  194. />
  195. <ProjectIssues
  196. metric={project.codeSmells}
  197. metricKey="code_smells"
  198. ratingMetric={project.maintainabilityRating}
  199. viewable={viewable}
  200. />
  201. <li>
  202. <span>{getMetricName('coverage')}</span>
  203. <div>
  204. {viewable && (
  205. <CountUp
  206. decimal="."
  207. decimals={1}
  208. delay={0}
  209. duration={4}
  210. end={project.coverage}
  211. suffix="%">
  212. {(data: { countUpRef?: React.RefObject<HTMLHeadingElement> }) => (
  213. <h6 className="display-inline-block big-spacer-right" ref={data.countUpRef}>
  214. 0
  215. </h6>
  216. )}
  217. </CountUp>
  218. )}
  219. <CoverageRating value={project.coverage} />
  220. </div>
  221. </li>
  222. <li>
  223. <span>{getMetricName('duplications')}</span>
  224. <div>
  225. {viewable && (
  226. <CountUp
  227. decimal="."
  228. decimals={1}
  229. delay={0}
  230. duration={4}
  231. end={project.duplications}
  232. suffix="%">
  233. {(data: { countUpRef?: React.RefObject<HTMLHeadingElement> }) => (
  234. <h6 className="display-inline-block big-spacer-right" ref={data.countUpRef}>
  235. 0
  236. </h6>
  237. )}
  238. </CountUp>
  239. )}
  240. <DuplicationsRating value={project.duplications} />
  241. </div>
  242. </li>
  243. </ul>
  244. <div className="sc-mention text-left big-spacer-top">
  245. {formatMeasure(project.ncloc, 'SHORT_INT')} lines of code /{' '}
  246. <ProjectCardLanguagesContainer
  247. className="display-inline-block"
  248. distribution={project.languages.join(';')}
  249. />
  250. </div>
  251. </Link>
  252. </div>
  253. );
  254. }
  255. interface ProjectIssues {
  256. metricKey: string;
  257. metric: number;
  258. ratingMetric: number;
  259. viewable: boolean;
  260. }
  261. export function ProjectIssues({ metric, metricKey, ratingMetric, viewable }: ProjectIssues) {
  262. const formattedString = formatMeasure(metric, 'SHORT_INT');
  263. const value = parseFloat(formattedString.slice(0, -1));
  264. const suffix = formattedString.substr(-1);
  265. return (
  266. <li>
  267. <span>{getMetricName(metricKey)}</span>
  268. <div>
  269. {viewable && (
  270. <CountUp delay={0} duration={4} end={value} suffix={suffix}>
  271. {(data: { countUpRef?: React.RefObject<HTMLHeadingElement> }) => (
  272. <h6 className="display-inline-block big-spacer-right" ref={data.countUpRef}>
  273. 0
  274. </h6>
  275. )}
  276. </CountUp>
  277. )}
  278. <Rating value={ratingMetric} />
  279. </div>
  280. </li>
  281. );
  282. }