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.

DomainSubnavigation.tsx 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 {
  21. BareButton,
  22. HelperHintIcon,
  23. SubnavigationAccordion,
  24. SubnavigationItem,
  25. SubnavigationSubheading,
  26. } from 'design-system';
  27. import React from 'react';
  28. import HelpTooltip from '../../../components/controls/HelpTooltip';
  29. import {
  30. getLocalizedCategoryMetricName,
  31. getLocalizedMetricDomain,
  32. getLocalizedMetricName,
  33. hasMessage,
  34. translate,
  35. } from '../../../helpers/l10n';
  36. import { MeasureEnhanced } from '../../../types/types';
  37. import {
  38. addMeasureCategories,
  39. getMetricSubnavigationName,
  40. hasBubbleChart,
  41. sortMeasures,
  42. } from '../utils';
  43. import DomainSubnavigationItem from './DomainSubnavigationItem';
  44. interface Props {
  45. domain: { measures: MeasureEnhanced[]; name: string };
  46. onChange: (metric: string) => void;
  47. open: boolean;
  48. selected: string;
  49. showFullMeasures: boolean;
  50. }
  51. export default function DomainSubnavigation(props: Readonly<Props>) {
  52. const { domain, onChange, open, selected, showFullMeasures } = props;
  53. const helperMessageKey = `component_measures.domain_subnavigation.${domain.name}.help`;
  54. const helper = hasMessage(helperMessageKey) ? translate(helperMessageKey) : undefined;
  55. const items = addMeasureCategories(domain.name, domain.measures);
  56. const hasCategories = items.some((item) => typeof item === 'string');
  57. const translateMetric = hasCategories ? getLocalizedCategoryMetricName : getLocalizedMetricName;
  58. let sortedItems = sortMeasures(domain.name, items);
  59. const hasOverview = (domain: string) => {
  60. return showFullMeasures && hasBubbleChart(domain);
  61. };
  62. // sortedItems contains both measures (type object) and categories (type string)
  63. // here we are filtering out categories that don't contain any measures (happen on the measures page for PRs)
  64. sortedItems = sortedItems.filter((item, index) => {
  65. return (
  66. typeof item === 'object' ||
  67. (index < sortedItems.length - 1 && typeof sortedItems[index + 1] === 'object')
  68. );
  69. });
  70. return (
  71. <SubnavigationAccordion
  72. header={
  73. <div className="sw-flex sw-items-center sw-gap-3">
  74. <strong className="sw-body-sm-highlight">{getLocalizedMetricDomain(domain.name)}</strong>
  75. {helper && (
  76. <HelpTooltip overlay={helper}>
  77. <HelperHintIcon aria-hidden="false" description={helper} />
  78. </HelpTooltip>
  79. )}
  80. </div>
  81. }
  82. initExpanded={open}
  83. id={`measure-${domain.name}`}
  84. >
  85. {hasOverview(domain.name) && (
  86. <SubnavigationItem active={domain.name === selected} onClick={onChange} value={domain.name}>
  87. <BareButton aria-current={domain.name === selected}>
  88. {translate('component_measures.domain_overview')}
  89. </BareButton>
  90. </SubnavigationItem>
  91. )}
  92. {sortedItems.map((item) =>
  93. typeof item === 'string' ? (
  94. showFullMeasures && (
  95. <SubnavigationSubheading key={item}>
  96. {translate('component_measures.subnavigation_category', item)}
  97. </SubnavigationSubheading>
  98. )
  99. ) : (
  100. <DomainSubnavigationItem
  101. key={item.metric.key}
  102. measure={item}
  103. name={getMetricSubnavigationName(item.metric, translateMetric)}
  104. onChange={onChange}
  105. selected={selected}
  106. />
  107. ),
  108. )}
  109. </SubnavigationAccordion>
  110. );
  111. }