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.

Measure.tsx 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 { MetricsLabel, MetricsRatingBadge, QualityGateIndicator } from 'design-system';
  21. import * as React from 'react';
  22. import Tooltip from '../../components/controls/Tooltip';
  23. import { translate, translateWithParameters } from '../../helpers/l10n';
  24. import { formatMeasure } from '../../helpers/measures';
  25. import { MetricType } from '../../types/metrics';
  26. import { Status } from '../../types/types';
  27. import RatingTooltipContent from './RatingTooltipContent';
  28. interface Props {
  29. className?: string;
  30. decimals?: number | null;
  31. metricKey: string;
  32. metricType: string;
  33. small?: boolean;
  34. value: string | number | undefined;
  35. ratingComponent?: JSX.Element;
  36. }
  37. export default function Measure({
  38. className,
  39. decimals,
  40. metricKey,
  41. metricType,
  42. small,
  43. value,
  44. ratingComponent,
  45. }: Props) {
  46. if (value === undefined) {
  47. return <span className={className}>—</span>;
  48. }
  49. if (metricType === MetricType.Level) {
  50. const formatted = formatMeasure(value, MetricType.Level);
  51. const ariaLabel = translateWithParameters('overview.quality_gate_x', formatted);
  52. return (
  53. <>
  54. <QualityGateIndicator
  55. status={(value as Status) ?? 'NONE'}
  56. className="sw-mr-2"
  57. ariaLabel={ariaLabel}
  58. size={small ? 'sm' : 'md'}
  59. />
  60. <span className={small ? '' : 'sw-body-md'}>{formatted}</span>
  61. </>
  62. );
  63. }
  64. if (metricType !== MetricType.Rating) {
  65. const formattedValue = formatMeasure(value, metricType, {
  66. decimals,
  67. omitExtraDecimalZeros: metricType === MetricType.Percent,
  68. });
  69. return <span className={className}>{formattedValue ?? '—'}</span>;
  70. }
  71. const tooltip = <RatingTooltipContent metricKey={metricKey} value={value} />;
  72. const rating = ratingComponent ?? (
  73. <MetricsRatingBadge
  74. size={small ? 'sm' : 'md'}
  75. label={
  76. value
  77. ? translateWithParameters('metric.has_rating_X', formatMeasure(value, MetricType.Rating))
  78. : translate('metric.no_rating')
  79. }
  80. rating={formatMeasure(value, MetricType.Rating) as MetricsLabel}
  81. />
  82. );
  83. return (
  84. <Tooltip overlay={tooltip}>
  85. <span className={className}>{rating}</span>
  86. </Tooltip>
  87. );
  88. }