<commit_before>import React from 'react'
import { throttle, isEmpty, sortBy } from 'lodash'
const { array, bool, func, object } = React.PropTypes
import cx from 'classnames'
import Message from './Message'
import { position } from '../util/scrolling'
import { findDOMNode } from 'react-dom'
import { appendComment } from '../actions/comments'
import { updatePostReadTime } from '../actions/posts'
import { getSocket, socketUrl } from '../client/websockets'

export default class MessageSection extends React.Component {
  static propTypes = {
    messages: array,
    onScrollToTop: func,
    pending: bool,
    thread: object
  }

  static contextTypes = {
    currentUser: object,
    dispatch: func
  }

  constructor(props) {
    super(props)
    this.state = {
      scrolledUp: false
    }
  }

  componentDidMount() {
    this.scrollToBottom()
  }

  componentDidUpdate(prevProps) {
    const messagesLength = this.props.messages.length
    const oldMessagesLength = prevProps.messages.length
    const { scrolledUp } = this.state
    if (!scrolledUp && messagesLength !== oldMessagesLength) this.scrollToBottom()
  }

  scrollToMessage(id) {
    const message = findDOMNode(this['message' + id])
    const messageTop = position(message, this.list).y -
      document.querySelector('.thread .header').offsetHeight -
      document.getElementById('topNav').offsetHeight - 40
    this.list.scrollTop = messageTop
  }

  handleScroll = throttle(target => {
    const { scrolledUp } = this.state
    const { scrollTop, scrollHeight, offsetHeight } = target
    const onBottom = scrollTop > scrollHeight - offsetHeight
    if (!onBottom && !scrolledUp) this.setState({ scrolledUp: true })
    else if (onBottom && scrolledUp) {
      this.setState({ scrolledUp: false })
      this.markAsRead()
    }
    if (scrollTop <= 20 && this.props.onScrollToTop) this.props.onScrollToTop()
  }, 500, {trailing: true})

  scrollToBottom = () => {
    this.list.scrollTop = this.list.scrollHeight
    this.markAsRead()
  }

  markAsRead() {
    const { thread } = this.props
    const { dispatch } = this.context
    dispatch(updatePostReadTime(thread.id))
  }

  render() {
    const { thread } = this.props
    const messages = sortBy(this.props.messages || [], 'created_at')
    const { currentUser } = this.context
    const { scrolledUp } = this.state

    const latestMessage = messages.length && messages[messages.length - 1]
    const latestFromOther = latestMessage && latestMessage.user_id !== currentUser.id<commit_msg>fix linter warnings<commit_after>import React from 'react'
import { throttle, isEmpty, sortBy } from 'lodash'
const { array, bool, func, object } = React.PropTypes
import cx from 'classnames'
import Message from './Message'
import { position } from '../util/scrolling'
import { findDOMNode } from 'react-dom'
import { updatePostReadTime } from '../actions/posts'

export default class MessageSection extends React.Component {
  static propTypes = {
    messages: array,
    onScrollToTop: func,
    pending: bool,
    thread: object
  }

  static contextTypes = {
    currentUser: object,
    dispatch: func
  }

  constructor (props) {
    super(props)
    this.state = {
      scrolledUp: false
    }
  }

  componentDidMount () {
    this.scrollToBottom()
  }

  componentDidUpdate (prevProps) {
    const messagesLength = this.props.messages.length
    const oldMessagesLength = prevProps.messages.length
    const { scrolledUp } = this.state
    if (!scrolledUp && messagesLength !== oldMessagesLength) this.scrollToBottom()
  }

  scrollToMessage (id) {
    const message = findDOMNode(this['message' + id])
    const messageTop = position(message, this.list).y -
      document.querySelector('.thread .header').offsetHeight -
      document.getElementById('topNav').offsetHeight - 40
    this.list.scrollTop = messageTop
  }

  handleScroll = throttle(target => {
    const { scrolledUp } = this.state
    const { scrollTop, scrollHeight, offsetHeight } = target
    const onBottom = scrollTop > scrollHeight - offsetHeight
    if (!onBottom && !scrolledUp) this.setState({ scrolledUp: true })
    else if (onBottom && scrolledUp) {
      this.setState({ scrolledUp: false })
      this.markAsRead()
    }
    if (scrollTop <= 20 && this.props.onScrollToTop) this.props.onScrollToTop()
  }, 500, {trailing: true})

  scrollToBottom = () => {
    this.list.scrollTop = this.list.scrollHeight
    this.markAsRead()
  }

  markAsRead () {
    const { thread } = this.props
    const { dispatch } = this.context
    dispatch(updatePostReadTime(thread.id))
  }

  render () {
    const { thread } = this.props
    const messages = sortBy(this.props.messages || [], 'created_at')
    const { currentUser } = this.context
    const { scrolledUp } = this.state

    const latestMessage = messages.length && messages[messages.length - 1]
    const latestFromOther = latestMessage && latestMessage.user_id !== currentUser.id
{'fraction_non_alphanumeric': 0.11796843383245649, 'fraction_numerical': 0.003237555645487657, 'mean_word_length': 3.413392857142857, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '906613', 'n_tokens_mistral': 1471, 'n_tokens_neox': 1429, 'n_words': 349}