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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 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 { LabelValueSelectOption, SearchSelectDropdown } from 'design-system';
  21. import { sortBy } from 'lodash';
  22. import * as React from 'react';
  23. import { Options } from 'react-select';
  24. import withMetricsContext from '../../../app/components/metrics/withMetricsContext';
  25. import { getLocalizedMetricDomain, translate } from '../../../helpers/l10n';
  26. import { Dict, Metric } from '../../../types/types';
  27. import { getLocalizedMetricNameNoDiffMetric } from '../utils';
  28. interface Props {
  29. metric?: Metric;
  30. metricsArray: Metric[];
  31. metrics: Dict<Metric>;
  32. onMetricChange: (metric: Metric) => void;
  33. }
  34. interface Option {
  35. isDisabled?: boolean;
  36. label: string;
  37. value: string;
  38. }
  39. export function MetricSelect({ metric, metricsArray, metrics, onMetricChange }: Readonly<Props>) {
  40. const handleChange = (option: Option | null) => {
  41. if (option) {
  42. const selectedMetric = metricsArray.find((metric) => metric.key === option.value);
  43. if (selectedMetric) {
  44. onMetricChange(selectedMetric);
  45. }
  46. }
  47. };
  48. const options: Array<Option & { domain?: string }> = sortBy(
  49. metricsArray.map((m) => ({
  50. value: m.key,
  51. label: getLocalizedMetricNameNoDiffMetric(m, metrics),
  52. domain: m.domain,
  53. })),
  54. 'domain',
  55. );
  56. // Use "disabled" property to emulate optgroups.
  57. const optionsWithDomains: Option[] = [];
  58. options.forEach((option, index, options) => {
  59. const previous = index > 0 ? options[index - 1] : null;
  60. if (option.domain && (!previous || previous.domain !== option.domain)) {
  61. optionsWithDomains.push({
  62. value: '<domain>',
  63. label: getLocalizedMetricDomain(option.domain),
  64. isDisabled: true,
  65. });
  66. }
  67. optionsWithDomains.push(option);
  68. });
  69. const handleAssigneeSearch = React.useCallback(
  70. (query: string, resolve: (options: Options<LabelValueSelectOption<string>>) => void) => {
  71. resolve(options.filter((opt) => opt.label.toLowerCase().includes(query.toLowerCase())));
  72. },
  73. [options],
  74. );
  75. return (
  76. <SearchSelectDropdown
  77. aria-label={translate('search.search_for_metrics')}
  78. size="large"
  79. controlSize="full"
  80. inputId="condition-metric"
  81. defaultOptions={optionsWithDomains}
  82. loadOptions={handleAssigneeSearch}
  83. onChange={handleChange}
  84. placeholder={translate('search.search_for_metrics')}
  85. controlLabel={
  86. optionsWithDomains.find((o) => o.value === metric?.key)?.label ?? translate('select_verb')
  87. }
  88. />
  89. );
  90. }
  91. export default withMetricsContext(MetricSelect);