Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

useProjectCreate.tsx 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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, useState } from 'react';
  21. import { useLocation, useRouter } from '../../../components/hoc/withRouter';
  22. import { isDefined } from '../../../helpers/types';
  23. import { AlmInstanceBase, AlmKeys } from '../../../types/alm-settings';
  24. import { DopSetting } from '../../../types/dop-translation';
  25. import { Paging } from '../../../types/types';
  26. export function useProjectCreate<RepoType, GroupType>(
  27. almKey: AlmKeys,
  28. dopSettings: DopSetting[],
  29. getKey: (repo: RepoType) => string,
  30. pageSize: number,
  31. ) {
  32. const [isInitialized, setIsInitialized] = useState(false);
  33. const [selectedDopSetting, setSelectedDopSetting] = useState<DopSetting>();
  34. const [isLoadingOrganizations, setIsLoadingOrganizations] = useState(true);
  35. const [organizations, setOrganizations] = useState<GroupType[]>([]);
  36. const [selectedOrganization, setSelectedOrganization] = useState<GroupType>();
  37. const [isLoadingRepositories, setIsLoadingRepositories] = useState<boolean>(false);
  38. const [isLoadingMoreRepositories, setIsLoadingMoreRepositories] = useState<boolean>(false);
  39. const [repositories, setRepositories] = useState<RepoType[]>([]);
  40. const [selectedRepository, setSelectedRepository] = useState<RepoType>();
  41. const [showPersonalAccessTokenForm, setShowPersonalAccessTokenForm] = useState<boolean>(true);
  42. const [resetPersonalAccessToken, setResetPersonalAccessToken] = useState<boolean>(false);
  43. const [searchQuery, setSearchQuery] = useState<string>('');
  44. const [projectsPaging, setProjectsPaging] = useState<Paging>({
  45. pageIndex: 1,
  46. pageSize,
  47. total: 0,
  48. });
  49. const router = useRouter();
  50. const location = useLocation();
  51. const isMonorepoSetup = location.query?.mono === 'true';
  52. const hasDopSettings = useMemo(() => Boolean(dopSettings?.length), [dopSettings]);
  53. const cleanUrl = useCallback(() => {
  54. delete location.query.resetPat;
  55. router.replace(location);
  56. }, [location, router]);
  57. const handlePersonalAccessTokenCreated = useCallback(() => {
  58. cleanUrl();
  59. setShowPersonalAccessTokenForm(false);
  60. setResetPersonalAccessToken(false);
  61. }, [cleanUrl]);
  62. const onSelectDopSetting = useCallback((setting: DopSetting | undefined) => {
  63. setIsInitialized(false);
  64. setSelectedDopSetting(setting);
  65. setShowPersonalAccessTokenForm(true);
  66. setOrganizations([]);
  67. setRepositories([]);
  68. setSearchQuery('');
  69. }, []);
  70. const resetLoading = useCallback((value: boolean, more = false) => {
  71. if (more) {
  72. setIsLoadingMoreRepositories(value);
  73. } else {
  74. setIsLoadingRepositories(value);
  75. }
  76. }, []);
  77. const onSelectedAlmInstanceChange = useCallback(
  78. (instance?: AlmInstanceBase) => {
  79. onSelectDopSetting(
  80. instance ? dopSettings.find((dopSetting) => dopSetting.key === instance.key) : undefined,
  81. );
  82. },
  83. [dopSettings, onSelectDopSetting],
  84. );
  85. const handleSelectRepository = useCallback(
  86. (repositoryKey: string) => {
  87. setSelectedRepository(repositories.find((repo) => getKey(repo) === repositoryKey));
  88. },
  89. [getKey, repositories, setSelectedRepository],
  90. );
  91. useEffect(() => {
  92. if (!hasDopSettings || (hasDopSettings && isDefined(selectedDopSetting))) {
  93. return;
  94. }
  95. if (almKey === AlmKeys.GitHub) {
  96. const selectedDopSettingId = location.query?.dopSetting;
  97. if (selectedDopSettingId !== undefined) {
  98. const selectedDopSetting = dopSettings.find(({ id }) => id === selectedDopSettingId);
  99. if (selectedDopSetting) {
  100. setSelectedDopSetting(selectedDopSetting);
  101. }
  102. return;
  103. }
  104. }
  105. if (dopSettings.length > 1) {
  106. setSelectedDopSetting(undefined);
  107. } else {
  108. setSelectedDopSetting(dopSettings[0]);
  109. }
  110. }, [almKey, dopSettings, hasDopSettings, location, selectedDopSetting, setSelectedDopSetting]);
  111. return {
  112. handlePersonalAccessTokenCreated,
  113. handleSelectRepository,
  114. hasDopSettings,
  115. isInitialized,
  116. isLoadingOrganizations,
  117. isLoadingRepositories,
  118. isLoadingMoreRepositories,
  119. isMonorepoSetup,
  120. onSelectedAlmInstanceChange,
  121. onSelectDopSetting,
  122. projectsPaging,
  123. organizations,
  124. repositories,
  125. resetLoading,
  126. resetPersonalAccessToken,
  127. searchQuery,
  128. selectedDopSetting,
  129. selectedRepository,
  130. setIsInitialized,
  131. setIsLoadingRepositories,
  132. setIsLoadingMoreRepositories,
  133. setIsLoadingOrganizations,
  134. setProjectsPaging,
  135. setOrganizations,
  136. selectedOrganization,
  137. setRepositories,
  138. setResetPersonalAccessToken,
  139. setSearchQuery,
  140. setSelectedDopSetting,
  141. setSelectedOrganization,
  142. setSelectedRepository,
  143. setShowPersonalAccessTokenForm,
  144. showPersonalAccessTokenForm,
  145. };
  146. }