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.

OrganizationSelect.tsx 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 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 * as React from 'react';
  21. import { sortBy } from 'lodash';
  22. import { translate } from 'sonar-ui-common/helpers/l10n';
  23. import { getBaseUrl } from 'sonar-ui-common/helpers/urls';
  24. import Select from 'sonar-ui-common/components/controls/Select';
  25. import { sanitizeAlmId } from '../../../helpers/almIntegrations';
  26. interface Props {
  27. hideIcons?: boolean;
  28. onChange: (organization: T.Organization) => void;
  29. organization: string;
  30. organizations: T.Organization[];
  31. }
  32. export default function OrganizationSelect({
  33. hideIcons,
  34. onChange,
  35. organization,
  36. organizations
  37. }: Props) {
  38. const optionRenderer = getOptionRenderer(hideIcons);
  39. return (
  40. <Select
  41. autoFocus={!organization}
  42. className="input-super-large"
  43. clearable={false}
  44. id="select-organization"
  45. labelKey="name"
  46. onChange={onChange}
  47. optionRenderer={optionRenderer}
  48. options={sortBy(organizations, o => o.name.toLowerCase())}
  49. placeholder={translate('onboarding.import_organization.choose_organization')}
  50. required={true}
  51. value={organization}
  52. valueKey="key"
  53. valueRenderer={optionRenderer}
  54. />
  55. );
  56. }
  57. export function getOptionRenderer(hideIcons?: boolean) {
  58. return function optionRenderer(organization: T.Organization) {
  59. const icon = organization.alm
  60. ? `sonarcloud/${sanitizeAlmId(organization.alm.key)}`
  61. : 'sonarcloud-square-logo';
  62. const isPaidOrg = organization.subscription === 'PAID';
  63. return (
  64. <div className="display-flex-space-between">
  65. <span className="text-ellipsis flex-1">
  66. {!hideIcons && (
  67. <img
  68. alt={organization.alm ? organization.alm.key : 'SonarCloud'}
  69. className="little-spacer-right"
  70. height={14}
  71. src={`${getBaseUrl()}/images/${icon}.svg`}
  72. />
  73. )}
  74. {organization.name}
  75. <span className="note little-spacer-left">{organization.key}</span>
  76. </span>
  77. {isPaidOrg && (
  78. <div className="outline-badge">{translate('organization.paid_plan.badge')}</div>
  79. )}
  80. </div>
  81. );
  82. };
  83. }