aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/components/issue/popups/CommentPopup.tsx
blob: 209d19909f911cff52ef60f3a46d9b6f301fed47 (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
/*
 * SonarQube
 * Copyright (C) 2009-2021 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 * as React from 'react';
import { Button, ResetButtonLink } from '../../../components/controls/buttons';
import { DropdownOverlay } from '../../../components/controls/Dropdown';
import { PopupPlacement } from '../../../components/ui/popups';
import { KeyboardCodes } from '../../../helpers/keycodes';
import { translate } from '../../../helpers/l10n';
import { IssueComment } from '../../../types/types';
import FormattingTips from '../../common/FormattingTips';

export interface CommentPopupProps {
  comment?: Pick<IssueComment, 'markdown'>;
  onComment: (text: string) => void;
  toggleComment: (visible: boolean) => void;
  placeholder: string;
  placement?: PopupPlacement;
  autoTriggered?: boolean;
}

interface State {
  textComment: string;
}

export default class CommentPopup extends React.PureComponent<CommentPopupProps, State> {
  constructor(props: CommentPopupProps) {
    super(props);
    this.state = {
      textComment: props.comment ? props.comment.markdown : ''
    };
  }

  handleCommentChange = (event: React.ChangeEvent<HTMLTextAreaElement>) => {
    this.setState({ textComment: event.target.value });
  };

  handleCommentClick = () => {
    if (this.state.textComment.trim().length > 0) {
      this.props.onComment(this.state.textComment);
    }
  };

  handleCancelClick = () => {
    this.props.toggleComment(false);
  };

  handleKeyboard = (event: React.KeyboardEvent) => {
    if (event.nativeEvent.code === KeyboardCodes.Enter && (event.metaKey || event.ctrlKey)) {
      this.handleCommentClick();
    } else if (
      [
        KeyboardCodes.UpArrow,
        KeyboardCodes.DownArrow,
        KeyboardCodes.LeftArrow,
        KeyboardCodes.RightArrow
      ].includes(event.nativeEvent.code as KeyboardCodes)
    ) {
      // Arrow keys
      event.stopPropagation();
    }
  };

  render() {
    const { comment, autoTriggered } = this.props;
    return (
      <DropdownOverlay placement={this.props.placement}>
        <div className="issue-comment-bubble-popup">
          <div className="issue-comment-form-text">
            <textarea
              autoFocus={true}
              onChange={this.handleCommentChange}
              onKeyDown={this.handleKeyboard}
              placeholder={this.props.placeholder}
              rows={2}
              value={this.state.textComment}
            />
          </div>
          <div className="issue-comment-form-footer">
            <div className="issue-comment-form-actions">
              <Button
                className="js-issue-comment-submit little-spacer-right"
                disabled={this.state.textComment.trim().length < 1}
                onClick={this.handleCommentClick}>
                {comment && translate('save')}
                {!comment && translate('issue.comment.submit')}
              </Button>
              <ResetButtonLink className="js-issue-comment-cancel" onClick={this.handleCancelClick}>
                {autoTriggered ? translate('skip') : translate('cancel')}
              </ResetButtonLink>
            </div>
            <div className="issue-comment-form-tips">
              <FormattingTips />
            </div>
          </div>
        </div>
      </DropdownOverlay>
    );
  }
}