<commit_before>import React from 'react';
import PropTypes from 'prop-types';
import { FormattedMessage } from 'react-intl';
import SelectInput from './SelectInput';
import LabelInput from './LabelInput';
import Subtitle from './Subtitle';
import SortInput from './SortInput';

const getColumnTitle = (columnName, columnOptions) =>
  columnOptions.find(obj => obj.value === columnName).title;

const handleChangeSpec = (change, oldSpec, onChangeSpec, columnOptions) => {
  const newSpec = Object.assign({}, oldSpec, change);
  const axisLabelUpdateTriggers = [
    'bucketColumn',
    'subBucketColumn',
    'truncateSize',
    'metricAggregation',
    'metricColumnY',
    'metricColumnX',
    'sort',
  ];

  const shouldUpdateAxisLabels = axisLabelUpdateTriggers.some(trigger =>
      Object.keys(change).some(key => key.toString() === trigger.toString())
  ) && (newSpec.metricColumnY !== null);

  if (!shouldUpdateAxisLabels) {
    onChangeSpec(change);
  }

  let autoAxisLabelY = getColumnTitle(newSpec.metricColumnY, columnOptions);
  let autoAxisLabelX = newSpec.bucketColumn ? getColumnTitle(newSpec.bucketColumn, columnOptions) : '';

  if (newSpec.bucketColumn !== null) {
    autoAxisLabelY += ` - ${newSpec.metricAggregation}`;

    if (newSpec.truncateSize !== null) {
      let truncateOrderIndicator;

      if (newSpec.sort === 'asc') {
        truncateOrderIndicator = 'bottom';
      } else if (newSpec.sort === 'dsc') {
        truncateOrderIndicator = 'top';
      } else {
        truncateOrderIndicator = 'first';
      }

      autoAxisLabelX += ` - ${truncateOrderIndicator} ${newSpec.truncateSize}`;
    }
  }

  const axisLabelX = newSpec.axisLabelXFromUser ? newSpec.axisLabelX : autoAxisLabelX;
  const axisLabelY = newSpec.axisLabelYFromUser ? newSpec.axisLabelY : autoAxisLabelY;

  const finalSpec = Object.assign({}, newSpec, { axisLabelY, axisLabelX });

  onChangeSpec(finalSpec);
};

export default function BarConfigMenu(props) {
  const {
    visualisation,<commit_msg>[#1026] Allow bucket column to be picked before metric column (without error)<commit_after>import React from 'react';
import PropTypes from 'prop-types';
import { FormattedMessage } from 'react-intl';
import SelectInput from './SelectInput';
import LabelInput from './LabelInput';
import Subtitle from './Subtitle';
import SortInput from './SortInput';

const getColumnTitle = (columnName, columnOptions) =>
  columnOptions.find(obj => obj.value === columnName).title;

const handleChangeSpec = (change, oldSpec, onChangeSpec, columnOptions) => {
  const newSpec = Object.assign({}, oldSpec, change);
  const axisLabelUpdateTriggers = [
    'bucketColumn',
    'subBucketColumn',
    'truncateSize',
    'metricAggregation',
    'metricColumnY',
    'metricColumnX',
    'sort',
  ];

  const shouldUpdateAxisLabels = axisLabelUpdateTriggers.some(trigger =>
      Object.keys(change).some(key => key.toString() === trigger.toString())
  ) && (newSpec.metricColumnY !== null);

  if (!shouldUpdateAxisLabels) {
    onChangeSpec(change);
  }

  let autoAxisLabelY = newSpec.metricColumnY ? getColumnTitle(newSpec.metricColumnY, columnOptions) : '';
  let autoAxisLabelX = newSpec.bucketColumn ? getColumnTitle(newSpec.bucketColumn, columnOptions) : '';

  if (newSpec.bucketColumn !== null) {
    autoAxisLabelY += ` - ${newSpec.metricAggregation}`;

    if (newSpec.truncateSize !== null) {
      let truncateOrderIndicator;

      if (newSpec.sort === 'asc') {
        truncateOrderIndicator = 'bottom';
      } else if (newSpec.sort === 'dsc') {
        truncateOrderIndicator = 'top';
      } else {
        truncateOrderIndicator = 'first';
      }

      autoAxisLabelX += ` - ${truncateOrderIndicator} ${newSpec.truncateSize}`;
    }
  }

  const axisLabelX = newSpec.axisLabelXFromUser ? newSpec.axisLabelX : autoAxisLabelX;
  const axisLabelY = newSpec.axisLabelYFromUser ? newSpec.axisLabelY : autoAxisLabelY;

  const finalSpec = Object.assign({}, newSpec, { axisLabelY, axisLabelX });

  onChangeSpec(finalSpec);
};

export default function BarConfigMenu(props) {
  const {
    visualisation,
{'fraction_non_alphanumeric': 0.1294089029433228, 'fraction_numerical': 0.0009729992702505473, 'mean_word_length': 4.251596424010217, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '4332431', 'n_tokens_mistral': 1229, 'n_tokens_neox': 1224, 'n_words': 261}