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.

SoftwareImpactMeasureCard.tsx 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 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 styled from '@emotion/styled';
  21. import classNames from 'classnames';
  22. import {
  23. BasicSeparator,
  24. LightGreyCard,
  25. NakedLink,
  26. TextBold,
  27. TextSubdued,
  28. themeColor,
  29. } from 'design-system';
  30. import * as React from 'react';
  31. import { useIntl } from 'react-intl';
  32. import Tooltip from '../../../components/controls/Tooltip';
  33. import { DEFAULT_ISSUES_QUERY } from '../../../components/shared/utils';
  34. import { formatMeasure } from '../../../helpers/measures';
  35. import { getComponentIssuesUrl } from '../../../helpers/urls';
  36. import {
  37. SoftwareImpactMeasureData,
  38. SoftwareImpactSeverity,
  39. SoftwareQuality,
  40. } from '../../../types/clean-code-taxonomy';
  41. import { MetricKey, MetricType } from '../../../types/metrics';
  42. import { Component, MeasureEnhanced } from '../../../types/types';
  43. import { OverviewDisabledLinkTooltip } from '../components/OverviewDisabledLinkTooltip';
  44. import { softwareQualityToMeasure } from '../utils';
  45. import SoftwareImpactMeasureBreakdownCard from './SoftwareImpactMeasureBreakdownCard';
  46. import SoftwareImpactMeasureRating from './SoftwareImpactMeasureRating';
  47. export interface SoftwareImpactBreakdownCardProps {
  48. component: Component;
  49. softwareQuality: SoftwareQuality;
  50. ratingMetricKey: MetricKey;
  51. measures: MeasureEnhanced[];
  52. }
  53. export function SoftwareImpactMeasureCard(props: Readonly<SoftwareImpactBreakdownCardProps>) {
  54. const { component, softwareQuality, ratingMetricKey, measures } = props;
  55. const intl = useIntl();
  56. // Find measure for this software quality
  57. const metricKey = softwareQualityToMeasure(softwareQuality);
  58. const measureRaw = measures.find((m) => m.metric.key === metricKey);
  59. const measure = JSON.parse(measureRaw?.value ?? 'null') as SoftwareImpactMeasureData;
  60. const renderDisabled = !measure || component.needIssueSync;
  61. // Find rating measure
  62. const ratingMeasure = measures.find((m) => m.metric.key === ratingMetricKey);
  63. const totalLinkHref = getComponentIssuesUrl(component.key, {
  64. ...DEFAULT_ISSUES_QUERY,
  65. impactSoftwareQualities: softwareQuality,
  66. });
  67. // We highlight the highest severity breakdown card with non-zero count
  68. const highlightedSeverity =
  69. measure &&
  70. [SoftwareImpactSeverity.High, SoftwareImpactSeverity.Medium, SoftwareImpactSeverity.Low].find(
  71. (severity) => measure[severity] > 0,
  72. );
  73. const countTooltipOverlay = component.needIssueSync ? (
  74. <OverviewDisabledLinkTooltip />
  75. ) : (
  76. intl.formatMessage({ id: 'overview.measures.software_impact.count_tooltip' })
  77. );
  78. return (
  79. <LightGreyCard
  80. data-testid={`overview__software-impact-card-${softwareQuality}`}
  81. className="sw-w-1/3 sw-overflow-hidden sw-rounded-2 sw-p-4 sw-flex-col"
  82. >
  83. <TextBold name={intl.formatMessage({ id: `software_quality.${softwareQuality}` })} />
  84. <BasicSeparator className="sw--mx-4" />
  85. <div className="sw-flex sw-flex-col sw-gap-3">
  86. <div
  87. className={classNames('sw-flex sw-gap-1 sw-items-center sw-h-8 sw-mt-2', {
  88. 'sw-opacity-60': renderDisabled,
  89. })}
  90. >
  91. {measure ? (
  92. <Tooltip overlay={countTooltipOverlay}>
  93. <NakedLink
  94. data-testid={`overview__software-impact-${softwareQuality}`}
  95. aria-label={intl.formatMessage(
  96. {
  97. id: `overview.measures.software_impact.see_list_of_x_open_issues`,
  98. },
  99. {
  100. count: measure.total,
  101. softwareQuality: intl.formatMessage({
  102. id: `software_quality.${softwareQuality}`,
  103. }),
  104. },
  105. )}
  106. className="sw-text-lg"
  107. to={totalLinkHref}
  108. disabled={component.needIssueSync}
  109. >
  110. {formatMeasure(measure.total, MetricType.ShortInteger)}
  111. </NakedLink>
  112. </Tooltip>
  113. ) : (
  114. <StyledDash className="sw-font-bold" name="-" />
  115. )}
  116. <TextSubdued className="sw-self-end sw-body-sm sw-pb-1">
  117. {intl.formatMessage({ id: 'overview.measures.software_impact.total_open_issues' })}
  118. </TextSubdued>
  119. <div className="sw-flex-grow sw-flex sw-justify-end">
  120. <SoftwareImpactMeasureRating
  121. softwareQuality={softwareQuality}
  122. value={ratingMeasure?.value}
  123. />
  124. </div>
  125. </div>
  126. <div className="sw-flex sw-gap-2">
  127. {[
  128. SoftwareImpactSeverity.High,
  129. SoftwareImpactSeverity.Medium,
  130. SoftwareImpactSeverity.Low,
  131. ].map((severity) => (
  132. <SoftwareImpactMeasureBreakdownCard
  133. key={severity}
  134. component={component}
  135. softwareQuality={softwareQuality}
  136. value={measure?.[severity]?.toString()}
  137. severity={severity}
  138. active={highlightedSeverity === severity}
  139. />
  140. ))}
  141. </div>
  142. </div>
  143. {!measure && (
  144. <>
  145. <BasicSeparator className="sw--mx-4 sw-mb-0 sw-mt-3" />
  146. <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">
  147. <span>{intl.formatMessage({ id: 'overview.project.no_data' })}</span>
  148. <span>
  149. {intl.formatMessage({
  150. id: `overview.run_analysis_to_compute.${component.qualifier}`,
  151. })}
  152. </span>
  153. </StyledInfoSection>
  154. </>
  155. )}
  156. </LightGreyCard>
  157. );
  158. }
  159. const StyledDash = styled(TextBold)`
  160. font-size: 36px;
  161. `;
  162. const StyledInfoSection = styled.div`
  163. background-color: ${themeColor('overviewSoftwareImpactSeverityNeutral')};
  164. `;
  165. export default SoftwareImpactMeasureCard;