aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/apps/overview/branches/SoftwareImpactMeasureCard.tsx
blob: e6df00f3ac98ff9681975d07b4a06f20575e1b61 (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
 * 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 styled from '@emotion/styled';
import classNames from 'classnames';
import {
  BasicSeparator,
  LightGreyCard,
  NakedLink,
  TextBold,
  TextSubdued,
  themeColor,
} from 'design-system';
import * as React from 'react';
import { useIntl } from 'react-intl';
import { DEFAULT_ISSUES_QUERY } from '../../../components/shared/utils';
import { formatMeasure, formatRating } from '../../../helpers/measures';
import { getComponentIssuesUrl } from '../../../helpers/urls';
import {
  SoftwareImpactMeasureData,
  SoftwareImpactSeverity,
  SoftwareQuality,
} from '../../../types/clean-code-taxonomy';
import { MetricKey, MetricType } from '../../../types/metrics';
import { Component, MeasureEnhanced } from '../../../types/types';
import { softwareQualityToMeasure } from '../utils';
import SoftwareImpactMeasureBreakdownCard from './SoftwareImpactMeasureBreakdownCard';
import SoftwareImpactMeasureRating from './SoftwareImpactMeasureRating';

export interface SoftwareImpactBreakdownCardProps {
  component: Component;
  softwareQuality: SoftwareQuality;
  ratingMetricKey: MetricKey;
  measures: MeasureEnhanced[];
}

export function SoftwareImpactMeasureCard(props: Readonly<SoftwareImpactBreakdownCardProps>) {
  const { component, softwareQuality, ratingMetricKey, measures } = props;

  const intl = useIntl();

  // Find measure for this software quality
  const metricKey = softwareQualityToMeasure(softwareQuality);
  const measureRaw = measures.find((m) => m.metric.key === metricKey);
  const measure = JSON.parse(measureRaw?.value ?? 'null') as SoftwareImpactMeasureData;

  // Find rating measure
  const ratingMeasure = measures.find((m) => m.metric.key === ratingMetricKey);
  const ratingLabel = ratingMeasure?.value ? formatRating(ratingMeasure.value) : undefined;

  const totalLinkHref = getComponentIssuesUrl(component.key, {
    ...DEFAULT_ISSUES_QUERY,
    impactSoftwareQualities: softwareQuality,
  });

  // We highlight the highest severity breakdown card with non-zero count if the rating is not A
  let highlightedSeverity: SoftwareImpactSeverity | undefined;
  if (measure && (!ratingLabel || ratingLabel !== 'A')) {
    highlightedSeverity = [
      SoftwareImpactSeverity.High,
      SoftwareImpactSeverity.Medium,
      SoftwareImpactSeverity.Low,
    ].find((severity) => measure[severity] > 0);
  }

  return (
    <LightGreyCard
      data-testid={`software-impact-card-${softwareQuality}`}
      className="sw-w-1/3 sw-overflow-hidden sw-rounded-2 sw-p-4 sw-flex-col"
    >
      <TextBold name={intl.formatMessage({ id: `software_quality.${softwareQuality}` })} />
      <BasicSeparator className="sw--mx-4" />
      <div className="sw-flex sw-flex-col sw-gap-3">
        <div
          className={classNames('sw-flex sw-gap-1 sw-items-end', {
            'sw-opacity-60': !measure,
          })}
        >
          {measure ? (
            <NakedLink
              aria-label={intl.formatMessage(
                {
                  id: `overview.measures.software_impact.see_list_of_x_open_issues`,
                },
                {
                  count: measure.total,
                  softwareQuality: intl.formatMessage({
                    id: `software_quality.${softwareQuality}`,
                  }),
                },
              )}
              className="sw-text-xl"
              to={totalLinkHref}
            >
              {formatMeasure(measure.total, MetricType.ShortInteger)}
            </NakedLink>
          ) : (
            <StyledDash className="sw-self-center sw-font-bold" name="-" />
          )}
          <TextSubdued className="sw-body-sm sw-mb-2">
            {intl.formatMessage({ id: 'overview.measures.software_impact.total_open_issues' })}
          </TextSubdued>
          <div className="sw-flex-grow sw-flex sw-justify-end">
            <SoftwareImpactMeasureRating
              softwareQuality={softwareQuality}
              value={ratingMeasure?.value}
            />
          </div>
        </div>
        <div className="sw-flex sw-gap-2">
          {[
            SoftwareImpactSeverity.High,
            SoftwareImpactSeverity.Medium,
            SoftwareImpactSeverity.Low,
          ].map((severity) => (
            <SoftwareImpactMeasureBreakdownCard
              key={severity}
              component={component}
              softwareQuality={softwareQuality}
              value={measure?.[severity]?.toString()}
              severity={severity}
              active={highlightedSeverity === severity}
            />
          ))}
        </div>
      </div>
      {!measure && (
        <>
          <BasicSeparator className="sw--mx-4 sw-mb-0 sw-mt-3" />
          <StyledInfoSection className="sw--ml-4 sw--mr-4 sw--mb-4 sw-text-xs sw-p-4 sw-flex sw-gap-1 sw-flex-wrap">
            <span>{intl.formatMessage({ id: 'overview.project.no_data' })}</span>
            <span>
              {intl.formatMessage({
                id: `overview.run_analysis_to_compute.${component.qualifier}`,
              })}
            </span>
          </StyledInfoSection>
        </>
      )}
    </LightGreyCard>
  );
}

const StyledDash = styled(TextBold)`
  font-size: 36px;
`;

const StyledInfoSection = styled.div`
  background-color: ${themeColor('overviewSoftwareImpactSeverityNeutral')};
`;

export default SoftwareImpactMeasureCard;