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.

App.tsx 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 { Location } from 'history';
  21. import * as React from 'react';
  22. import { Helmet } from 'react-helmet-async';
  23. import { getPermissionTemplates } from '../../../api/permissions';
  24. import withAppStateContext from '../../../app/components/app-state/withAppStateContext';
  25. import Suggestions from '../../../app/components/embed-docs-modal/Suggestions';
  26. import { translate } from '../../../helpers/l10n';
  27. import { AppState } from '../../../types/appstate';
  28. import { Permission, PermissionTemplate } from '../../../types/types';
  29. import '../../permissions/styles.css';
  30. import { mergeDefaultsToTemplates, mergePermissionsToTemplates, sortPermissions } from '../utils';
  31. import Home from './Home';
  32. import Template from './Template';
  33. interface Props {
  34. location: Location;
  35. appState: AppState;
  36. }
  37. interface State {
  38. ready: boolean;
  39. permissions: Permission[];
  40. permissionTemplates: PermissionTemplate[];
  41. }
  42. export class App extends React.PureComponent<Props, State> {
  43. mounted = false;
  44. state: State = {
  45. ready: false,
  46. permissions: [],
  47. permissionTemplates: []
  48. };
  49. componentDidMount() {
  50. this.mounted = true;
  51. this.requestPermissions();
  52. }
  53. componentWillUnmount() {
  54. this.mounted = false;
  55. }
  56. requestPermissions = async () => {
  57. const { permissions, defaultTemplates, permissionTemplates } = await getPermissionTemplates();
  58. if (this.mounted) {
  59. const sortedPerm = sortPermissions(permissions);
  60. const permissionTemplatesMerged = mergeDefaultsToTemplates(
  61. mergePermissionsToTemplates(permissionTemplates, sortedPerm),
  62. defaultTemplates
  63. );
  64. this.setState({
  65. ready: true,
  66. permissionTemplates: permissionTemplatesMerged,
  67. permissions: sortedPerm
  68. });
  69. }
  70. };
  71. renderTemplate(id: string) {
  72. if (!this.state.ready) {
  73. return null;
  74. }
  75. const template = this.state.permissionTemplates.find(t => t.id === id);
  76. if (!template) {
  77. return null;
  78. }
  79. return (
  80. <Template
  81. refresh={this.requestPermissions}
  82. template={template}
  83. topQualifiers={this.props.appState.qualifiers}
  84. />
  85. );
  86. }
  87. renderHome() {
  88. return (
  89. <Home
  90. permissionTemplates={this.state.permissionTemplates}
  91. permissions={this.state.permissions}
  92. ready={this.state.ready}
  93. refresh={this.requestPermissions}
  94. topQualifiers={this.props.appState.qualifiers}
  95. />
  96. );
  97. }
  98. render() {
  99. const { id } = this.props.location.query;
  100. return (
  101. <div>
  102. <Suggestions suggestions="permission_templates" />
  103. <Helmet defer={false} title={translate('permission_templates.page')} />
  104. {id && this.renderTemplate(id)}
  105. {!id && this.renderHome()}
  106. </div>
  107. );
  108. }
  109. }
  110. export default withAppStateContext(App);