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 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 Tooltip from '../../components/controls/Tooltip';
  22. import Level from '../../components/ui/Level';
  23. import Rating from '../../components/ui/Rating';
  24. import { formatMeasure } from '../../helpers/measures';
  25. import RatingTooltipContent from './RatingTooltipContent';
  26. interface Props {
  27. className?: string;
  28. decimals?: number | null;
  29. metricKey: string;
  30. metricType: string;
  31. small?: boolean;
  32. value: string | undefined;
  33. }
  34. export default function Measure({
  35. className,
  36. decimals,
  37. metricKey,
  38. metricType,
  39. small,
  40. value
  41. }: Props) {
  42. if (value === undefined) {
  43. return <span className={className}>–</span>;
  44. }
  45. if (metricType === 'LEVEL') {
  46. return <Level className={className} level={value} small={small} />;
  47. }
  48. if (metricType !== 'RATING') {
  49. const formattedValue = formatMeasure(value, metricType, {
  50. decimals,
  51. omitExtraDecimalZeros: metricType === 'PERCENT'
  52. });
  53. return <span className={className}>{formattedValue != null ? formattedValue : '–'}</span>;
  54. }
  55. const tooltip = <RatingTooltipContent metricKey={metricKey} value={value} />;
  56. const rating = <Rating small={small} value={value} />;
  57. if (tooltip) {
  58. return (
  59. <Tooltip overlay={tooltip}>
  60. <span className={className}>{rating}</span>
  61. </Tooltip>
  62. );
  63. }
  64. return rating;
  65. }