<commit_before>import SortDirection from 'react-virtualized/dist/commonjs/Table/SortDirection';

import RenderData from '~/renderers/renderData';
import reactRenderers from '~/react/renderers';

import BaseTable from './base';

export {SortDirection};

export default function sortableTable(Table) {
  return class SortableTable extends BaseTable {
    static propTypes = {
      sortable: React.PropTypes.bool,
      initialSort: React.PropTypes.shape({
        sortBy: React.PropTypes.string,
        sortDirection: React.PropTypes.string
      }),
      onSort: React.PropTypes.func,
      sortBy: React.PropTypes.string,
      sortDirection: React.PropTypes.string
    };

    static defaultProps = {
      ...BaseTable.defaultProps,
      sortable: true,
      initialSort: {}
    };

    constructor(props) {
      super(props);

      this.state = this.createInitialState();
    }

    createInitialState() {
      return {
        sortBy: this.props.initialSort.sortBy,
        sortDirection: this.props.initialSort.sortDirection
      };
    }

    // Only sort if you are non-infinite load, or if you have an `onSort` prop.
    isSortable = () => {
      return this.props.sortable && (
        !this.props.infiniteLoad ||
        !!this.props.onSort
      );
    }

    onSort = props => {
      if (this.props.onSort) {
        this.props.onSort(props);
      } else if (!this.props.infiniteLoad) {
        // The default sort implementation does not support tables with
        // infinite loading since we only sort rows that are currently loaded.
        if (this.state.sortBy == this.props.sortBy &&
            this.state.sortDirection == SortDirection.DESC) {
          this.setState(this.createInitialState());
        } else {
          this.setState(props);
        }
      }
    }

    doSort() {
      return !!this.getSortBy();
    }

    getSortBy() {
      return this.props.sortBy || this.state.sortBy;
    }

    getSortDirection() {
      return this.props.sortDirection || this.state.sortDirection;
    }

    getColumnProps = getter => {
      return column => ({
        ...getter(column),
        sortable: column.isSortable
      });
    }

    rowsModifier = rows => {
      const renderData = new RenderData(this.props.dataType, null, {
        viewTypes: this.props.viewTypes,
        ...this.props.renderOptions
      });

      const columnProps = this.props.columns
        .map(column => reactRenderers.getTableProps(column, renderData))
        .find(columnProps => columnProps.label == this.getSortBy());

      const getSortValue = function(row) {
        renderData.dataValue = row;
        return columnProps.viewType.getValue(renderData);
      };

      if (!columnProps) {
        return rows;
      }

      return rows
        .update(models => this.doSort() ?
          models
            .sortBy(getSortValue, (a, b) => {
              // TODO: Put sort in the column props like filter is?
              const nullA = (a === null || typeof a == 'undefined');
              const nullB = (b === null || typeof b == 'undefined');
              if (nullA && nullB) {
                return 0;
              } else if (nullA) {
                return 1;
              } else if (nullB) {
                return -1;
              } else {
                return a > b ? 1 : a < b ? -1 : 0;
              }
            })
            .update(models =>
                this.getSortDirection() == SortDirection.DESC ?
                  models.reverse() :
                  models
            ) :
          models
        );
    }

    render() {
      if (this.isSortable()) {<commit_msg>Prevent local sort when sort is managed.<commit_after>import SortDirection from 'react-virtualized/dist/commonjs/Table/SortDirection';

import RenderData from '~/renderers/renderData';
import reactRenderers from '~/react/renderers';

import BaseTable from './base';

export {SortDirection};

export default function sortableTable(Table) {
  return class SortableTable extends BaseTable {
    static propTypes = {
      sortable: React.PropTypes.bool,
      initialSort: React.PropTypes.shape({
        sortBy: React.PropTypes.string,
        sortDirection: React.PropTypes.string
      }),
      onSort: React.PropTypes.func,
      sortBy: React.PropTypes.string,
      sortDirection: React.PropTypes.string
    };

    static defaultProps = {
      ...BaseTable.defaultProps,
      sortable: true,
      initialSort: {}
    };

    constructor(props) {
      super(props);

      this.state = this.createInitialState();
    }

    createInitialState() {
      return {
        sortBy: this.props.initialSort.sortBy,
        sortDirection: this.props.initialSort.sortDirection
      };
    }

    // Only sort if you are non-infinite load, or if you have an `onSort` prop.
    isSortable = () => {
      return this.props.sortable && (
        !this.props.infiniteLoad ||
        !!this.props.onSort
      );
    }

    isInfiniteSort = () => {
      return this.props.initialLoad && !!this.props.onSort;
    }

    onSort = props => {
      if (this.props.onSort) {
        this.props.onSort(props);
      } else if (!this.props.infiniteLoad) {
        // The default sort implementation does not support tables with
        // infinite loading since we only sort rows that are currently loaded.
        if (this.state.sortBy == this.props.sortBy &&
            this.state.sortDirection == SortDirection.DESC) {
          this.setState(this.createInitialState());
        } else {
          this.setState(props);
        }
      }
    }

    doSort() {
      return !!this.getSortBy();
    }

    getSortBy() {
      return this.props.sortBy || this.state.sortBy;
    }

    getSortDirection() {
      return this.props.sortDirection || this.state.sortDirection;
    }

    getColumnProps = getter => {
      return column => ({
        ...getter(column),
        sortable: column.isSortable
      });
    }

    rowsModifier = rows => {
      const renderData = new RenderData(this.props.dataType, null, {
        viewTypes: this.props.viewTypes,
        ...this.props.renderOptions
      });

      const columnProps = this.props.columns
        .map(column => reactRenderers.getTableProps(column, renderData))
        .find(columnProps => columnProps.label == this.getSortBy());

      const getSortValue = function(row) {
        renderData.dataValue = row;
        return columnProps.viewType.getValue(renderData);
      };

      if (!columnProps) {
        return rows;
      }

      return rows
        .update(models => this.doSort() ?
          models
            .sortBy(getSortValue, (a, b) => {
              // TODO: Put sort in the column props like filter is?
              const nullA = (a === null || typeof a == 'undefined');
              const nullB = (b === null || typeof b == 'undefined');
              if (nullA && nullB) {
                return 0;
              } else if (nullA) {
                return 1;
              } else if (nullB) {
                return -1;
              } else {
                return a > b ? 1 : a < b ? -1 : 0;
              }
            })
            .update(models =>
                this.getSortDirection() == SortDirection.DESC ?
                  models.reverse() :
                  models
            ) :
          models
        );
    }

    render() {
      if (this.isInfiniteSort()) {
        const mergedProps = this.mergeProps({
          getColumnProps: this.getColumnProps,
          sort: this.onSort,
          sortBy: this.getSortBy(),
          sortDirection: this.getSortDirection()
        });
        return (
          <Table
            ref={table => this.table = table}
            {...mergedProps}
          />
        );
      } else if (isSortable()) {
{'fraction_non_alphanumeric': 0.1173823605039856, 'fraction_numerical': 0.0015428130624839291, 'mean_word_length': 1.8328477785870356, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5550145', 'n_tokens_mistral': 2197, 'n_tokens_neox': 2150, 'n_words': 484}