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.

RatingTooltipContent.tsx 3.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2022 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 * as React from 'react';
  21. import withAppStateContext from '../../app/components/app-state/withAppStateContext';
  22. import { translate, translateWithParameters } from '../../helpers/l10n';
  23. import { formatMeasure, isDiffMetric } from '../../helpers/measures';
  24. import { AppState } from '../../types/appstate';
  25. import { MetricKey } from '../../types/metrics';
  26. import { GlobalSettingKeys } from '../../types/settings';
  27. import { KNOWN_RATINGS } from './utils';
  28. const RATING_GRID_SIZE = 4;
  29. const DIFF_METRIC_PREFIX_LENGTH = 4;
  30. const PERCENT_MULTIPLIER = 100;
  31. const GRID_INDEX_OFFSET = 2; // Rating of 2 should get index 0 (threshold between 1 and 2)
  32. export interface RatingTooltipContentProps {
  33. appState: AppState;
  34. metricKey: MetricKey | string;
  35. value: number | string;
  36. }
  37. function getMaintainabilityGrid(ratingGridSetting: string) {
  38. const numbers = ratingGridSetting
  39. .split(',')
  40. .map(s => parseFloat(s))
  41. .filter(n => !isNaN(n));
  42. return numbers.length === RATING_GRID_SIZE ? numbers : [0, 0, 0, 0];
  43. }
  44. export function RatingTooltipContent(props: RatingTooltipContentProps) {
  45. const {
  46. appState: { settings },
  47. metricKey,
  48. value
  49. } = props;
  50. const finalMetricKey = isDiffMetric(metricKey)
  51. ? metricKey.slice(DIFF_METRIC_PREFIX_LENGTH)
  52. : metricKey;
  53. if (!KNOWN_RATINGS.includes(finalMetricKey)) {
  54. return null;
  55. }
  56. const rating = Number(value);
  57. const ratingLetter = formatMeasure(value, 'RATING');
  58. if (finalMetricKey !== 'sqale_rating' && finalMetricKey !== 'maintainability_rating') {
  59. return <>{translate('metric', finalMetricKey, 'tooltip', ratingLetter)}</>;
  60. }
  61. const maintainabilityGrid = getMaintainabilityGrid(settings[GlobalSettingKeys.RatingGrid] ?? '');
  62. const maintainabilityRatingThreshold =
  63. maintainabilityGrid[Math.floor(rating) - GRID_INDEX_OFFSET];
  64. return (
  65. // Required to correctly satisfy the context typing
  66. // eslint-disable-next-line react/jsx-no-useless-fragment
  67. <>
  68. {rating === 1
  69. ? translateWithParameters(
  70. 'metric.sqale_rating.tooltip.A',
  71. formatMeasure(maintainabilityGrid[0] * PERCENT_MULTIPLIER, 'PERCENT')
  72. )
  73. : translateWithParameters(
  74. 'metric.sqale_rating.tooltip',
  75. ratingLetter,
  76. formatMeasure(maintainabilityRatingThreshold * PERCENT_MULTIPLIER, 'PERCENT')
  77. )}
  78. </>
  79. );
  80. }
  81. export default withAppStateContext(RatingTooltipContent);