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.

AddGraphMetricPopup.tsx 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 { Badge, FlagMessage, MultiSelectMenu } from 'design-system';
  21. import * as React from 'react';
  22. import { FormattedMessage, useIntl } from 'react-intl';
  23. import { DEPRECATED_ACTIVITY_METRICS } from '../../helpers/constants';
  24. import { getLocalizedMetricName, translate, translateWithParameters } from '../../helpers/l10n';
  25. import { MetricKey } from '../../types/metrics';
  26. import DocumentationLink from '../common/DocumentationLink';
  27. export interface AddGraphMetricPopupProps {
  28. elements: string[];
  29. filterSelected: (query: string, selectedElements: string[]) => string[];
  30. metricsTypeFilter?: string[];
  31. onSearch: (query: string) => Promise<void>;
  32. onSelect: (item: string) => void;
  33. onUnselect: (item: string) => void;
  34. popupPosition?: any;
  35. selectedElements: string[];
  36. }
  37. export default function AddGraphMetricPopup({
  38. elements,
  39. metricsTypeFilter,
  40. ...props
  41. }: AddGraphMetricPopupProps) {
  42. const intl = useIntl();
  43. let footerNode: React.ReactNode = '';
  44. if (props.selectedElements.length >= 6) {
  45. footerNode = (
  46. <FlagMessage className="sw-m-2" variant="info">
  47. {translate('project_activity.graphs.custom.add_metric_info')}
  48. </FlagMessage>
  49. );
  50. } else if (metricsTypeFilter && metricsTypeFilter.length > 0) {
  51. footerNode = (
  52. <FlagMessage className="sw-m-2" variant="info">
  53. {translateWithParameters(
  54. 'project_activity.graphs.custom.type_x_message',
  55. metricsTypeFilter
  56. .map((type: string) => translate('metric.type', type))
  57. .sort((a, b) => a.localeCompare(b))
  58. .join(', '),
  59. )}
  60. </FlagMessage>
  61. );
  62. }
  63. const renderLabel = (key: string) => {
  64. const metricName = getLocalizedMetricName({ key });
  65. const isDeprecated = DEPRECATED_ACTIVITY_METRICS.includes(key as MetricKey);
  66. return (
  67. <>
  68. {metricName}
  69. {isDeprecated && (
  70. <Badge className="sw-ml-1">{intl.formatMessage({ id: 'deprecated' })}</Badge>
  71. )}
  72. </>
  73. );
  74. };
  75. const renderTooltip = (key: string) => {
  76. const isDeprecated = DEPRECATED_ACTIVITY_METRICS.includes(key as MetricKey);
  77. if (isDeprecated) {
  78. return (
  79. <FormattedMessage
  80. id="project_activity.custom_metric.deprecated"
  81. tagName="div"
  82. values={{
  83. learn_more: (
  84. <DocumentationLink
  85. className="sw-ml-2 sw-whitespace-nowrap"
  86. to="/user-guide/clean-code/code-analysis/"
  87. >
  88. {intl.formatMessage({ id: 'learn_more' })}
  89. </DocumentationLink>
  90. ),
  91. }}
  92. />
  93. );
  94. }
  95. return null;
  96. };
  97. return (
  98. <MultiSelectMenu
  99. createElementLabel=""
  100. searchInputAriaLabel={translate('project_activity.graphs.custom.select_metric')}
  101. allowNewElements={false}
  102. allowSelection={props.selectedElements.length < 6}
  103. elements={elements}
  104. filterSelected={props.filterSelected}
  105. footerNode={footerNode}
  106. noResultsLabel={translateWithParameters('no_results')}
  107. onSearch={props.onSearch}
  108. onSelect={(item: string) => elements.includes(item) && props.onSelect(item)}
  109. onUnselect={props.onUnselect}
  110. placeholder={translate('search.search_for_metrics')}
  111. renderLabel={renderLabel}
  112. renderTooltip={renderTooltip}
  113. selectedElements={props.selectedElements}
  114. listSize={0}
  115. />
  116. );
  117. }