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.

useProjectRepositorySearch.tsx 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 { useCallback, useEffect, useMemo, useRef, useState } from 'react';
  21. import { AlmKeys } from '../../../types/alm-settings';
  22. import { DopSetting } from '../../../types/dop-translation';
  23. import { REPOSITORY_SEARCH_DEBOUNCE_TIME } from './constants';
  24. export function useProjectRepositorySearch(
  25. almKey: AlmKeys,
  26. fetchRepositories: (
  27. organizationKey?: string,
  28. query?: string,
  29. pageIndex?: number,
  30. more?: boolean,
  31. ) => Promise<void>,
  32. isInitialized: boolean,
  33. selectedDopSetting: DopSetting | undefined,
  34. selectedOrganizationKey: string | undefined,
  35. setSearchQuery: (query: string) => void,
  36. showPersonalAccessTokenForm = false,
  37. ) {
  38. const repositorySearchDebounceId = useRef<NodeJS.Timeout | undefined>();
  39. const [isSearching, setIsSearching] = useState<boolean>(false);
  40. const orgValid = useMemo(
  41. () =>
  42. almKey !== AlmKeys.GitHub ||
  43. (almKey === AlmKeys.GitHub && selectedOrganizationKey !== undefined),
  44. [almKey, selectedOrganizationKey],
  45. );
  46. useEffect(() => {
  47. if (selectedDopSetting && !showPersonalAccessTokenForm && orgValid) {
  48. if (almKey === AlmKeys.GitHub) {
  49. fetchRepositories(selectedOrganizationKey);
  50. } else if (!isInitialized) {
  51. fetchRepositories();
  52. }
  53. }
  54. }, [
  55. almKey,
  56. fetchRepositories,
  57. isInitialized,
  58. orgValid,
  59. selectedDopSetting,
  60. selectedOrganizationKey,
  61. showPersonalAccessTokenForm,
  62. ]);
  63. const onSearch = useCallback(
  64. (query: string) => {
  65. setSearchQuery(query);
  66. if (!isInitialized || !orgValid) {
  67. return;
  68. }
  69. clearTimeout(repositorySearchDebounceId.current);
  70. repositorySearchDebounceId.current = setTimeout(() => {
  71. setIsSearching(true);
  72. fetchRepositories(
  73. almKey === AlmKeys.GitHub ? selectedOrganizationKey : undefined,
  74. query,
  75. ).then(
  76. () => setIsSearching(false),
  77. () => setIsSearching(false),
  78. );
  79. }, REPOSITORY_SEARCH_DEBOUNCE_TIME);
  80. },
  81. [
  82. almKey,
  83. fetchRepositories,
  84. isInitialized,
  85. orgValid,
  86. repositorySearchDebounceId,
  87. selectedOrganizationKey,
  88. setIsSearching,
  89. setSearchQuery,
  90. ],
  91. );
  92. return {
  93. isSearching,
  94. onSearch,
  95. };
  96. }