]> source.dussan.org Git - sonarqube.git/blob
3d0aa3a30b203fe9eef9fdda40faaf31236b0de6
[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 { SelectionCard } from 'design-system';
21 import * as React from 'react';
22 import { components, OptionProps } from 'react-select';
23 import Select from '../../../components/controls/Select';
24 import Tooltip from '../../../components/controls/Tooltip';
25 import AlertErrorIcon from '../../../components/icons/AlertErrorIcon';
26 import MandatoryFieldMarker from '../../../components/ui/MandatoryFieldMarker';
27 import MandatoryFieldsExplanation from '../../../components/ui/MandatoryFieldsExplanation';
28 import { translate, translateWithParameters } from '../../../helpers/l10n';
29 import { NewCodeDefinitionType } from '../../../types/new-code-definition';
30
31 export interface BaselineSettingReferenceBranchProps {
32   branchList: BranchOption[];
33   className?: string;
34   disabled?: boolean;
35   onChangeReferenceBranch: (value: string) => void;
36   onSelect: (selection: NewCodeDefinitionType) => void;
37   referenceBranch: string;
38   selected: boolean;
39   settingLevel: 'project' | 'branch';
40 }
41
42 export interface BranchOption {
43   isDisabled?: boolean;
44   isInvalid?: boolean;
45   isMain: boolean;
46   label: string;
47   value: string;
48 }
49
50 function renderBranchOption(props: OptionProps<BranchOption, false>) {
51   const { data: option } = props;
52
53   return (
54     <components.Option {...props}>
55       {option.isInvalid ? (
56         <Tooltip
57           overlay={translateWithParameters(
58             'baseline.reference_branch.does_not_exist',
59             option.value
60           )}
61         >
62           <span>
63             {option.value} <AlertErrorIcon />
64           </span>
65         </Tooltip>
66       ) : (
67         <>
68           <span
69             title={
70               option.isDisabled
71                 ? translate('baseline.reference_branch.cannot_be_itself')
72                 : undefined
73             }
74           >
75             {option.value}
76           </span>
77           {option.isMain && (
78             <div className="badge spacer-left">{translate('branches.main_branch')}</div>
79           )}
80         </>
81       )}
82     </components.Option>
83   );
84 }
85
86 export default function NewCodeDefinitionSettingReferenceBranch(
87   props: BaselineSettingReferenceBranchProps
88 ) {
89   const { branchList, className, disabled, referenceBranch, selected, settingLevel } = props;
90
91   const currentBranch = branchList.find((b) => b.value === referenceBranch) || {
92     label: referenceBranch,
93     value: referenceBranch,
94     isMain: false,
95     isInvalid: true,
96   };
97
98   return (
99     <SelectionCard
100       className={className}
101       disabled={disabled}
102       onClick={() => props.onSelect(NewCodeDefinitionType.ReferenceBranch)}
103       selected={selected}
104       title={translate('baseline.reference_branch')}
105     >
106       <>
107         <div>
108           <p className="sw-mb-3">{translate('baseline.reference_branch.description')}</p>
109           <p className="sw-mb-4">{translate('baseline.reference_branch.usecase')}</p>
110         </div>
111         {selected && (
112           <>
113             {settingLevel === 'project' && (
114               <p className="spacer-top">{translate('baseline.reference_branch.description2')}</p>
115             )}
116             <div className="big-spacer-top display-flex-column">
117               <MandatoryFieldsExplanation className="spacer-bottom" />
118               <label className="text-middle" htmlFor="reference_branch">
119                 <strong>{translate('baseline.reference_branch.choose')}</strong>
120                 <MandatoryFieldMarker />
121               </label>
122               <Select
123                 className="little-spacer-top spacer-bottom"
124                 options={branchList}
125                 aria-label={translate('baseline.reference_branch.choose')}
126                 onChange={(option: BranchOption) => props.onChangeReferenceBranch(option.value)}
127                 value={currentBranch}
128                 components={{
129                   Option: renderBranchOption,
130                 }}
131               />
132             </div>
133           </>
134         )}
135       </>
136     </SelectionCard>
137   );
138 }