Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

TutorialSelection.tsx 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 * as React from 'react';
  21. import { getAlmSettingsNoCatch } from '../../api/alm-settings';
  22. import { getScannableProjects } from '../../api/components';
  23. import { getValue } from '../../api/settings';
  24. import { getHostUrl } from '../../helpers/urls';
  25. import { hasGlobalPermission } from '../../helpers/users';
  26. import { useProjectBindingQuery } from '../../queries/devops-integration';
  27. import { AlmSettingsInstance } from '../../types/alm-settings';
  28. import { Permissions } from '../../types/permissions';
  29. import { SettingsKey } from '../../types/settings';
  30. import { Component } from '../../types/types';
  31. import { LoggedInUser } from '../../types/users';
  32. import { Location, withRouter } from '../hoc/withRouter';
  33. import TutorialSelectionRenderer from './TutorialSelectionRenderer';
  34. import { TutorialModes } from './types';
  35. export interface TutorialSelectionProps {
  36. component: Component;
  37. currentUser: LoggedInUser;
  38. willRefreshAutomatically?: boolean;
  39. location: Location;
  40. }
  41. export function TutorialSelection(props: Readonly<TutorialSelectionProps>) {
  42. const { component, currentUser, location, willRefreshAutomatically } = props;
  43. const [currentUserCanScanProject, setCurrentUserCanScanProject] = React.useState(false);
  44. const [baseUrl, setBaseUrl] = React.useState(getHostUrl());
  45. const [loading, setLoading] = React.useState(true);
  46. const [loadingAlm, setLoadingAlm] = React.useState(false);
  47. const [almBinding, setAlmBinding] = React.useState<AlmSettingsInstance | undefined>(undefined);
  48. const { data: projectBinding } = useProjectBindingQuery(component.key);
  49. React.useEffect(() => {
  50. const checkUserPermissions = async () => {
  51. if (hasGlobalPermission(currentUser, Permissions.Scan)) {
  52. setCurrentUserCanScanProject(true);
  53. return Promise.resolve();
  54. }
  55. const { projects } = await getScannableProjects();
  56. setCurrentUserCanScanProject(projects.find((p) => p.key === component.key) !== undefined);
  57. return Promise.resolve();
  58. };
  59. const fetchBaseUrl = async () => {
  60. const setting = await getValue({ key: SettingsKey.ServerBaseUrl }).catch(() => undefined);
  61. const baseUrl = setting?.value;
  62. if (baseUrl && baseUrl.length > 0) {
  63. setBaseUrl(baseUrl);
  64. }
  65. };
  66. Promise.all([fetchBaseUrl(), checkUserPermissions()])
  67. .then(() => {
  68. setLoading(false);
  69. })
  70. .catch(() => {});
  71. }, [component.key, currentUser]);
  72. React.useEffect(() => {
  73. const fetchAlmBindings = async () => {
  74. if (projectBinding != null) {
  75. setLoadingAlm(true);
  76. const almSettings = await getAlmSettingsNoCatch(component.key).catch(() => undefined);
  77. let almBinding;
  78. if (almSettings !== undefined) {
  79. almBinding = almSettings.find((d) => d.key === projectBinding.key);
  80. }
  81. setAlmBinding(almBinding);
  82. setLoadingAlm(false);
  83. }
  84. };
  85. fetchAlmBindings().catch(() => {});
  86. }, [component.key, projectBinding]);
  87. const selectedTutorial: TutorialModes | undefined = location.query?.selectedTutorial;
  88. return (
  89. <TutorialSelectionRenderer
  90. almBinding={almBinding}
  91. baseUrl={baseUrl}
  92. component={component}
  93. currentUser={currentUser}
  94. currentUserCanScanProject={currentUserCanScanProject}
  95. loading={loading || loadingAlm}
  96. projectBinding={projectBinding}
  97. selectedTutorial={selectedTutorial}
  98. willRefreshAutomatically={willRefreshAutomatically}
  99. />
  100. );
  101. }
  102. export default withRouter(TutorialSelection);