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.

RepositoryFacet.tsx 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 { Note } from 'design-system';
  21. import * as React from 'react';
  22. import { getRuleRepositories } from '../../../api/rules';
  23. import withLanguagesContext from '../../../app/components/languages/withLanguagesContext';
  24. import { translate } from '../../../helpers/l10n';
  25. import { highlightTerm } from '../../../helpers/search';
  26. import { Languages } from '../../../types/languages';
  27. import { Dict } from '../../../types/types';
  28. import { ListStyleFacet } from '../../issues/sidebar/ListStyleFacet';
  29. import { BasicProps } from './Facet';
  30. interface StateProps {
  31. languages: Languages;
  32. }
  33. interface Props extends BasicProps, StateProps {
  34. referencedRepositories: Dict<{ key: string; language: string; name: string }>;
  35. }
  36. class RepositoryFacet extends React.PureComponent<Props> {
  37. getLanguageName = (languageKey: string) => {
  38. const { languages } = this.props;
  39. const language = languages[languageKey];
  40. return (language && language.name) || languageKey;
  41. };
  42. handleSearch(query: string) {
  43. return getRuleRepositories({ q: query }).then((repos) => {
  44. return {
  45. paging: { pageIndex: 1, pageSize: repos.length, total: repos.length },
  46. results: repos.map((r) => r.key),
  47. };
  48. });
  49. }
  50. renderName = (repositoryKey: string) => {
  51. const { referencedRepositories } = this.props;
  52. const repository = referencedRepositories[repositoryKey];
  53. return repository ? (
  54. <>
  55. {repository.name}
  56. <Note className="sw-ml-1">{this.getLanguageName(repository.language)}</Note>
  57. </>
  58. ) : (
  59. repositoryKey
  60. );
  61. };
  62. renderTextName = (repositoryKey: string) => {
  63. const { referencedRepositories } = this.props;
  64. const repository = referencedRepositories[repositoryKey];
  65. return (repository && repository.name) || repositoryKey;
  66. };
  67. renderSearchTextName = (repositoryKey: string, query: string) => {
  68. const { referencedRepositories } = this.props;
  69. const repository = referencedRepositories[repositoryKey];
  70. return repository ? (
  71. <>
  72. {highlightTerm(repository.name, query)}
  73. <Note className="sw-ml-1">{this.getLanguageName(repository.language)}</Note>
  74. </>
  75. ) : (
  76. repositoryKey
  77. );
  78. };
  79. render() {
  80. return (
  81. <ListStyleFacet<string>
  82. facetHeader={translate('coding_rules.facet.repositories')}
  83. showMoreAriaLabel={translate('coding_rules.facet.repository.show_more')}
  84. showLessAriaLabel={translate('coding_rules.facet.repository.show_less')}
  85. fetching={false}
  86. getFacetItemText={this.renderTextName}
  87. getSearchResultKey={(rep) => rep}
  88. getSearchResultText={this.renderTextName}
  89. onChange={this.props.onChange}
  90. onSearch={this.handleSearch}
  91. onToggle={this.props.onToggle}
  92. open={this.props.open}
  93. property="repositories"
  94. renderFacetItem={this.renderName}
  95. renderSearchResult={this.renderSearchTextName}
  96. searchPlaceholder={translate('search.search_for_repositories')}
  97. searchInputAriaLabel={translate('search.search_for_repositories')}
  98. stats={this.props.stats}
  99. values={this.props.values}
  100. />
  101. );
  102. }
  103. }
  104. export default withLanguagesContext(RepositoryFacet);