<commit_before>// Aurelia
import {
  inject,  // eslint-disable-line
  LogManager
} from 'aurelia-framework';  // eslint-disable-line
import { Router } from 'aurelia-router';  // eslint-disable-line
import { EventAggregator } from 'aurelia-event-aggregator';  // eslint-disable-line

// Third party
import { json } from 'd3';

// Injectable
import States from 'services/states';  // eslint-disable-line

// Utils
import examples from 'configs/examples';
import { updateConfigs } from 'app-actions';
import readJsonFile from 'utils/read-json-file';
import validateConfig from 'utils/validate-config';

const logger = LogManager.getLogger('home');


@inject(EventAggregator, Router, States)
export class Home {

  constructor (event, router, states) {
    this.event = event;

    this.router = router;

    this.store = states.store;
    this.unsubscribeStore = this.store.subscribe(this.update.bind(this));

    this.examples = examples;
  }

  /* ----------------------- Aurelia-specific methods ----------------------- */

  /**
   * Called once the component is attached.
   */
  attached () {
    this.selectConfigButton.addEventListener(
      'click', this.selectConfig.bind(this)
    );
  }

  /**
   * Called once the component is detached.
   */
  detached () {
    // Unsubscribe from redux store
    this.unsubscribeStore();
  }

  /* ---------------------------- Class methods ----------------------------- */

  loadExample (example) {
    json(example.url, (error, config) => {
      if (error) {
        logger.error(error);
        this.event.publish(
          'showGlobalError', ['Could not load example config', 30000]
        );
        return;
      }

      this.setState(config);
    });
  }

  selectedConfigChanged () {
    let results;

    try {
      results = readJsonFile(this.selectedConfig[0]);
    } catch (error) {
      logger.error(error);
    }

    results
      .then(resultsJson => this.setState(resultsJson))
      .catch(error => logger.error(error));
  }

  selectConfig (event) {
    if (event.artificial) { return; }

    const newEvent = new MouseEvent(event.type, event);
    newEvent.artificial = true;

    this.inputConfigFile.dispatchEvent(newEvent);
  }

  setState (config) {
    if (validateConfig(config.fgm, config.hgl)) {
      this.store.dispatch(updateConfigs(config));
      this.router.navigateToRoute('explore');
    } else {
      this.event.publish(
        'showGlobalError', ['Corrupted config file', 3000]
      );
    }
  }

  update () {}
}<commit_msg>Load config from queryParams. Preserve queryParams. Show error for 3sec only.<commit_after>// Aurelia
import {
  inject,  // eslint-disable-line
  LogManager
} from 'aurelia-framework';  // eslint-disable-line
import { Router } from 'aurelia-router';  // eslint-disable-line
import { EventAggregator } from 'aurelia-event-aggregator';  // eslint-disable-line

// Third party
import { json } from 'd3';

// Injectable
import States from 'services/states';  // eslint-disable-line

// Utils
import examples from 'configs/examples';
import { updateConfigs } from 'app-actions';
import readJsonFile from 'utils/read-json-file';
import validateConfig from 'utils/validate-config';

const logger = LogManager.getLogger('home');


@inject(EventAggregator, Router, States)
export class Home {

  constructor (event, router, states) {
    this.event = event;

    this.router = router;

    this.store = states.store;

    this.examples = examples;
  }

  /* ----------------------- Aurelia-specific methods ----------------------- */

  /**
   * Called once the component is attached.
   */
  attached (...args) {
    this.selectConfigButton.addEventListener(
      'click', this.selectConfig.bind(this)
    );

    if (this.queryParams.config) {
      this.loadExample({ url: this.queryParams.config });
    }
  }

  activate (params, routeConfig, navigationInstruction) {
    this.queryParams = navigationInstruction.queryParams;
  }

  /* ---------------------------- Class methods ----------------------------- */

  loadExample (example) {
    json(example.url, (error, config) => {
      if (error) {
        logger.error(error);
        this.event.publish(
          'showGlobalError', ['Could not load example config', 5000]
        );
        return;
      }

      this.setState(config);
    });
  }

  selectedConfigChanged () {
    let results;

    try {
      results = readJsonFile(this.selectedConfig[0]);
    } catch (error) {
      logger.error(error);
    }

    results
      .then(resultsJson => this.setState(resultsJson))
      .catch(error => logger.error(error));
  }

  selectConfig (event) {
    if (event.artificial) { return; }

    const newEvent = new MouseEvent(event.type, event);
    newEvent.artificial = true;

    this.inputConfigFile.dispatchEvent(newEvent);
  }

  setState (config) {
    if (validateConfig(config.fgm, config.hgl)) {
      this.store.dispatch(updateConfigs(config));
      this.router.navigateToRoute('explore', this.queryParams);
    } else {
      this.event.publish(
        'showGlobalError', ['Corrupted config file', 3000]
      );
    }
  }
}
{'fraction_non_alphanumeric': 0.16741551084196132, 'fraction_numerical': 0.0042977143973432315, 'mean_word_length': 2.4901158827539196, 'pattern_counts': {'":': 0, '<': 3, '<?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': '4633887', 'n_tokens_mistral': 1556, 'n_tokens_neox': 1503, 'n_words': 322}