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

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