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.

DopSettingDropdown.tsx 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 classNames from 'classnames';
  21. import { DarkLabel, InputSelect, LabelValueSelectOption, Note } from 'design-system';
  22. import * as React from 'react';
  23. import { FormattedMessage } from 'react-intl';
  24. import { OptionProps, SingleValueProps, components } from 'react-select';
  25. import { translate } from '../../../../helpers/l10n';
  26. import { AlmKeys } from '../../../../types/alm-settings';
  27. import { DopSetting } from '../../../../types/dop-translation';
  28. export interface DopSettingDropdownProps {
  29. almKey: AlmKeys;
  30. className?: string;
  31. dopSettings?: DopSetting[];
  32. onChangeSetting: (setting: DopSetting) => void;
  33. selectedDopSetting?: DopSetting;
  34. }
  35. const MIN_SIZE_INSTANCES = 2;
  36. function optionRenderer(props: OptionProps<LabelValueSelectOption<DopSetting>, false>) {
  37. return <components.Option {...props}>{customOptions(props.data.value)}</components.Option>;
  38. }
  39. function singleValueRenderer(props: SingleValueProps<LabelValueSelectOption<DopSetting>, false>) {
  40. return (
  41. <components.SingleValue {...props}>{customOptions(props.data.value)}</components.SingleValue>
  42. );
  43. }
  44. function customOptions(setting: DopSetting) {
  45. return setting.url ? (
  46. <>
  47. <span>{setting.key} — </span>
  48. <Note>{setting.url}</Note>
  49. </>
  50. ) : (
  51. <span>{setting.key}</span>
  52. );
  53. }
  54. function orgToOption(alm: DopSetting) {
  55. return { value: alm, label: alm.key };
  56. }
  57. export default function DopSettingDropdown(props: Readonly<DopSettingDropdownProps>) {
  58. const { almKey, className, dopSettings, onChangeSetting, selectedDopSetting } = props;
  59. if (!dopSettings || dopSettings.length < MIN_SIZE_INSTANCES) {
  60. return null;
  61. }
  62. return (
  63. <div className={classNames('sw-flex sw-flex-col', className)}>
  64. <DarkLabel htmlFor="dop-setting-dropdown" className="sw-mb-2">
  65. <FormattedMessage id={`onboarding.create_project.monorepo.choose_dop_setting.${almKey}`} />
  66. </DarkLabel>
  67. <InputSelect
  68. inputId="dop-setting-dropdown"
  69. className={className}
  70. isClearable={false}
  71. isSearchable={false}
  72. options={dopSettings.map(orgToOption)}
  73. onChange={(data: LabelValueSelectOption<DopSetting>) => {
  74. onChangeSetting(data.value);
  75. }}
  76. components={{
  77. Option: optionRenderer,
  78. SingleValue: singleValueRenderer,
  79. }}
  80. placeholder={translate('alm.configuration.selector.placeholder')}
  81. getOptionValue={(opt: LabelValueSelectOption<DopSetting>) => opt.value.key}
  82. value={
  83. dopSettings.map(orgToOption).find((opt) => opt.value.key === selectedDopSetting?.key) ??
  84. null
  85. }
  86. size="full"
  87. />
  88. </div>
  89. );
  90. }