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.

FilesView.tsx 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 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 key from 'keymaster';
  21. import { throttle } from 'lodash';
  22. import * as React from 'react';
  23. import { Button } from '../../../components/controls/buttons';
  24. import ListFooter from '../../../components/controls/ListFooter';
  25. import { Alert } from '../../../components/ui/Alert';
  26. import { translate, translateWithParameters } from '../../../helpers/l10n';
  27. import { formatMeasure, isDiffMetric, isPeriodBestValue } from '../../../helpers/measures';
  28. import { scrollToElement } from '../../../helpers/scrolling';
  29. import { BranchLike } from '../../../types/branch-like';
  30. import { MeasurePageView } from '../../../types/measures';
  31. import ComponentsList from './ComponentsList';
  32. interface Props {
  33. branchLike?: BranchLike;
  34. components: T.ComponentMeasureEnhanced[];
  35. defaultShowBestMeasures: boolean;
  36. fetchMore: () => void;
  37. handleSelect: (component: T.ComponentMeasureEnhanced) => void;
  38. handleOpen: (component: T.ComponentMeasureEnhanced) => void;
  39. loadingMore: boolean;
  40. metric: T.Metric;
  41. metrics: T.Dict<T.Metric>;
  42. paging?: T.Paging;
  43. rootComponent: T.ComponentMeasure;
  44. selectedComponent?: T.ComponentMeasureEnhanced;
  45. selectedIdx?: number;
  46. view: MeasurePageView;
  47. }
  48. interface State {
  49. showBestMeasures: boolean;
  50. }
  51. const keyScope = 'measures-files';
  52. export default class FilesView extends React.PureComponent<Props, State> {
  53. listContainer?: HTMLElement | null;
  54. constructor(props: Props) {
  55. super(props);
  56. this.state = { showBestMeasures: props.defaultShowBestMeasures };
  57. this.selectNext = throttle(this.selectNext, 100);
  58. this.selectPrevious = throttle(this.selectPrevious, 100);
  59. }
  60. componentDidMount() {
  61. this.attachShortcuts();
  62. if (this.props.selectedComponent !== undefined) {
  63. this.scrollToElement();
  64. }
  65. }
  66. componentDidUpdate(prevProps: Props) {
  67. if (
  68. this.props.selectedComponent &&
  69. prevProps.selectedComponent !== this.props.selectedComponent
  70. ) {
  71. this.scrollToElement();
  72. }
  73. if (prevProps.metric.key !== this.props.metric.key || prevProps.view !== this.props.view) {
  74. this.setState({ showBestMeasures: this.props.defaultShowBestMeasures });
  75. }
  76. }
  77. componentWillUnmount() {
  78. this.detachShortcuts();
  79. }
  80. attachShortcuts() {
  81. key('up', keyScope, () => {
  82. this.selectPrevious();
  83. return false;
  84. });
  85. key('down', keyScope, () => {
  86. this.selectNext();
  87. return false;
  88. });
  89. key('right', keyScope, () => {
  90. this.openSelected();
  91. return false;
  92. });
  93. }
  94. detachShortcuts() {
  95. ['up', 'down', 'right'].forEach(action => key.unbind(action, keyScope));
  96. }
  97. getVisibleComponents = () => {
  98. const { components } = this.props;
  99. if (this.state.showBestMeasures) {
  100. return components;
  101. }
  102. const filtered = components.filter(component => !this.hasBestValue(component));
  103. if (filtered.length === 0) {
  104. return components;
  105. }
  106. return filtered;
  107. };
  108. handleShowBestMeasures = () => {
  109. this.setState({ showBestMeasures: true });
  110. };
  111. hasBestValue = (component: T.ComponentMeasureEnhanced) => {
  112. const { metric } = this.props;
  113. const focusedMeasure = component.measures.find(measure => measure.metric.key === metric.key);
  114. if (focusedMeasure && isDiffMetric(metric.key)) {
  115. return isPeriodBestValue(focusedMeasure);
  116. }
  117. return Boolean(focusedMeasure && focusedMeasure.bestValue);
  118. };
  119. openSelected = () => {
  120. if (this.props.selectedComponent !== undefined) {
  121. this.props.handleOpen(this.props.selectedComponent);
  122. }
  123. };
  124. selectPrevious = () => {
  125. const { selectedIdx } = this.props;
  126. const visibleComponents = this.getVisibleComponents();
  127. if (selectedIdx !== undefined && selectedIdx > 0) {
  128. this.props.handleSelect(visibleComponents[selectedIdx - 1]);
  129. } else {
  130. this.props.handleSelect(visibleComponents[visibleComponents.length - 1]);
  131. }
  132. };
  133. selectNext = () => {
  134. const { selectedIdx } = this.props;
  135. const visibleComponents = this.getVisibleComponents();
  136. if (selectedIdx !== undefined && selectedIdx < visibleComponents.length - 1) {
  137. this.props.handleSelect(visibleComponents[selectedIdx + 1]);
  138. } else {
  139. this.props.handleSelect(visibleComponents[0]);
  140. }
  141. };
  142. scrollToElement = () => {
  143. if (this.listContainer) {
  144. const elem = this.listContainer.getElementsByClassName('selected')[0];
  145. if (elem) {
  146. scrollToElement(elem, { topOffset: 215, bottomOffset: 100 });
  147. }
  148. }
  149. };
  150. render() {
  151. const { components } = this.props;
  152. const filteredComponents = this.getVisibleComponents();
  153. const hidingBestMeasures = filteredComponents.length < components.length;
  154. return (
  155. <div ref={elem => (this.listContainer = elem)}>
  156. <ComponentsList
  157. branchLike={this.props.branchLike}
  158. components={filteredComponents}
  159. metric={this.props.metric}
  160. metrics={this.props.metrics}
  161. rootComponent={this.props.rootComponent}
  162. selectedComponent={this.props.selectedComponent}
  163. view={this.props.view}
  164. />
  165. {hidingBestMeasures && this.props.paging && (
  166. <Alert className="spacer-top" variant="info">
  167. <div className="display-flex-center">
  168. {translateWithParameters(
  169. 'component_measures.hidden_best_score_metrics',
  170. formatMeasure(this.props.paging.total - filteredComponents.length, 'INT'),
  171. formatMeasure(this.props.metric.bestValue, this.props.metric.type)
  172. )}
  173. <Button className="button-small spacer-left" onClick={this.handleShowBestMeasures}>
  174. {translate('show_them')}
  175. </Button>
  176. </div>
  177. </Alert>
  178. )}
  179. {!hidingBestMeasures && this.props.paging && this.props.components.length > 0 && (
  180. <ListFooter
  181. count={this.props.components.length}
  182. loadMore={this.props.fetchMore}
  183. loading={this.props.loadingMore}
  184. total={this.props.paging.total}
  185. />
  186. )}
  187. </div>
  188. );
  189. }
  190. }