您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

TutorialSelection.tsx 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2022 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 { AlmSettingsInstance, ProjectAlmBindingResponse } from '../../types/alm-settings';
  27. import { Permissions } from '../../types/permissions';
  28. import { SettingsKey } from '../../types/settings';
  29. import { Component } from '../../types/types';
  30. import { LoggedInUser } from '../../types/users';
  31. import { Location, Router, withRouter } from '../hoc/withRouter';
  32. import TutorialSelectionRenderer from './TutorialSelectionRenderer';
  33. import { TutorialModes } from './types';
  34. interface Props {
  35. component: Component;
  36. currentUser: LoggedInUser;
  37. projectBinding?: ProjectAlmBindingResponse;
  38. willRefreshAutomatically?: boolean;
  39. location: Location;
  40. router: Router;
  41. }
  42. interface State {
  43. almBinding?: AlmSettingsInstance;
  44. currentUserCanScanProject: boolean;
  45. baseUrl: string;
  46. loading: boolean;
  47. }
  48. export class TutorialSelection extends React.PureComponent<Props, State> {
  49. mounted = false;
  50. state: State = {
  51. currentUserCanScanProject: false,
  52. baseUrl: getHostUrl(),
  53. loading: true
  54. };
  55. async componentDidMount() {
  56. this.mounted = true;
  57. await Promise.all([this.fetchAlmBindings(), this.fetchBaseUrl(), this.checkUserPermissions()]);
  58. if (this.mounted) {
  59. this.setState({ loading: false });
  60. }
  61. }
  62. componentWillUnmount() {
  63. this.mounted = false;
  64. }
  65. checkUserPermissions = async () => {
  66. const { component, currentUser } = this.props;
  67. if (hasGlobalPermission(currentUser, Permissions.Scan)) {
  68. this.setState({ currentUserCanScanProject: true });
  69. return Promise.resolve();
  70. }
  71. const { projects } = await getScannableProjects();
  72. this.setState({
  73. currentUserCanScanProject: projects.find(p => p.key === component.key) !== undefined
  74. });
  75. return Promise.resolve();
  76. };
  77. fetchAlmBindings = async () => {
  78. const { component, projectBinding } = this.props;
  79. if (projectBinding !== undefined) {
  80. const almSettings = await getAlmSettingsNoCatch(component.key).catch(() => undefined);
  81. if (this.mounted) {
  82. let almBinding;
  83. if (almSettings !== undefined) {
  84. almBinding = almSettings.find(d => d.key === projectBinding.key);
  85. }
  86. this.setState({ almBinding });
  87. }
  88. }
  89. };
  90. fetchBaseUrl = async () => {
  91. const setting = await getValue({ key: SettingsKey.ServerBaseUrl }).catch(() => undefined);
  92. const baseUrl = setting?.value;
  93. if (baseUrl && baseUrl.length > 0 && this.mounted) {
  94. this.setState({ baseUrl });
  95. }
  96. };
  97. handleSelectTutorial = (selectedTutorial: TutorialModes) => {
  98. const {
  99. router,
  100. location: { pathname, query }
  101. } = this.props;
  102. router.push({
  103. pathname,
  104. query: { ...query, selectedTutorial }
  105. });
  106. };
  107. render() {
  108. const {
  109. component,
  110. currentUser,
  111. location,
  112. projectBinding,
  113. willRefreshAutomatically
  114. } = this.props;
  115. const { almBinding, baseUrl, currentUserCanScanProject, loading } = this.state;
  116. const selectedTutorial: TutorialModes | undefined = location.query?.selectedTutorial;
  117. return (
  118. <TutorialSelectionRenderer
  119. almBinding={almBinding}
  120. baseUrl={baseUrl}
  121. component={component}
  122. currentUser={currentUser}
  123. currentUserCanScanProject={currentUserCanScanProject}
  124. loading={loading}
  125. onSelectTutorial={this.handleSelectTutorial}
  126. projectBinding={projectBinding}
  127. selectedTutorial={selectedTutorial}
  128. willRefreshAutomatically={willRefreshAutomatically}
  129. />
  130. );
  131. }
  132. }
  133. export default withRouter(TutorialSelection);