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.

WorkspaceRuleDetails.tsx 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 { keyBy } from 'lodash';
  21. import * as React from 'react';
  22. import { getRuleDetails, getRulesApp } from '../../api/rules';
  23. import RuleDetailsDescription from '../../apps/coding-rules/components/RuleDetailsDescription';
  24. import RuleDetailsMeta from '../../apps/coding-rules/components/RuleDetailsMeta';
  25. import '../../apps/coding-rules/styles.css';
  26. import DeferredSpinner from '../../components/ui/DeferredSpinner';
  27. import { Dict, RuleDetails } from '../../types/types';
  28. interface Props {
  29. onLoad: (details: { name: string }) => void;
  30. ruleKey: string;
  31. }
  32. interface State {
  33. loading: boolean;
  34. referencedRepositories: Dict<{ key: string; language: string; name: string }>;
  35. ruleDetails?: RuleDetails;
  36. }
  37. export default class WorkspaceRuleDetails extends React.PureComponent<Props, State> {
  38. mounted = false;
  39. state: State = { loading: true, referencedRepositories: {} };
  40. componentDidMount() {
  41. this.mounted = true;
  42. this.fetchRuleDetails();
  43. }
  44. componentDidUpdate(prevProps: Props) {
  45. if (prevProps.ruleKey !== this.props.ruleKey) {
  46. this.fetchRuleDetails();
  47. }
  48. }
  49. componentWillUnmount() {
  50. this.mounted = false;
  51. }
  52. fetchRuleDetails = () => {
  53. this.setState({ loading: true });
  54. Promise.all([getRulesApp(), getRuleDetails({ key: this.props.ruleKey })]).then(
  55. ([{ repositories }, { rule }]) => {
  56. if (this.mounted) {
  57. this.setState({
  58. loading: false,
  59. referencedRepositories: keyBy(repositories, 'key'),
  60. ruleDetails: rule
  61. });
  62. this.props.onLoad({ name: rule.name });
  63. }
  64. },
  65. () => {
  66. if (this.mounted) {
  67. this.setState({ loading: false });
  68. }
  69. }
  70. );
  71. };
  72. noOp = () => {};
  73. render() {
  74. return (
  75. <DeferredSpinner loading={this.state.loading}>
  76. {this.state.ruleDetails && (
  77. <>
  78. <RuleDetailsMeta
  79. canWrite={false}
  80. hideSimilarRulesFilter={true}
  81. onFilterChange={this.noOp}
  82. onTagsChange={this.noOp}
  83. referencedRepositories={this.state.referencedRepositories}
  84. ruleDetails={this.state.ruleDetails}
  85. />
  86. <RuleDetailsDescription
  87. canWrite={false}
  88. onChange={this.noOp}
  89. ruleDetails={this.state.ruleDetails}
  90. />
  91. </>
  92. )}
  93. </DeferredSpinner>
  94. );
  95. }
  96. }