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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 { getDeprecatedTranslationKeyForTooltip } from './utils';
  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: MetricKey) => {
  64. const metricName = getLocalizedMetricName({ key });
  65. const isDeprecated = DEPRECATED_ACTIVITY_METRICS.includes(key);
  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: MetricKey) => {
  76. const isDeprecated = DEPRECATED_ACTIVITY_METRICS.includes(key);
  77. if (isDeprecated) {
  78. return <FormattedMessage id={getDeprecatedTranslationKeyForTooltip(key)} tagName="div" />;
  79. }
  80. return null;
  81. };
  82. return (
  83. <MultiSelectMenu
  84. createElementLabel=""
  85. searchInputAriaLabel={translate('project_activity.graphs.custom.select_metric')}
  86. allowNewElements={false}
  87. allowSelection={props.selectedElements.length < 6}
  88. elements={elements}
  89. filterSelected={props.filterSelected}
  90. footerNode={footerNode}
  91. noResultsLabel={translateWithParameters('no_results')}
  92. onSearch={props.onSearch}
  93. onSelect={(item: string) => elements.includes(item) && props.onSelect(item)}
  94. onUnselect={props.onUnselect}
  95. placeholder={translate('search.search_for_metrics')}
  96. renderLabel={renderLabel}
  97. renderTooltip={renderTooltip}
  98. selectedElements={props.selectedElements}
  99. listSize={0}
  100. />
  101. );
  102. }