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.

GlobalSearchResults.tsx 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 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 { ItemDivider, ItemHeader } from 'design-system';
  21. import * as React from 'react';
  22. import { translate } from '../../../helpers/l10n';
  23. import GlobalSearchShowMore from './GlobalSearchShowMore';
  24. import { ComponentResult, More, Results, sortQualifiers } from './utils';
  25. export interface Props {
  26. query: string;
  27. loadingMore?: string;
  28. more: More;
  29. onMoreClick: (qualifier: string) => void;
  30. onSelect: (componentKey: string) => void;
  31. renderNoResults: () => React.ReactElement<any>;
  32. renderResult: (component: ComponentResult) => React.ReactNode;
  33. results: Results;
  34. selected?: string;
  35. }
  36. export default function GlobalSearchResults(props: Props): React.ReactElement<Props> {
  37. const qualifiers = Object.keys(props.results);
  38. const renderedComponents: React.ReactNode[] = [];
  39. const allowMore = props.query.length !== 1;
  40. sortQualifiers(qualifiers).forEach((qualifier) => {
  41. const components = props.results[qualifier];
  42. if (components.length > 0) {
  43. const more = props.more[qualifier];
  44. renderedComponents.push(
  45. <li key={`group-${qualifier}`}>
  46. <ul key={`header-${qualifier}`} aria-labelledby={translate('qualifiers', qualifier)}>
  47. <ItemHeader>
  48. <p id={translate('qualifiers', qualifier)}>{translate('qualifiers', qualifier)}</p>
  49. </ItemHeader>
  50. {components.map((component) => props.renderResult(component))}
  51. {more !== undefined && more > 0 && (
  52. <GlobalSearchShowMore
  53. allowMore={allowMore}
  54. key={`more-${qualifier}`}
  55. loadingMore={props.loadingMore}
  56. onMoreClick={props.onMoreClick}
  57. onSelect={props.onSelect}
  58. qualifier={qualifier}
  59. selected={props.selected === `qualifier###${qualifier}`}
  60. />
  61. )}
  62. <ItemDivider />
  63. </ul>
  64. </li>
  65. );
  66. }
  67. });
  68. return renderedComponents.length > 0 ? <>{renderedComponents}</> : props.renderNoResults();
  69. }