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.

SettingsApp.tsx 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 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 { find } from 'lodash';
  21. import * as React from 'react';
  22. import { Helmet } from 'react-helmet-async';
  23. import { connect } from 'react-redux';
  24. import { WithRouterProps } from 'react-router';
  25. import Suggestions from '../../../app/components/embed-docs-modal/Suggestions';
  26. import ScreenPositionHelper from '../../../components/common/ScreenPositionHelper';
  27. import { translate } from '../../../helpers/l10n';
  28. import {
  29. addSideBarClass,
  30. addWhitePageClass,
  31. removeSideBarClass,
  32. removeWhitePageClass
  33. } from '../../../helpers/pages';
  34. import { getSettingsAppDefaultCategory, Store } from '../../../store/rootReducer';
  35. import { fetchSettings } from '../store/actions';
  36. import '../styles.css';
  37. import { ADDITIONAL_CATEGORIES } from './AdditionalCategories';
  38. import AllCategoriesList from './AllCategoriesList';
  39. import CategoryDefinitionsList from './CategoryDefinitionsList';
  40. import CATEGORY_OVERRIDES from './CategoryOverrides';
  41. import PageHeader from './PageHeader';
  42. interface Props {
  43. component?: T.Component;
  44. defaultCategory: string;
  45. fetchSettings(component?: string): Promise<void>;
  46. }
  47. interface State {
  48. loading: boolean;
  49. }
  50. export class SettingsApp extends React.PureComponent<Props & WithRouterProps, State> {
  51. mounted = false;
  52. state: State = { loading: true };
  53. componentDidMount() {
  54. this.mounted = true;
  55. addSideBarClass();
  56. addWhitePageClass();
  57. this.fetchSettings();
  58. }
  59. componentDidUpdate(prevProps: Props) {
  60. if (prevProps.component !== this.props.component) {
  61. this.fetchSettings();
  62. }
  63. }
  64. componentWillUnmount() {
  65. this.mounted = false;
  66. removeSideBarClass();
  67. removeWhitePageClass();
  68. }
  69. fetchSettings = () => {
  70. const { component } = this.props;
  71. this.props.fetchSettings(component && component.key).then(this.stopLoading, this.stopLoading);
  72. };
  73. stopLoading = () => {
  74. if (this.mounted) {
  75. this.setState({ loading: false });
  76. }
  77. };
  78. render() {
  79. if (this.state.loading) {
  80. return null;
  81. }
  82. const { query } = this.props.location;
  83. const originalCategory = (query.category as string) || this.props.defaultCategory;
  84. const overriddenCategory = CATEGORY_OVERRIDES[originalCategory.toLowerCase()];
  85. const selectedCategory = overriddenCategory || originalCategory;
  86. const foundAdditionalCategory = find(ADDITIONAL_CATEGORIES, c => c.key === selectedCategory);
  87. const isProjectSettings = this.props.component;
  88. const shouldRenderAdditionalCategory =
  89. foundAdditionalCategory &&
  90. ((isProjectSettings && foundAdditionalCategory.availableForProject) ||
  91. (!isProjectSettings && foundAdditionalCategory.availableGlobally));
  92. return (
  93. <div id="settings-page">
  94. <Suggestions suggestions="settings" />
  95. <Helmet defer={false} title={translate('settings.page')} />
  96. <PageHeader component={this.props.component} />
  97. <div className="layout-page">
  98. <ScreenPositionHelper className="layout-page-side-outer">
  99. {({ top }) => (
  100. <div className="layout-page-side" style={{ top }}>
  101. <div className="layout-page-side-inner">
  102. <AllCategoriesList
  103. component={this.props.component}
  104. defaultCategory={this.props.defaultCategory}
  105. selectedCategory={selectedCategory}
  106. />
  107. </div>
  108. </div>
  109. )}
  110. </ScreenPositionHelper>
  111. <div className="layout-page-main">
  112. <div className="layout-page-main-inner">
  113. <div className="big-padded">
  114. {foundAdditionalCategory && shouldRenderAdditionalCategory ? (
  115. foundAdditionalCategory.renderComponent({
  116. component: this.props.component,
  117. selectedCategory: originalCategory
  118. })
  119. ) : (
  120. <CategoryDefinitionsList
  121. category={selectedCategory}
  122. component={this.props.component}
  123. />
  124. )}
  125. </div>
  126. </div>
  127. </div>
  128. </div>
  129. </div>
  130. );
  131. }
  132. }
  133. const mapStateToProps = (state: Store) => ({
  134. defaultCategory: getSettingsAppDefaultCategory(state)
  135. });
  136. const mapDispatchToProps = { fetchSettings: fetchSettings as any };
  137. export default connect(mapStateToProps, mapDispatchToProps)(SettingsApp);