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.

AuthorFacet.tsx 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 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 { omit } from 'lodash';
  21. import * as React from 'react';
  22. import { searchIssueAuthors } from '../../../api/issues';
  23. import ListStyleFacet from '../../../components/facet/ListStyleFacet';
  24. import { translate } from '../../../helpers/l10n';
  25. import { highlightTerm } from '../../../helpers/search';
  26. import { Facet } from '../../../types/issues';
  27. import { Query } from '../utils';
  28. interface Props {
  29. component: T.Component | undefined;
  30. fetching: boolean;
  31. loadSearchResultCount: (property: string, changes: Partial<Query>) => Promise<Facet>;
  32. onChange: (changes: Partial<Query>) => void;
  33. onToggle: (property: string) => void;
  34. open: boolean;
  35. query: Query;
  36. stats: T.Dict<number> | undefined;
  37. author: string[];
  38. }
  39. const SEARCH_SIZE = 100;
  40. export default class AuthorFacet extends React.PureComponent<Props> {
  41. identity = (author: string) => {
  42. return author;
  43. };
  44. handleSearch = (query: string, _page: number) => {
  45. const { component } = this.props;
  46. const project =
  47. component && ['TRK', 'VW', 'APP'].includes(component.qualifier) ? component.key : undefined;
  48. return searchIssueAuthors({
  49. project,
  50. ps: SEARCH_SIZE, // maximum
  51. q: query
  52. }).then(authors => ({ maxResults: authors.length === SEARCH_SIZE, results: authors }));
  53. };
  54. loadSearchResultCount = (author: string[]) => {
  55. return this.props.loadSearchResultCount('author', { author });
  56. };
  57. renderSearchResult = (author: string, term: string) => {
  58. return highlightTerm(author, term);
  59. };
  60. render() {
  61. return (
  62. <ListStyleFacet<string>
  63. facetHeader={translate('issues.facet.authors')}
  64. fetching={this.props.fetching}
  65. getFacetItemText={this.identity}
  66. getSearchResultKey={this.identity}
  67. getSearchResultText={this.identity}
  68. loadSearchResultCount={this.loadSearchResultCount}
  69. onChange={this.props.onChange}
  70. onSearch={this.handleSearch}
  71. onToggle={this.props.onToggle}
  72. open={this.props.open}
  73. property="author"
  74. query={omit(this.props.query, 'author')}
  75. renderFacetItem={this.identity}
  76. renderSearchResult={this.renderSearchResult}
  77. searchPlaceholder={translate('search.search_for_authors')}
  78. stats={this.props.stats}
  79. values={this.props.author}
  80. />
  81. );
  82. }
  83. }