3 * Copyright (C) 2009-2024 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 import { Badge, FlagErrorIcon, FormField, InputSelect, SelectionCard } from 'design-system';
21 import * as React from 'react';
22 import { MenuPlacement, OptionProps, components } from 'react-select';
23 import Tooltip from '../../../components/controls/Tooltip';
24 import { NewCodeDefinitionLevels } from '../../../components/new-code-definition/utils';
25 import MandatoryFieldsExplanation from '../../../components/ui/MandatoryFieldsExplanation';
26 import { translate, translateWithParameters } from '../../../helpers/l10n';
27 import { NewCodeDefinitionType } from '../../../types/new-code-definition';
29 export interface BaselineSettingReferenceBranchProps {
30 branchList: BranchOption[];
33 onChangeReferenceBranch: (value: string) => void;
34 onSelect: (selection: NewCodeDefinitionType) => void;
35 referenceBranch: string;
37 settingLevel: Exclude<
38 NewCodeDefinitionLevels,
39 NewCodeDefinitionLevels.NewProject | NewCodeDefinitionLevels.Global
41 inputSelectMenuPlacement?: MenuPlacement;
44 export interface BranchOption {
52 function renderBranchOption(props: OptionProps<BranchOption, false>) {
53 const { data: option } = props;
56 <components.Option {...props}>
59 overlay={translateWithParameters(
60 'baseline.reference_branch.does_not_exist',
65 {option.value} <FlagErrorIcon className="sw-ml-2" />
73 ? translate('baseline.reference_branch.cannot_be_itself')
79 {option.isMain && <Badge className="sw-ml-2">{translate('branches.main_branch')}</Badge>}
86 export default function NewCodeDefinitionSettingReferenceBranch(
87 props: Readonly<BaselineSettingReferenceBranchProps>,
96 inputSelectMenuPlacement,
99 const currentBranch = branchList.find((b) => b.value === referenceBranch) || {
100 label: referenceBranch,
101 value: referenceBranch,
108 className={className}
110 onClick={() => props.onSelect(NewCodeDefinitionType.ReferenceBranch)}
112 title={translate('baseline.reference_branch')}
116 <p className="sw-mb-3">{translate('baseline.reference_branch.description')}</p>
117 <p className="sw-mb-4">{translate('baseline.reference_branch.usecase')}</p>
121 {settingLevel === NewCodeDefinitionLevels.Project && (
122 <p>{translate('baseline.reference_branch.description2')}</p>
124 <div className="sw-flex sw-flex-col">
125 <MandatoryFieldsExplanation className="sw-mb-2" />
128 ariaLabel={translate('baseline.reference_branch.choose')}
129 label={translate('baseline.reference_branch.choose')}
130 htmlFor="new-code-definition-reference-branch"
134 inputId="new-code-definition-reference-branch"
135 className="sw-w-abs-300"
138 onChange={(option: BranchOption) => props.onChangeReferenceBranch(option.value)}
139 value={currentBranch}
141 Option: renderBranchOption,
143 menuPlacement={inputSelectMenuPlacement}