<commit_before>import React from 'react';
import { Link } from 'react-router';
import { StickyContainer, Sticky } from 'react-sticky';
import _ from 'lodash';
import cn from 'classnames';

import { faChevronUp } from '@fortawesome/free-solid-svg-icons';
import { preventDefault } from '../utils';
import PostComment from './post-comment';
import MoreCommentsWrapper from './more-comments-wrapper';
import ErrorBoundary from './error-boundary';
import { Icon } from './fontawesome-icons';
import { faCommentPlus } from './fontawesome-custom-icons';

const minCommentsToFold = 12;

export default class PostComments extends React.Component {
  static defaultProps = { user: {} };

  addingCommentForm;
  rootEl;

  constructor(props) {
    super(props);
    this.addingCommentForm = null;
    this.rootEl = null;
    this.state = {
      // true if user manually fold expanded comments
      folded: false,
    };
  }

  openAnsweringComment = (answerText) => {
    const { post, toggleCommenting, updateCommentingText } = this.props;

    if (!post.isCommenting && !post.isSinglePost) {
      toggleCommenting(post.id);
    }
    const text = post.newCommentText || '';
    const check = new RegExp(`(^|\\s)${_.escapeRegExp(answerText)}\\s*$`);
    if (!text.match(check)) {
      const addSpace = text.length && !text.match(/\s$/);
      updateCommentingText(post.id, `${text}${addSpace ? ' ' : ''}${answerText} `);
    }
    if (this.addingCommentForm) {
      this.addingCommentForm.focus();
    }
  };

  registerCommentForm = (el) => {
    this.addingCommentForm = el;
  };

  renderAddingComment() {
    const { props } = this;
    return (
      <PostComment
        id={props.post.id}
        key={`${props.post.id}-comment-adding`}
        inputRef={this.registerCommentForm}
        isEditing={true}
        editText={props.post.newCommentText}
        updateCommentingText={props.updateCommentingText}
        saveEditingComment={props.addComment}
        toggleEditingComment={props.toggleCommenting}
        errorString={props.commentError}
        isSaving={props.post.isSavingComment}
        isSinglePost={props.post.isSinglePost}
        currentUser={props.post.user}
        isAddingComment={true}
      />
    );
  }

  renderAddCommentLink() {
    const { props } = this;
    const disabledForOthers =
      props.post.commentsDisabled && (props.post.isEditable || props.post.isModeratable);
    const toggleCommenting = props.post.isSinglePost
      ? () => {}
      : () => props.toggleCommenting(props.post.id);

    if (props.comments.length > 2 && !props.post.omittedComments) {
      return (
        <div className="comment">
          <a className="comment-icon fa-stack" onClick={preventDefault(toggleCommenting)}>
            <Icon icon={faCommentPlus} /><commit_msg>Always focus the new comment input after click on comment cloud<commit_after>import React from 'react';
import { Link } from 'react-router';
import { StickyContainer, Sticky } from 'react-sticky';
import _ from 'lodash';
import cn from 'classnames';

import { faChevronUp } from '@fortawesome/free-solid-svg-icons';
import { preventDefault } from '../utils';
import PostComment from './post-comment';
import MoreCommentsWrapper from './more-comments-wrapper';
import ErrorBoundary from './error-boundary';
import { Icon } from './fontawesome-icons';
import { faCommentPlus } from './fontawesome-custom-icons';

const minCommentsToFold = 12;

export default class PostComments extends React.Component {
  static defaultProps = { user: {} };

  addingCommentForm;
  rootEl;

  constructor(props) {
    super(props);
    this.addingCommentForm = null;
    this.rootEl = null;
    this.state = {
      // true if user manually fold expanded comments
      folded: false,
    };
  }

  openAnsweringComment = (answerText) => {
    const { post, toggleCommenting, updateCommentingText } = this.props;

    if (!post.isCommenting && !post.isSinglePost) {
      toggleCommenting(post.id);
    }
    const text = post.newCommentText || '';
    const check = new RegExp(`(^|\\s)${_.escapeRegExp(answerText)}\\s*$`);
    if (!text.match(check)) {
      const addSpace = text.length && !text.match(/\s$/);
      updateCommentingText(post.id, `${text}${addSpace ? ' ' : ''}${answerText} `);
    }
    if (this.addingCommentForm) {
      this.addingCommentForm.focus();
    }
  };

  registerCommentForm = (el) => {
    this.addingCommentForm = el;
  };

  renderAddingComment() {
    const { props } = this;
    return (
      <PostComment
        id={props.post.id}
        key={`${props.post.id}-comment-adding`}
        ref={this.registerCommentForm}
        isEditing={true}
        editText={props.post.newCommentText}
        updateCommentingText={props.updateCommentingText}
        saveEditingComment={props.addComment}
        toggleEditingComment={props.toggleCommenting}
        errorString={props.commentError}
        isSaving={props.post.isSavingComment}
        isSinglePost={props.post.isSinglePost}
        currentUser={props.post.user}
        isAddingComment={true}
      />
    );
  }

  renderAddCommentLink() {
    const { props } = this;
    const disabledForOthers =
      props.post.commentsDisabled && (props.post.isEditable || props.post.isModeratable);
    const toggleCommenting = props.post.isSinglePost
      ? () => {}
      : () => props.toggleCommenting(props.post.id);

    if (props.comments.length > 2 && !props.post.omittedComments) {
      return (
        <div className="comment">
          <a className="comment-icon fa-stack" onClick={preventDefault(toggleCommenting)}>
            <Icon icon={faCommentPlus} />
{'fraction_non_alphanumeric': 0.1383714741883981, 'fraction_numerical': 0.0010643959552953698, 'mean_word_length': 3.229557389347337, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '647221', 'n_tokens_mistral': 1707, 'n_tokens_neox': 1670, 'n_words': 324}