<commit_before>import * as React from 'react';
import { Card } from '@shopify/polaris';
import { connect } from 'react-redux';
import { RootState, HitId } from 'types';
import { hitDatabaseFilteredBySearchTerm } from 'selectors/databaseFilterSettings';
import { Iterable } from 'immutable';
import DatabaseFilterPagination, {
  DatabaseFilterPaginationProps
} from './DatabaseFilterPagination';
import { DATABASE_FILTER_RESULTS_PER_PAGE } from 'constants/misc';
import ResultsList from './ResultsList';

interface Props {
  readonly hitIds: Iterable<HitId, HitId>;
}

interface State {
  readonly page: number;
  readonly maxPage: number;
}

class DatabaseFilter extends React.Component<Props, State> {
  public readonly state: State = { page: 0, maxPage: 0 };

  shouldComponentUpdate(nextProps: Props) {
    return nextProps.hitIds.equals(this.props.hitIds);
  }

  static getDerivedStateFromProps(nextProps: Props): State {
    return {
      page: 0,
      maxPage: DatabaseFilter.calculateMaxPage(
        nextProps.hitIds.size,
        DATABASE_FILTER_RESULTS_PER_PAGE
      )
    };
  }

  private static calculateMaxPage = (
    numResults: number,
    resultsPerPage: number
  ) => Math.ceil(numResults / resultsPerPage) - 1;

  private calculateHasNext = (page: number, maxPage: number) => page < maxPage;
  private calculateHasPrevious = (page: number) => page > 0;

  private onNext = () =>
    this.setState((prevState: State) => ({
      page: Math.min(prevState.page + 1, this.state.maxPage)
    }));<commit_msg>Fix Pagination buttons in DatabaseFilter sometimes being rendered unnecessarily.<commit_after>import * as React from 'react';
import { Card } from '@shopify/polaris';
import { connect } from 'react-redux';
import { RootState, HitId } from 'types';
import { hitDatabaseFilteredBySearchTerm } from 'selectors/databaseFilterSettings';
import { Iterable } from 'immutable';
import DatabaseFilterPagination, {
  DatabaseFilterPaginationProps
} from './DatabaseFilterPagination';
import { DATABASE_FILTER_RESULTS_PER_PAGE } from 'constants/misc';
import ResultsList from './ResultsList';

interface Props {
  readonly hitIds: Iterable<HitId, HitId>;
}

interface State {
  readonly page: number;
  readonly maxPage: number;
}

class DatabaseFilter extends React.Component<Props, State> {
  public readonly state: State = { page: 0, maxPage: 0 };

  shouldComponentUpdate(nextProps: Props) {
    return !nextProps.hitIds.equals(this.props.hitIds);
  }

  static getDerivedStateFromProps(nextProps: Props): State {
    return {
      page: 0,
      maxPage: DatabaseFilter.calculateMaxPage(
        nextProps.hitIds.size,
        DATABASE_FILTER_RESULTS_PER_PAGE
      )
    };
  }

  private static calculateMaxPage = (
    numResults: number,
    resultsPerPage: number
  ) => Math.ceil(numResults / resultsPerPage) - 1;

  private calculateHasNext = (page: number, maxPage: number) => page < maxPage;
  private calculateHasPrevious = (page: number) => page > 0;

  private onNext = () =>
    this.setState((prevState: State) => ({
      page: Math.min(prevState.page + 1, this.state.maxPage)
    }));
{'fraction_non_alphanumeric': 0.11018577834721333, 'fraction_numerical': 0.003843689942344651, 'mean_word_length': 4.311224489795919, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 19, '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': '5435', 'n_tokens_mistral': 949, 'n_tokens_neox': 925, 'n_words': 234}