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.

GlobalNavPlus.tsx 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2018 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 { Link, withRouter, WithRouterProps } from 'react-router';
  22. import CreateFormShim from '../../../../apps/portfolio/components/CreateFormShim';
  23. import Dropdown from '../../../../components/controls/Dropdown';
  24. import PlusIcon from '../../../../components/icons-components/PlusIcon';
  25. import { getExtensionStart } from '../../extensions/utils';
  26. import { getComponentNavigation } from '../../../../api/nav';
  27. import { translate } from '../../../../helpers/l10n';
  28. import { isSonarCloud } from '../../../../helpers/system';
  29. import { getPortfolioAdminUrl, getPortfolioUrl } from '../../../../helpers/urls';
  30. import { hasGlobalPermission } from '../../../../helpers/users';
  31. import { OnboardingContextShape } from '../../OnboardingContext';
  32. interface Props {
  33. appState: Pick<T.AppState, 'qualifiers'>;
  34. currentUser: T.LoggedInUser;
  35. openProjectOnboarding: OnboardingContextShape;
  36. }
  37. interface State {
  38. createPortfolio: boolean;
  39. governanceReady: boolean;
  40. }
  41. export class GlobalNavPlus extends React.PureComponent<Props & WithRouterProps, State> {
  42. mounted = false;
  43. state: State = { createPortfolio: false, governanceReady: false };
  44. componentDidMount() {
  45. this.mounted = true;
  46. if (this.props.appState.qualifiers.includes('VW')) {
  47. getExtensionStart('governance/console').then(
  48. () => {
  49. if (this.mounted) {
  50. this.setState({ governanceReady: true });
  51. }
  52. },
  53. () => {}
  54. );
  55. }
  56. }
  57. componentWillUnmount() {
  58. this.mounted = false;
  59. }
  60. handleNewProjectClick = (event: React.MouseEvent<HTMLAnchorElement>) => {
  61. event.preventDefault();
  62. this.props.openProjectOnboarding();
  63. };
  64. openCreatePortfolioForm = () => {
  65. this.setState({ createPortfolio: true });
  66. };
  67. closeCreatePortfolioForm = () => {
  68. this.setState({ createPortfolio: false });
  69. };
  70. handleNewPortfolioClick = (event: React.MouseEvent<HTMLAnchorElement>) => {
  71. event.preventDefault();
  72. event.currentTarget.blur();
  73. this.openCreatePortfolioForm();
  74. };
  75. handleCreatePortfolio = ({ key, qualifier }: { key: string; qualifier: string }) => {
  76. return getComponentNavigation({ component: key }).then(data => {
  77. if (
  78. data.configuration &&
  79. data.configuration.extensions &&
  80. data.configuration.extensions.find(
  81. (item: { key: string; name: string }) => item.key === 'governance/console'
  82. )
  83. ) {
  84. this.props.router.push(getPortfolioAdminUrl(key, qualifier));
  85. } else {
  86. this.props.router.push(getPortfolioUrl(key));
  87. }
  88. this.closeCreatePortfolioForm();
  89. });
  90. };
  91. renderCreateProject(canCreateProject: boolean) {
  92. if (!canCreateProject) {
  93. return null;
  94. }
  95. return (
  96. <li>
  97. <a className="js-new-project" href="#" onClick={this.handleNewProjectClick}>
  98. {translate('provisioning.analyze_new_project')}
  99. </a>
  100. </li>
  101. );
  102. }
  103. renderCreateOrganization(canCreateOrg: boolean) {
  104. if (!canCreateOrg) {
  105. return null;
  106. }
  107. return (
  108. <li>
  109. <Link className="js-new-organization" to="/create-organization">
  110. {translate('my_account.create_new_organization')}
  111. </Link>
  112. </li>
  113. );
  114. }
  115. renderCreatePortfolio(showGovernanceEntry: boolean, defaultQualifier?: string) {
  116. const governanceInstalled = this.props.appState.qualifiers.includes('VW');
  117. if (!governanceInstalled || !showGovernanceEntry) {
  118. return null;
  119. }
  120. return (
  121. <li>
  122. <a className="js-new-portfolio" href="#" onClick={this.handleNewPortfolioClick}>
  123. {defaultQualifier
  124. ? translate('my_account.create_new', defaultQualifier)
  125. : translate('my_account.create_new_portfolio_application')}
  126. </a>
  127. </li>
  128. );
  129. }
  130. render() {
  131. const { currentUser } = this.props;
  132. const canCreateApplication = hasGlobalPermission(currentUser, 'applicationcreator');
  133. const canCreateOrg = isSonarCloud();
  134. const canCreatePortfolio = hasGlobalPermission(currentUser, 'portfoliocreator');
  135. const canCreateProject = isSonarCloud() || hasGlobalPermission(currentUser, 'provisioning');
  136. if (!canCreateProject && !canCreateApplication && !canCreatePortfolio && !canCreateOrg) {
  137. return null;
  138. }
  139. let defaultQualifier: string | undefined;
  140. if (!canCreateApplication) {
  141. defaultQualifier = 'VW';
  142. } else if (!canCreatePortfolio) {
  143. defaultQualifier = 'APP';
  144. }
  145. return (
  146. <>
  147. <Dropdown
  148. overlay={
  149. <ul className="menu">
  150. {this.renderCreateProject(canCreateProject)}
  151. {this.renderCreateOrganization(canCreateOrg)}
  152. {this.renderCreatePortfolio(
  153. canCreateApplication || canCreatePortfolio,
  154. defaultQualifier
  155. )}
  156. </ul>
  157. }
  158. tagName="li">
  159. <a
  160. className="navbar-plus"
  161. href="#"
  162. title={
  163. isSonarCloud()
  164. ? translate('my_account.create_new_project_or_organization')
  165. : translate('my_account.create_new_project_portfolio_or_application')
  166. }>
  167. <PlusIcon />
  168. </a>
  169. </Dropdown>
  170. {this.state.governanceReady &&
  171. this.state.createPortfolio && (
  172. <CreateFormShim
  173. defaultQualifier={defaultQualifier}
  174. onClose={this.closeCreatePortfolioForm}
  175. onCreate={this.handleCreatePortfolio}
  176. />
  177. )}
  178. </>
  179. );
  180. }
  181. }
  182. export default withRouter(GlobalNavPlus);