diff options
Diffstat (limited to 'server/sonar-web/src/main/js/apps/create/project/useProjectRepositorySearch.tsx')
-rw-r--r-- | server/sonar-web/src/main/js/apps/create/project/useProjectRepositorySearch.tsx | 124 |
1 files changed, 66 insertions, 58 deletions
diff --git a/server/sonar-web/src/main/js/apps/create/project/useProjectRepositorySearch.tsx b/server/sonar-web/src/main/js/apps/create/project/useProjectRepositorySearch.tsx index c4b0a5ec7c1..69adbfc788c 100644 --- a/server/sonar-web/src/main/js/apps/create/project/useProjectRepositorySearch.tsx +++ b/server/sonar-web/src/main/js/apps/create/project/useProjectRepositorySearch.tsx @@ -17,86 +17,94 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -import { useCallback, useEffect, useMemo, useRef, useState } from 'react'; -import { AlmKeys } from '../../../types/alm-settings'; +import { isEmpty } from 'lodash'; +import { useCallback, useEffect, useRef, useState } from 'react'; +import { AzureRepository, BitbucketRepository } from '../../../types/alm-integration'; import { DopSetting } from '../../../types/dop-translation'; import { REPOSITORY_SEARCH_DEBOUNCE_TIME } from './constants'; -export function useProjectRepositorySearch( - almKey: AlmKeys, - fetchRepositories: ( - organizationKey?: string, - query?: string, - pageIndex?: number, - more?: boolean, - ) => Promise<void>, - isInitialized: boolean, - selectedDopSetting: DopSetting | undefined, - selectedOrganizationKey: string | undefined, - setSearchQuery: (query: string) => void, - showPersonalAccessTokenForm = false, -) { - const repositorySearchDebounceId = useRef<NodeJS.Timeout | undefined>(); - const [isSearching, setIsSearching] = useState<boolean>(false); - - const orgValid = useMemo( - () => - almKey !== AlmKeys.GitHub || - (almKey === AlmKeys.GitHub && selectedOrganizationKey !== undefined), - [almKey, selectedOrganizationKey], - ); +type RepoTypes = AzureRepository | BitbucketRepository; - useEffect(() => { - if (selectedDopSetting && !showPersonalAccessTokenForm && orgValid) { - if (almKey === AlmKeys.GitHub) { - fetchRepositories(selectedOrganizationKey); - } else if (!isInitialized) { - fetchRepositories(); - } - } - }, [ - almKey, - fetchRepositories, - isInitialized, - orgValid, - selectedDopSetting, - selectedOrganizationKey, - showPersonalAccessTokenForm, - ]); +export function useProjectRepositorySearch<RepoType extends RepoTypes>({ + defaultRepositorySelect, + fetchData, + fetchSearchResults, + getRepositoryKey, + isMonorepoSetup, + selectedDopSetting, + setSearchQuery, + setSelectedRepository, + setShowPersonalAccessTokenForm, +}: { + defaultRepositorySelect: (repositoryKey: string) => void; + fetchData: () => void; + fetchSearchResults: (query: string, dopKey: string) => Promise<{ repositories: RepoType[] }>; + getRepositoryKey: (repo: RepoType) => string; + isMonorepoSetup: boolean; + selectedDopSetting: DopSetting | undefined; + setSearchQuery: (query: string) => void; + setSelectedRepository: (repo: RepoType) => void; + setShowPersonalAccessTokenForm: (show: boolean) => void; +}) { + const repositorySearchDebounceId = useRef<NodeJS.Timeout | undefined>(); + const [isSearching, setIsSearching] = useState(false); + const [searchResults, setSearchResults] = useState<RepoType[] | undefined>(); const onSearch = useCallback( (query: string) => { setSearchQuery(query); - if (!isInitialized || !orgValid) { + if (!selectedDopSetting) { + return; + } + + if (isEmpty(query)) { + setSearchQuery(''); + setSearchResults(undefined); return; } clearTimeout(repositorySearchDebounceId.current); repositorySearchDebounceId.current = setTimeout(() => { setIsSearching(true); - fetchRepositories( - almKey === AlmKeys.GitHub ? selectedOrganizationKey : undefined, - query, - ).then( - () => setIsSearching(false), + fetchSearchResults(query, selectedDopSetting.key).then( + ({ repositories }) => { + setIsSearching(false); + setSearchResults(repositories); + }, () => setIsSearching(false), ); }, REPOSITORY_SEARCH_DEBOUNCE_TIME); }, - [ - almKey, - fetchRepositories, - isInitialized, - orgValid, - repositorySearchDebounceId, - selectedOrganizationKey, - setIsSearching, - setSearchQuery, - ], + [fetchSearchResults, selectedDopSetting, setSearchQuery], + ); + + const onSelectRepository = useCallback( + (repositoryKey: string) => { + const repo = searchResults?.find((o) => getRepositoryKey(o) === repositoryKey); + if (searchResults && repo) { + setSelectedRepository(repo); + } else { + // If we dont have a set of search results we should look for the repository in the base set of repositories + defaultRepositorySelect(repositoryKey); + } + }, + [defaultRepositorySelect, getRepositoryKey, searchResults, setSelectedRepository], ); + useEffect(() => { + setSearchResults(undefined); + setSearchQuery(''); + setShowPersonalAccessTokenForm(true); + }, [isMonorepoSetup, selectedDopSetting, setSearchQuery, setShowPersonalAccessTokenForm]); + + useEffect(() => { + fetchData(); + }, [fetchData]); + return { isSearching, onSearch, + onSelectRepository, + searchResults, }; } |