]> source.dussan.org Git - sonarqube.git/blob
5ad1bdb149343ed5c1ed219776b5e7da42ab3bef
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2023 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 classNames from 'classnames';
21 import * as React from 'react';
22 import { BranchLike } from '../../../../../../types/branch-like';
23 import { getBranches } from '../../../../../../api/branches';
24 import { getRegulatoryReportUrl } from '../../../../../../api/regulatory-report';
25 import { ButtonLink } from '../../../../../../components/controls/buttons';
26 import Select, { BasicSelectOption } from '../../../../../../components/controls/Select';
27 import {
28   getBranchLikeDisplayName,
29   isBranch,
30   isMainBranch,
31 } from '../../../../../../helpers/branch-like';
32 import { translate } from '../../../../../../helpers/l10n';
33 import { Component } from '../../../../../../types/types';
34 import { orderBy } from 'lodash';
35
36 interface Props {
37   component: Pick<Component, 'key' | 'name'>;
38   branchLike?: BranchLike;
39   onClose: () => void;
40 }
41
42 interface State {
43   downloadStarted: boolean;
44   selectedBranch: string;
45   branchLikesOptions: BasicSelectOption[];
46 }
47
48 export default class RegulatoryReport extends React.PureComponent<Props, State> {
49   constructor(props: Props) {
50     super(props);
51     this.state = {
52       downloadStarted: false,
53       selectedBranch: '',
54       branchLikesOptions: [],
55     };
56   }
57
58   componentDidMount() {
59     const { component, branchLike } = this.props;
60     getBranches(component.key)
61       .then((data) => {
62         const mainBranch = data.find(isMainBranch);
63         const otherBranchSorted = orderBy(
64           data.filter(isBranch).filter((b) => !isMainBranch(b)),
65           (b) => b.name
66         );
67         const sortedBranch = mainBranch ? [mainBranch, ...otherBranchSorted] : otherBranchSorted;
68         const options = sortedBranch
69           .filter((br) => br.excludedFromPurge)
70           .map((br) => {
71             return {
72               value: getBranchLikeDisplayName(br),
73               label: getBranchLikeDisplayName(br),
74             };
75           });
76
77         let selectedBranch = '';
78         if (branchLike && isBranch(branchLike) && branchLike.excludedFromPurge) {
79           selectedBranch = getBranchLikeDisplayName(branchLike);
80         } else if (mainBranch) {
81           selectedBranch = getBranchLikeDisplayName(mainBranch);
82         }
83         this.setState({ selectedBranch, branchLikesOptions: options });
84       })
85       .catch(() => {
86         this.setState({ branchLikesOptions: [] });
87       });
88   }
89
90   onBranchSelect = (newOption: BasicSelectOption) => {
91     this.setState({ selectedBranch: newOption.value, downloadStarted: false });
92   };
93
94   render() {
95     const { component, onClose } = this.props;
96     const { downloadStarted, selectedBranch, branchLikesOptions } = this.state;
97
98     return (
99       <>
100         <div className="modal-head">
101           <h2>{translate('regulatory_report.page')}</h2>
102         </div>
103         <div className="modal-body">
104           <p>{translate('regulatory_report.description1')}</p>
105           <div className="markdown">
106             <ul>
107               <li>{translate('regulatory_report.bullet_point1')}</li>
108               <li>{translate('regulatory_report.bullet_point2')}</li>
109               <li>{translate('regulatory_report.bullet_point3')}</li>
110             </ul>
111           </div>
112           <p>{translate('regulatory_report.description2')}</p>
113           <div className="modal-field big-spacer-top">
114             <label htmlFor="regulatory-report-branch-select">
115               {translate('regulatory_page.select_branch')}
116             </label>
117             <Select
118               className="width-100"
119               inputId="regulatory-report-branch-select"
120               id="regulatory-report-branch-select-input"
121               onChange={this.onBranchSelect}
122               options={branchLikesOptions}
123               value={branchLikesOptions.find((o) => o.value === selectedBranch)}
124             />
125           </div>
126           <div className="modal-field big-spacer-top">
127             {downloadStarted && (
128               <div>
129                 <p>{translate('regulatory_page.download_start.sentence')}</p>
130               </div>
131             )}
132           </div>
133         </div>
134         <div className="modal-foot">
135           <a
136             className={classNames('button button-primary big-spacer-right', {
137               disabled: downloadStarted,
138             })}
139             download={[component.name, selectedBranch, 'regulatory report.zip']
140               .filter((s) => !!s)
141               .join(' - ')}
142             onClick={() => this.setState({ downloadStarted: true })}
143             href={getRegulatoryReportUrl(component.key, selectedBranch)}
144             target="_blank"
145             rel="noopener noreferrer"
146           >
147             {translate('download_verb')}
148           </a>
149           <ButtonLink onClick={onClose}>{translate('close')}</ButtonLink>
150         </div>
151       </>
152     );
153   }
154 }