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.

ProjectsSortingSelect.tsx 3.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 SortDescIcon from 'sonar-ui-common/components/icons/SortDescIcon';
  22. import { sortBy } from 'lodash';
  23. import SortAscIcon from 'sonar-ui-common/components/icons/SortAscIcon';
  24. import { translate } from 'sonar-ui-common/helpers/l10n';
  25. import Tooltip from 'sonar-ui-common/components/controls/Tooltip';
  26. import { ButtonIcon } from 'sonar-ui-common/components/controls/buttons';
  27. import Select from 'sonar-ui-common/components/controls/Select';
  28. import ProjectsSortingSelectOption, { Option } from './ProjectsSortingSelectOption';
  29. import { colors } from '../../../app/theme';
  30. import { SORTING_METRICS, SORTING_LEAK_METRICS, parseSorting } from '../utils';
  31. interface Props {
  32. className?: string;
  33. defaultOption: string;
  34. onChange: (sort: string, desc: boolean) => void;
  35. selectedSort: string;
  36. view: string;
  37. }
  38. export default class ProjectsSortingSelect extends React.PureComponent<Props> {
  39. getSorting = () => parseSorting(this.props.selectedSort);
  40. getOptions = () => {
  41. const sortMetrics = this.props.view === 'leak' ? SORTING_LEAK_METRICS : SORTING_METRICS;
  42. return sortBy(sortMetrics, option => (option.value === this.props.defaultOption ? 0 : 1)).map(
  43. option => ({
  44. value: option.value,
  45. label: translate('projects.sorting', option.value),
  46. class: option.class
  47. })
  48. );
  49. };
  50. handleDescToggle = () => {
  51. const sorting = this.getSorting();
  52. this.props.onChange(sorting.sortValue, !sorting.sortDesc);
  53. };
  54. handleSortChange = (option: Option) =>
  55. this.props.onChange(option.value, this.getSorting().sortDesc);
  56. render() {
  57. const { sortDesc, sortValue } = this.getSorting();
  58. return (
  59. <div className={this.props.className}>
  60. <label>{translate('projects.sort_by')}:</label>
  61. <Select
  62. className="little-spacer-left input-medium"
  63. clearable={false}
  64. onChange={this.handleSortChange}
  65. optionComponent={ProjectsSortingSelectOption}
  66. options={this.getOptions()}
  67. searchable={false}
  68. value={sortValue}
  69. />
  70. <Tooltip
  71. overlay={
  72. sortDesc ? translate('projects.sort_descending') : translate('projects.sort_ascending')
  73. }>
  74. <ButtonIcon
  75. className="js-projects-sorting-invert spacer-left"
  76. color={colors.gray60}
  77. onClick={this.handleDescToggle}>
  78. {sortDesc ? <SortDescIcon className="" /> : <SortAscIcon className="" />}
  79. </ButtonIcon>
  80. </Tooltip>
  81. </div>
  82. );
  83. }
  84. }