aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/apps/coding-rules/components/RuleDetailsDescription.tsx
blob: 0a9ac8ee0d4c106498e638772e40374a2149c712 (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
/*
 * 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 { Button, ButtonVariety, Spinner } from '@sonarsource/echoes-react';
import {
  ButtonPrimary,
  ButtonSecondary,
  CodeSyntaxHighlighter,
  InputTextArea,
} from 'design-system';

import * as React from 'react';
import FormattingTips from '../../../components/common/FormattingTips';
import RuleTabViewer from '../../../components/rules/RuleTabViewer';
import { translate, translateWithParameters } from '../../../helpers/l10n';
import { sanitizeUserInput } from '../../../helpers/sanitize';
import { useUpdateRuleMutation } from '../../../queries/rules';
import { RuleDetails } from '../../../types/types';
import RemoveExtendedDescriptionModal from './RemoveExtendedDescriptionModal';

interface Props {
  canWrite: boolean | undefined;
  ruleDetails: RuleDetails;
}

export default function RuleDetailsDescription(props: Readonly<Props>) {
  const { ruleDetails, canWrite } = props;
  const [description, setDescription] = React.useState('');
  const [descriptionForm, setDescriptionForm] = React.useState(false);
  const [removeDescriptionModal, setDescriptionModal] = React.useState(false);

  const { mutate: updateRule, isPending: updatingRule } = useUpdateRuleMutation(undefined, () =>
    setDescriptionForm(false),
  );

  const updateDescription = (text = '') => {
    updateRule({
      key: ruleDetails.key,
      markdown_note: text,
    });
  };

  const renderExtendedDescription = () => (
    <div id="coding-rules-detail-description-extra">
      {ruleDetails.htmlNote !== undefined && (
        <CodeSyntaxHighlighter
          className="markdown sw-my-6"
          htmlAsString={sanitizeUserInput(ruleDetails.htmlNote)}
          language={ruleDetails.lang}
        />
      )}

      <div className="sw-my-6">
        {canWrite && (
          <ButtonSecondary
            onClick={() => {
              setDescription(ruleDetails.mdNote ?? '');
              setDescriptionForm(true);
            }}
          >
            {translate('coding_rules.extend_description')}
          </ButtonSecondary>
        )}
      </div>
    </div>
  );

  const renderForm = () => (
    <form
      aria-label={translate('coding_rules.detail.extend_description.form')}
      className="sw-my-6"
      onSubmit={(event: React.SyntheticEvent<HTMLFormElement>) => {
        event.preventDefault();
        updateDescription(description);
      }}
    >
      <InputTextArea
        aria-label={translate('coding_rules.extend_description')}
        className="sw-mb-2 sw-resize-y"
        id="coding-rules-detail-extend-description-text"
        size="full"
        onChange={({ currentTarget: { value } }: React.SyntheticEvent<HTMLTextAreaElement>) =>
          setDescription(value)
        }
        rows={4}
        value={description}
      />

      <div className="sw-flex sw-items-center sw-justify-between">
        <div className="sw-flex sw-items-center">
          <ButtonPrimary
            id="coding-rules-detail-extend-description-submit"
            disabled={updatingRule}
            type="submit"
          >
            {translate('save')}
          </ButtonPrimary>

          {ruleDetails.mdNote !== undefined && (
            <>
              <Button
                className="sw-ml-2"
                isDisabled={updatingRule}
                id="coding-rules-detail-extend-description-remove"
                onClick={() => setDescriptionModal(true)}
                variety={ButtonVariety.DangerOutline}
              >
                {translate('remove')}
              </Button>
              {removeDescriptionModal && (
                <RemoveExtendedDescriptionModal
                  onCancel={() => setDescriptionModal(false)}
                  onSubmit={() => {
                    setDescriptionModal(false);
                    updateDescription();
                  }}
                />
              )}
            </>
          )}

          <ButtonSecondary
            className="sw-ml-2"
            disabled={updatingRule}
            id="coding-rules-detail-extend-description-cancel"
            onClick={() => setDescriptionForm(false)}
          >
            {translate('cancel')}
          </ButtonSecondary>

          <Spinner className="sw-ml-2" isLoading={updatingRule} />
        </div>

        <FormattingTips />
      </div>
    </form>
  );

  return (
    <div className="js-rule-description">
      <RuleTabViewer ruleDetails={ruleDetails} />

      {ruleDetails.isExternal && (
        <div className="coding-rules-detail-description rule-desc markdown">
          {translateWithParameters('issue.external_issue_description', ruleDetails.name)}
        </div>
      )}

      {!ruleDetails.templateKey && (
        <div className="sw-mt-6">
          {!descriptionForm && renderExtendedDescription()}
          {descriptionForm && canWrite && renderForm()}
        </div>
      )}
    </div>
  );
}