aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/apps/code/components/ComponentMeasure.tsx
blob: 30d46001e75dcc8cea8ea23b484b99ebf27e3f83 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
 * SonarQube
 * Copyright (C) 2009-2024 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

import { ContentCell, NumericalCell, QualityGateIndicator, RatingCell } from 'design-system';
import * as React from 'react';
import Measure from '~sonar-aligned/components/measure/Measure';
import { formatMeasure } from '~sonar-aligned/helpers/measures';
import { Status } from '~sonar-aligned/types/common';
import { MetricKey, MetricType } from '~sonar-aligned/types/metrics';
import RatingComponent from '../../../app/components/metrics/RatingComponent';
import { getLeakValue } from '../../../components/measure/utils';
import {
  CCT_SOFTWARE_QUALITY_METRICS,
  OLD_TO_NEW_TAXONOMY_METRICS_MAP,
} from '../../../helpers/constants';
import {
  areCCTMeasuresComputed as areCCTMeasuresComputedFn,
  isDiffMetric,
} from '../../../helpers/measures';
import { useStandardExperienceMode } from '../../../queries/settings';
import { BranchLike } from '../../../types/branch-like';
import { isApplication, isProject } from '../../../types/component';
import { Metric, ComponentMeasure as TypeComponentMeasure } from '../../../types/types';

interface Props {
  branchLike?: BranchLike;
  component: TypeComponentMeasure;
  metric: Metric;
}

export default function ComponentMeasure(props: Props) {
  const { component, metric, branchLike } = props;
  const isProjectLike = isProject(component.qualifier) || isApplication(component.qualifier);
  const { data: isStandardMode } = useStandardExperienceMode();
  const isReleasability = metric.key === MetricKey.releasability_rating;

  let finalMetricKey = isProjectLike && isReleasability ? MetricKey.alert_status : metric.key;
  const finalMetricType = isProjectLike && isReleasability ? MetricType.Level : metric.type;

  const areCCTMeasuresComputed = !isStandardMode && areCCTMeasuresComputedFn(component.measures);
  finalMetricKey = areCCTMeasuresComputed
    ? (OLD_TO_NEW_TAXONOMY_METRICS_MAP[finalMetricKey as MetricKey] ?? finalMetricKey)
    : finalMetricKey;

  const measure = Array.isArray(component.measures)
    ? component.measures.find((measure) => measure.metric === finalMetricKey)
    : undefined;

  let value;
  if (
    measure?.value !== undefined &&
    CCT_SOFTWARE_QUALITY_METRICS.includes(measure.metric as MetricKey)
  ) {
    value = JSON.parse(measure.value).total;
  } else {
    value = isDiffMetric(metric.key) ? getLeakValue(measure) : measure?.value;
  }

  switch (finalMetricType) {
    case MetricType.Level: {
      const formatted = formatMeasure(value, MetricType.Level);

      return (
        <ContentCell className="sw-whitespace-nowrap">
          <QualityGateIndicator
            status={(value as Status) ?? 'NONE'}
            className="sw-mr-2"
            size="sm"
          />
          <span>{formatted}</span>
        </ContentCell>
      );
    }
    case MetricType.Rating:
      return (
        <RatingCell className="sw-whitespace-nowrap">
          <RatingComponent
            branchLike={branchLike}
            componentKey={component.key}
            ratingMetric={metric.key as MetricKey}
          />
        </RatingCell>
      );
    default:
      return (
        <NumericalCell className="sw-whitespace-nowrap">
          <Measure
            branchLike={branchLike}
            componentKey={component.key}
            metricKey={finalMetricKey}
            metricType={finalMetricType}
            value={value}
          />
        </NumericalCell>
      );
  }
}