<commit_before>import React, { Component, Fragment } from "react";

import { ChatMessage } from './chat-message';
import { ChatScrollContainer } from "./chat-scroll-container";
import { UnreadNotice } from "./unread-notice";
import { ResubscribeElement } from "./resubscribe-element";
import { BacklogElement } from "./backlog-element";

const MAX_BACKLOG_SIZE = 1000;
const DEFAULT_BACKLOG_SIZE = 200;
const PAGE_SIZE = 50;
const INITIAL_LOAD = 20;


export class ChatWindow extends Component {
  constructor(props) {
    super(props);
    this.state = {
      numPages: 1,
    };

    this.hasAskedForMessages = false;
    
    this.dismissUnread = this.dismissUnread.bind(this);
    this.scrollIsAtBottom = this.scrollIsAtBottom.bind(this);
    this.scrollIsAtTop = this.scrollIsAtTop.bind(this);

    this.scrollReference = React.createRef();
    this.unreadReference = React.createRef();
  }

  componentDidMount() {
    this.initialFetch();

    if (this.state.numPages === 1 && this.props.unreadCount < INITIAL_LOAD) {
      this.dismissUnread();
      this.scrollToBottom();
    }
  }

  initialFetch() {
    const { props } = this;
    if (props.messages.length > 0) {
      const unreadUnloaded = props.unreadCount - props.messages.length;

      if (unreadUnloaded <= MAX_BACKLOG_SIZE &&
          unreadUnloaded + INITIAL_LOAD > DEFAULT_BACKLOG_SIZE) {
        this.fetchBacklog(unreadUnloaded + INITIAL_LOAD);
      } else {
        this.fetchBacklog(DEFAULT_BACKLOG_SIZE);
      }
    } else {
      setTimeout(() => {
        this.initialFetch();
      }, 2000);
    }
  }

  componentDidUpdate(prevProps, prevState) {
    const { props, state } = this;

    if (props.isChatMissing) {
      props.history.push("/~chat");
    } else if (props.messages.length >= prevProps.messages.length + 10) {
      this.hasAskedForMessages = false;
      let numPages = props.unreadCount > 0 ? 
        Math.ceil(props.unreadCount / PAGE_SIZE) : 1;

      if (this.state.numPages === numPages) {
        if (props.unreadCount > 20) {
          this.scrollToUnread();    <commit_msg>chat: fix page number reset<commit_after>import React, { Component, Fragment } from "react";

import { ChatMessage } from './chat-message';
import { ChatScrollContainer } from "./chat-scroll-container";
import { UnreadNotice } from "./unread-notice";
import { ResubscribeElement } from "./resubscribe-element";
import { BacklogElement } from "./backlog-element";

const MAX_BACKLOG_SIZE = 1000;
const DEFAULT_BACKLOG_SIZE = 200;
const PAGE_SIZE = 50;
const INITIAL_LOAD = 20;


export class ChatWindow extends Component {
  constructor(props) {
    super(props);
    this.state = {
      numPages: 1,
    };

    this.hasAskedForMessages = false;
    
    this.dismissUnread = this.dismissUnread.bind(this);
    this.scrollIsAtBottom = this.scrollIsAtBottom.bind(this);
    this.scrollIsAtTop = this.scrollIsAtTop.bind(this);

    this.scrollReference = React.createRef();
    this.unreadReference = React.createRef();
  }

  componentDidMount() {
    this.initialFetch();

    if (this.state.numPages === 1 && this.props.unreadCount < INITIAL_LOAD) {
      this.dismissUnread();
      this.scrollToBottom();
    }
  }

  initialFetch() {
    const { props } = this;
    if (props.messages.length > 0) {
      const unreadUnloaded = props.unreadCount - props.messages.length;

      if (unreadUnloaded <= MAX_BACKLOG_SIZE &&
          unreadUnloaded + INITIAL_LOAD > DEFAULT_BACKLOG_SIZE) {
        this.fetchBacklog(unreadUnloaded + INITIAL_LOAD);
      } else {
        this.fetchBacklog(DEFAULT_BACKLOG_SIZE);
      }
    } else {
      setTimeout(() => {
        this.initialFetch();
      }, 2000);
    }
  }

  componentDidUpdate(prevProps, prevState) {
    const { props, state } = this;

    if (props.isChatMissing) {
      props.history.push("/~chat");
    } else if (props.messages.length >= prevProps.messages.length + 10) {
      this.hasAskedForMessages = false;
      let numPages = props.unreadCount > 0 ? 
        Math.ceil(props.unreadCount / PAGE_SIZE) : this.state.numPages;

      if (this.state.numPages === numPages) {
        if (props.unreadCount > 20) {
          this.scrollToUnread();    
{'fraction_non_alphanumeric': 0.12547619047619046, 'fraction_numerical': 0.01119047619047619, 'mean_word_length': 3.043310875842156, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, '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': '414506', 'n_tokens_mistral': 1380, 'n_tokens_neox': 1294, 'n_words': 252}