From a7d7420a719ee56590e5c09d70bfd1a75a14abdf Mon Sep 17 00:00:00 2001 From: Pascal Mugnier Date: Fri, 20 Apr 2018 07:38:14 +0200 Subject: SONAR-9472 Change the rendering of best values on the Measures page --- .../components/MeasureContent.js | 5 + .../component-measures/drilldown/ComponentsList.js | 152 ++++++++++++++------- .../apps/component-measures/drilldown/FilesView.js | 2 + server/sonar-web/src/main/js/helpers/measures.ts | 10 ++ .../sonar-web/src/main/js/store/metrics/actions.js | 1 + 5 files changed, 122 insertions(+), 48 deletions(-) (limited to 'server/sonar-web/src') diff --git a/server/sonar-web/src/main/js/apps/component-measures/components/MeasureContent.js b/server/sonar-web/src/main/js/apps/component-measures/components/MeasureContent.js index a4847ed1848..a7cd534be4c 100644 --- a/server/sonar-web/src/main/js/apps/component-measures/components/MeasureContent.js +++ b/server/sonar-web/src/main/js/apps/component-measures/components/MeasureContent.js @@ -62,6 +62,7 @@ import { isSameBranchLike, getBranchLikeQuery } from '../../../helpers/branches' |}; */ /*:: type State = { + bestValue?: string, components: Array, metric: ?Metric, paging?: Paging, @@ -117,6 +118,7 @@ export default class MeasureContent extends React.PureComponent { const metricKeys = [metric.key]; const opts /*: Object */ = { ...getBranchLikeQuery(this.props.branchLike), + additionalFields: 'metrics', metricSortFilter: 'withMeasuresOnly' }; const isDiff = isDiffMetric(metric.key); @@ -151,6 +153,7 @@ export default class MeasureContent extends React.PureComponent { if (metric === this.props.metric) { if (this.mounted) { this.setState(({ selected } /*: State */) => ({ + bestValue: r.metrics[0].bestValue, components: r.components.map(component => enhanceComponent(component, metric, metrics) ), @@ -185,6 +188,7 @@ export default class MeasureContent extends React.PureComponent { if (metric === this.props.metric) { if (this.mounted) { this.setState(state => ({ + bestValue: r.metrics[0].bestValue, components: [ ...state.components, ...r.components.map(component => enhanceComponent(component, metric, metrics)) @@ -245,6 +249,7 @@ export default class MeasureContent extends React.PureComponent { const selectedIdx = this.getSelectedIndex(); return ( , onClick: string => void, @@ -36,54 +38,108 @@ import { getLocalizedMetricName } from '../../../helpers/l10n'; selectedComponent?: ?string |}; */ -export default function ComponentsList( - { - branchLike, - components, - onClick, - metrics, - metric, - rootComponent, - selectedComponent - } /*: Props */ -) { - if (!components.length) { - return ; +/*:: type State = { + hideBest: boolean +}; */ + +export default class ComponentsList extends React.PureComponent { + /*:: props: Props; */ + state /*: State */ = { + hideBest: true + }; + + componentWillReceiveProps(nextProps /*: Props */) { + if (nextProps.metric !== this.props.metric) { + this.setState({ hideBest: true }); + } + } + + displayAll = (event /*: Event */) => { + event.preventDefault(); + this.setState({ hideBest: false }); + }; + + hasBestValue(component /*: Component*/, otherMetrics /*: Array */) { + const { metric } = this.props; + const focusedMeasure = component.measures.find(measure => measure.metric.key === metric.key); + if (isDiffMetric(focusedMeasure.metric.key)) { + return isPeriodBestValue(focusedMeasure, 1); + } + return focusedMeasure.bestValue; } - const otherMetrics = (complementary[metric.key] || []).map(key => metrics[key]); - return ( - - {otherMetrics.length > 0 && ( - - - - - {otherMetrics.map(metric => ( - - ))} - - - )} + renderComponent(component /*: Component*/, otherMetrics /*: Array */) { + const { branchLike, metric, selectedComponent, onClick, rootComponent } = this.props; + return ( + + ); + } - - {components.map(component => ( - - ))} - -
  - {getLocalizedMetricName(metric)} - - {getLocalizedMetricName(metric)} -
- ); + renderHiddenLink(hiddenCount /*: number*/, colCount /*: number*/) { + return ( +
+ {translateWithParameters( + 'component_measures.hidden_best_score_metrics', + hiddenCount, + formatMeasure(this.props.bestValue, this.props.metric.type) + )} + + {translate('show_all')} + +
+ ); + } + + render() { + const { components, metric, metrics } = this.props; + if (!components.length) { + return ; + } + + const otherMetrics = (complementary[metric.key] || []).map(key => metrics[key]); + const notBestComponents = components.filter( + component => !this.hasBestValue(component, otherMetrics) + ); + const hiddenCount = components.length - notBestComponents.length; + const shouldHideBest = this.state.hideBest && hiddenCount !== components.length; + return ( + + + {otherMetrics.length > 0 && ( + + + + + {otherMetrics.map(metric => ( + + ))} + + + )} + + + {(shouldHideBest ? notBestComponents : components).map(component => + this.renderComponent(component, otherMetrics) + )} + +
  + {getLocalizedMetricName(metric)} + + {getLocalizedMetricName(metric)} +
+ {shouldHideBest && + hiddenCount > 0 && + this.renderHiddenLink(hiddenCount, otherMetrics.length + 3)} +
+ ); + } } diff --git a/server/sonar-web/src/main/js/apps/component-measures/drilldown/FilesView.js b/server/sonar-web/src/main/js/apps/component-measures/drilldown/FilesView.js index 4c1fbb8ce08..93f464ef47b 100644 --- a/server/sonar-web/src/main/js/apps/component-measures/drilldown/FilesView.js +++ b/server/sonar-web/src/main/js/apps/component-measures/drilldown/FilesView.js @@ -28,6 +28,7 @@ import { scrollToElement } from '../../../helpers/scrolling'; /*:: import type { Metric } from '../../../store/metrics/actions'; */ /*:: type Props = {| + bestValue?: string, branchLike?: { id?: string; name: string }, components: Array, fetchMore: () => void, @@ -124,6 +125,7 @@ export default class ListView extends React.PureComponent { return (
(this.listContainer = elem)}> period.index === periodIndex); + return (period && period.bestValue) || false; +} + /** Check if metric is differential */ export function isDiffMetric(metricKey: string): boolean { return metricKey.indexOf('new_') === 0; diff --git a/server/sonar-web/src/main/js/store/metrics/actions.js b/server/sonar-web/src/main/js/store/metrics/actions.js index 31f4cc198e7..a271928ce3b 100644 --- a/server/sonar-web/src/main/js/store/metrics/actions.js +++ b/server/sonar-web/src/main/js/store/metrics/actions.js @@ -19,6 +19,7 @@ */ // @flow /*:: export type Metric = { + bestValue?: string, custom?: boolean, decimalScale?: number, description?: string, -- cgit v1.2.3