<commit_before>// @flow
import React from 'react';
import { remote } from 'electron';
import Button from 'component/button';
import { FormRow } from 'component/common/form';
import path from 'path';

type Props = {
  type: string,
  currentPath: ?string,
  onFileChosen: (string, string) => void,
};

class FileSelector extends React.PureComponent<Props> {
  static defaultProps = {
    type: 'file',
  };

  constructor() {
    super();
    this.input = null;
  }

  handleButtonClick() {
    remote.dialog.showOpenDialog(
      {
        properties:
          this.props.type === 'file' ? ['openFile'] : ['openDirectory', 'createDirectory'],
      },
      paths => {
        if (!paths) {
          // User hit cancel, so do nothing
          return;
        }

        const filePath = paths[0];
        const extension = path.extname(filePath);
        const fileName = path.basename(filePath, extension);

        if (this.props.onFileChosen) {
          this.props.onFileChosen(filePath, fileName);
        }
      }
    );
  }

  input: ?HTMLInputElement;

  render() {
    const { type, currentPath } = this.props;

    return (
      <FormRow verticallyCentered padded>
        <Button
          button="primary"
          onClick={() => this.handleButtonClick()}
          label={type === 'file' ? __('Choose File') : __('Choose Directory')}
        />
        <input
          webkitdirectory="true"
          className="input-copyable"
          type="text"
          ref={input => {
            if (this.input) this.input = input;
          }}
          onFocus={() => {
            if (this.input) this.input.select();
          }}
          readOnly="readonly"
          value={currentPath || __('No File Chosen')}
        />
      </FormRow>
    );
  }
}

export default FileSelector;<commit_msg>allow custom labels in file-selector<commit_after>// @flow
import React from 'react';
import { remote } from 'electron';
import Button from 'component/button';
import { FormRow } from 'component/common/form';
import path from 'path';

type Props = {
  type: string,
  currentPath: ?string,
  onFileChosen: (string, string) => void,
  fileLabel: ?string,
  directoryLabel: ?string,
};

class FileSelector extends React.PureComponent<Props> {
  static defaultProps = {
    type: 'file',
  };

  constructor() {
    super();
    this.input = null;
  }

  handleButtonClick() {
    remote.dialog.showOpenDialog(
      {
        properties:
          this.props.type === 'file' ? ['openFile'] : ['openDirectory', 'createDirectory'],
      },
      paths => {
        if (!paths) {
          // User hit cancel, so do nothing
          return;
        }

        const filePath = paths[0];
        const extension = path.extname(filePath);
        const fileName = path.basename(filePath, extension);

        if (this.props.onFileChosen) {
          this.props.onFileChosen(filePath, fileName);
        }
      }
    );
  }

  input: ?HTMLInputElement;

  render() {
    const { type, currentPath, fileLabel, directoryLabel } = this.props;

    const label =
      type === 'file' ? fileLabel || __('Choose File') : directoryLabel || __('Choose Directory');

    return (
      <FormRow verticallyCentered padded>
        <Button button="primary" onClick={() => this.handleButtonClick()} label={label} />
        <input
          webkitdirectory="true"
          className="input-copyable"
          type="text"
          ref={input => {
            if (this.input) this.input = input;
          }}
          onFocus={() => {
            if (this.input) this.input.select();
          }}
          readOnly="readonly"
          value={currentPath || __('No File Chosen')}
        />
      </FormRow>
    );
  }
}

export default FileSelector;
{'fraction_non_alphanumeric': 0.1384, 'fraction_numerical': 0.0005333333333333334, 'mean_word_length': 2.208725406330197, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 23, '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': '407225', 'n_tokens_mistral': 1098, 'n_tokens_neox': 1078, 'n_words': 252}