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.

GitlabProjectCreateRenderer.tsx 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 { Link, Spinner } from '@sonarsource/echoes-react';
  21. import { LightPrimary, Title } from 'design-system';
  22. import React, { useContext, useEffect, useState } from 'react';
  23. import { FormattedMessage } from 'react-intl';
  24. import { AvailableFeaturesContext } from '../../../../app/components/available-features/AvailableFeaturesContext';
  25. import { translate } from '../../../../helpers/l10n';
  26. import { queryToSearch } from '../../../../helpers/urls';
  27. import { GitlabProject } from '../../../../types/alm-integration';
  28. import { AlmInstanceBase, AlmKeys, AlmSettingsInstance } from '../../../../types/alm-settings';
  29. import { Feature } from '../../../../types/features';
  30. import { Paging } from '../../../../types/types';
  31. import AlmSettingsInstanceDropdown from '../components/AlmSettingsInstanceDropdown';
  32. import RepositoryList from '../components/RepositoryList';
  33. import WrongBindingCountAlert from '../components/WrongBindingCountAlert';
  34. import { CreateProjectModes } from '../types';
  35. import GitlabPersonalAccessTokenForm from './GItlabPersonalAccessTokenForm';
  36. export interface GitlabProjectCreateRendererProps {
  37. almInstances?: AlmSettingsInstance[];
  38. loading: boolean;
  39. onImport: (id: string[]) => void;
  40. onLoadMore: () => void;
  41. onPersonalAccessTokenCreated: () => void;
  42. onSearch: (searchQuery: string) => void;
  43. projects?: GitlabProject[];
  44. projectsPaging: Paging;
  45. resetPat: boolean;
  46. searchQuery: string;
  47. selectedAlmInstance?: AlmSettingsInstance;
  48. onSelectedAlmInstanceChange: (instance: AlmInstanceBase) => void;
  49. showPersonalAccessTokenForm?: boolean;
  50. }
  51. export default function GitlabProjectCreateRenderer(
  52. props: Readonly<GitlabProjectCreateRendererProps>,
  53. ) {
  54. const isMonorepoSupported = useContext(AvailableFeaturesContext).includes(
  55. Feature.MonoRepositoryPullRequestDecoration,
  56. );
  57. const {
  58. almInstances,
  59. loading,
  60. onLoadMore,
  61. onSearch,
  62. projects,
  63. projectsPaging,
  64. resetPat,
  65. searchQuery,
  66. selectedAlmInstance,
  67. showPersonalAccessTokenForm,
  68. } = props;
  69. const [selected, setSelected] = useState<Set<string>>(new Set());
  70. const handleCheck = (id: string) => {
  71. setSelected((prev) => new Set(prev.delete(id) ? prev : prev.add(id)));
  72. };
  73. const handleCheckAll = () => {
  74. setSelected(
  75. new Set(projects?.filter((r) => r.sqProjectKey === undefined).map((r) => r.id) ?? []),
  76. );
  77. };
  78. const handleImport = () => {
  79. props.onImport(Array.from(selected));
  80. };
  81. const handleUncheckAll = () => {
  82. setSelected(new Set());
  83. };
  84. useEffect(() => {
  85. const selectedIds = Array.from(selected).filter((id) => projects?.find((r) => r.id === id));
  86. setSelected(new Set(selectedIds));
  87. // We want to update only when `projects` changes.
  88. // If we subscribe to `selected` changes we will enter an infinite loop.
  89. // eslint-disable-next-line react-hooks/exhaustive-deps
  90. }, [projects]);
  91. return (
  92. <>
  93. <header className="sw-mb-10">
  94. <Title className="sw-mb-4">{translate('onboarding.create_project.gitlab.title')}</Title>
  95. <LightPrimary className="sw-body-sm">
  96. {isMonorepoSupported ? (
  97. <FormattedMessage
  98. id="onboarding.create_project.gitlab.subtitle.with_monorepo"
  99. values={{
  100. monorepoSetupLink: (
  101. <Link
  102. to={{
  103. pathname: '/projects/create',
  104. search: queryToSearch({
  105. mode: CreateProjectModes.GitLab,
  106. mono: true,
  107. }),
  108. }}
  109. >
  110. <FormattedMessage id="onboarding.create_project.subtitle_monorepo_setup_link" />
  111. </Link>
  112. ),
  113. }}
  114. />
  115. ) : (
  116. <FormattedMessage id="onboarding.create_project.gitlab.subtitle" />
  117. )}
  118. </LightPrimary>
  119. </header>
  120. <AlmSettingsInstanceDropdown
  121. almKey={AlmKeys.GitLab}
  122. almInstances={almInstances}
  123. selectedAlmInstance={selectedAlmInstance}
  124. onChangeConfig={props.onSelectedAlmInstanceChange}
  125. />
  126. <Spinner isLoading={loading} />
  127. {!loading && almInstances && almInstances.length === 0 && !selectedAlmInstance && (
  128. <WrongBindingCountAlert alm={AlmKeys.GitLab} />
  129. )}
  130. {!loading &&
  131. selectedAlmInstance &&
  132. (showPersonalAccessTokenForm ? (
  133. <GitlabPersonalAccessTokenForm
  134. almSetting={selectedAlmInstance}
  135. resetPat={resetPat}
  136. onPersonalAccessTokenCreated={props.onPersonalAccessTokenCreated}
  137. />
  138. ) : (
  139. <RepositoryList
  140. almKey={AlmKeys.GitLab}
  141. checkAll={handleCheckAll}
  142. loadingRepositories={loading}
  143. onCheck={handleCheck}
  144. onImport={handleImport}
  145. onLoadMore={onLoadMore}
  146. onSearch={onSearch}
  147. repositories={projects}
  148. repositoryPaging={projectsPaging}
  149. searchQuery={searchQuery}
  150. selected={selected}
  151. uncheckAll={handleUncheckAll}
  152. />
  153. ))}
  154. </>
  155. );
  156. }