aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/apps/coding-rules/components/ActivationFormModal.tsx
blob: f5af3a127381737e981d3913d76686d0bc316338 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
 * SonarQube
 * Copyright (C) 2009-2024 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

import {
  ButtonPrimary,
  FlagMessage,
  FormField,
  InputField,
  InputSelect,
  InputTextArea,
  LabelValueSelectOption,
  Modal,
  Note,
} from 'design-system';
import * as React from 'react';
import { Profile } from '../../../api/quality-profiles';
import DocumentationLink from '../../../components/common/DocumentationLink';
import { DocLink } from '../../../helpers/doc-links';
import { translate } from '../../../helpers/l10n';
import { sanitizeString } from '../../../helpers/sanitize';
import { useActivateRuleMutation } from '../../../queries/quality-profiles';
import { IssueSeverity } from '../../../types/issues';
import { Dict, Rule, RuleActivation, RuleDetails } from '../../../types/types';
import { sortProfiles } from '../../quality-profiles/utils';
import { SeveritySelect } from './SeveritySelect';

interface Props {
  activation?: RuleActivation;
  modalHeader: string;
  onClose: () => void;
  onDone?: (severity: string) => Promise<void> | void;
  profiles: Profile[];
  rule: Rule | RuleDetails;
}

interface ProfileWithDepth extends Profile {
  depth: number;
}

const MIN_PROFILES_TO_ENABLE_SELECT = 2;
const FORM_ID = 'rule-activation-modal-form';

export default function ActivationFormModal(props: Readonly<Props>) {
  const { activation, rule, profiles, modalHeader } = props;
  const { mutate: activateRule, isPending: submitting } = useActivateRuleMutation((data) => {
    props.onDone?.(data.severity as string);
    props.onClose();
  });

  const profilesWithDepth = getQualityProfilesWithDepth(profiles, rule.lang);
  const [profile, setProfile] = React.useState(profilesWithDepth[0]);
  const [params, setParams] = React.useState(getRuleParams({ activation, rule }));
  const [severity, setSeverity] = React.useState(
    (activation ? activation.severity : rule.severity) as IssueSeverity,
  );

  const profileOptions = profilesWithDepth.map((p) => ({ label: p.name, value: p }));
  const isCustomRule = !!(rule as RuleDetails).templateKey;
  const activeInAllProfiles = profilesWithDepth.length <= 0;
  const isUpdateMode = !!activation;

  const handleFormSubmit = (event: React.SyntheticEvent<HTMLFormElement>) => {
    event.preventDefault();
    const data = {
      key: profile?.key ?? '',
      params,
      rule: rule.key,
      severity,
    };
    activateRule(data);
  };

  const handleParameterChange = (
    event: React.SyntheticEvent<HTMLInputElement | HTMLTextAreaElement>,
  ) => {
    const { name, value } = event.currentTarget;
    setParams({ ...params, [name]: value });
  };

  const makeScrollable = (rule.params?.length ?? 0) > 1;

  return (
    <Modal
      headerTitle={modalHeader}
      onClose={props.onClose}
      loading={submitting}
      isOverflowVisible={!makeScrollable}
      isScrollable={makeScrollable}
      primaryButton={
        <ButtonPrimary disabled={submitting || activeInAllProfiles} form={FORM_ID} type="submit">
          {isUpdateMode ? translate('save') : translate('coding_rules.activate')}
        </ButtonPrimary>
      }
      secondaryButtonLabel={translate('cancel')}
      body={
        <form className="sw-pb-10" id={FORM_ID} onSubmit={handleFormSubmit}>
          {!isUpdateMode && activeInAllProfiles && (
            <FlagMessage className="sw-mb-2" variant="info">
              {translate('coding_rules.active_in_all_profiles')}
            </FlagMessage>
          )}

          <FlagMessage className="sw-mb-4" variant="info">
            {translate('coding_rules.severity_deprecated')}
            <DocumentationLink
              className="sw-ml-2 sw-whitespace-nowrap"
              to={DocLink.CleanCodeIntroduction}
            >
              {translate('learn_more')}
            </DocumentationLink>
          </FlagMessage>

          <FormField
            ariaLabel={translate('coding_rules.quality_profile')}
            label={translate('coding_rules.quality_profile')}
            htmlFor="coding-rules-quality-profile-select-input"
          >
            <InputSelect
              id="coding-rules-quality-profile-select"
              inputId="coding-rules-quality-profile-select-input"
              isClearable={false}
              isDisabled={submitting || profilesWithDepth.length < MIN_PROFILES_TO_ENABLE_SELECT}
              onChange={({ value }: LabelValueSelectOption<ProfileWithDepth>) => {
                setProfile(value);
              }}
              getOptionLabel={({ value }: LabelValueSelectOption<ProfileWithDepth>) =>
                '   '.repeat(value.depth) + value.name
              }
              options={profileOptions}
              value={profileOptions.find(({ value }) => value.key === profile?.key)}
            />
          </FormField>

          <FormField
            ariaLabel={translate('severity')}
            label={translate('severity')}
            htmlFor="coding-rules-severity-select"
          >
            <SeveritySelect
              isDisabled={submitting}
              onChange={({ value }: LabelValueSelectOption<IssueSeverity>) => {
                setSeverity(value);
              }}
              severity={severity}
            />
          </FormField>

          {isCustomRule ? (
            <Note as="p" className="sw-my-4">
              {translate('coding_rules.custom_rule.activation_notice')}
            </Note>
          ) : (
            rule.params?.map((param) => (
              <FormField label={param.key} key={param.key} htmlFor={param.key}>
                {param.type === 'TEXT' ? (
                  <InputTextArea
                    id={param.key}
                    disabled={submitting}
                    name={param.key}
                    onChange={handleParameterChange}
                    placeholder={param.defaultValue}
                    rows={3}
                    size="full"
                    value={params[param.key] ?? ''}
                  />
                ) : (
                  <InputField
                    id={param.key}
                    disabled={submitting}
                    name={param.key}
                    onChange={handleParameterChange}
                    placeholder={param.defaultValue}
                    size="full"
                    type="text"
                    value={params[param.key] ?? ''}
                  />
                )}
                {param.htmlDesc !== undefined && (
                  <Note
                    as="div"
                    // eslint-disable-next-line react/no-danger
                    dangerouslySetInnerHTML={{ __html: sanitizeString(param.htmlDesc) }}
                  />
                )}
              </FormField>
            ))
          )}
        </form>
      }
    />
  );
}

function getQualityProfilesWithDepth(
  profiles: Profile[] = [],
  ruleLang?: string,
): ProfileWithDepth[] {
  return sortProfiles(
    profiles.filter(
      (profile) =>
        !profile.isBuiltIn &&
        profile.actions &&
        profile.actions.edit &&
        profile.language === ruleLang,
    ),
  ).map((profile) => ({
    ...profile,
    // Decrease depth by 1, so the top level starts at 0
    depth: profile.depth - 1,
  }));
}

function getRuleParams({
  activation,
  rule,
}: {
  activation?: RuleActivation;
  rule: RuleDetails | Rule;
}) {
  const params: Dict<string> = {};
  if (rule?.params) {
    for (const param of rule.params) {
      params[param.key] = param.defaultValue ?? '';
    }
    if (activation?.params) {
      for (const param of activation.params) {
        params[param.key] = param.value;
      }
    }
  }
  return params;
}