<commit_before>import _ from 'intl'
import ActionButton from 'action-button'
import ActionToggle from 'action-toggle'
import GenericInput from 'json-schema-input'
import Icon from 'icon'
import isEmpty from 'lodash/isEmpty'
import map from 'lodash/map'
import React, { Component } from 'react'
import size from 'lodash/size'
import { addSubscriptions } from 'utils'
import { generateUiSchema } from 'xo-json-schema-input'
import { lastly } from 'promise-toolbox'
import { Row, Col } from 'grid'
import {
  configurePlugin,
  disablePluginAutoload,
  enablePluginAutoload,
  loadPlugin,
  purgePluginConfiguration,
  subscribePlugins,
  testPlugin,
  unloadPlugin
} from 'xo'

class Plugin extends Component {
  constructor (props) {
    super(props)
    const { configurationSchema } = props

    // Don't update input with schema in edit mode!
    // It's always the same!
    this.state = {
      configurationSchema,
      uiSchema: generateUiSchema(configurationSchema)
    }
    this.configFormId = `form-config-${props.id}`
    this.testFormId = `form-test-${props.id}`
  }

  componentWillReceiveProps (nextProps) {
    // Don't update input with schema in edit mode!
    if (!this.state.edit) {
      const { configurationSchema } = nextProps

      this.setState({
        configurationSchema,
        uiSchema: generateUiSchema(configurationSchema)
      })

      if (this.refs.pluginInput) {
        // TODO: Compare values!!!
        // `|| undefined` because old configs can be null.
        this.refs.pluginInput.value = nextProps.configuration || undefined
      }
    }
  }

  _updateExpanded = () => {
    this.setState({
      expanded: !this.state.expanded
    })
  }

  _setAutoload = (event) => {
    if (this._updateAutoload) {
      return
    }

    this._updateAutoload = true

    const method = event.target.checked ? enablePluginAutoload : disablePluginAutoload

    method(this.props.id)::lastly(() => {
      this._updateAutoload = false
    })
  }

  _updateLoad = () => {
    const { props } = this

    if (!props.loaded) {
      return loadPlugin(props.id)
    }
    if (props.unloadable !== false) {
      return unloadPlugin(props.id)
    }
  }

  _saveConfiguration = async () => {
    try {
      await configurePlugin(this.props.id, this.refs.pluginInput.value)

      this.setState({
        edit: false
      })
    } catch (_) { }
  }

  _deleteConfiguration = async () => {
    try {
      await purgePluginConfiguration(this.props.id)
      this.refs.pluginInput.value = undefined
    } catch (_) { }
  }

  _edit = () => {
    this.setState({
      edit: true
    })
  }

  _cancelEdit = () => {
    this.setState({
      edit: false
    })
  }

  _applyPredefinedConfiguration = () => {
    const configName = this.refs.selectPredefinedConfiguration.value
    this.refs.pluginInput.value = this.props.configurationPresets[configName]
  }

  _test = async () => testPlugin(this.props.id, this.refs.testInput.value)

  render () {
    const {<commit_msg>feat(settings/plugins): display plugin test failures (#1754)<commit_after>import _ from 'intl'
import ActionButton from 'action-button'
import ActionToggle from 'action-toggle'
import GenericInput from 'json-schema-input'
import Icon from 'icon'
import isEmpty from 'lodash/isEmpty'
import map from 'lodash/map'
import React, { Component } from 'react'
import size from 'lodash/size'
import { addSubscriptions } from 'utils'
import { alert } from 'modal'
import { generateUiSchema } from 'xo-json-schema-input'
import { lastly } from 'promise-toolbox'
import { Row, Col } from 'grid'
import {
  configurePlugin,
  disablePluginAutoload,
  enablePluginAutoload,
  loadPlugin,
  purgePluginConfiguration,
  subscribePlugins,
  testPlugin,
  unloadPlugin
} from 'xo'

class Plugin extends Component {
  constructor (props) {
    super(props)
    const { configurationSchema } = props

    // Don't update input with schema in edit mode!
    // It's always the same!
    this.state = {
      configurationSchema,
      uiSchema: generateUiSchema(configurationSchema)
    }
    this.configFormId = `form-config-${props.id}`
    this.testFormId = `form-test-${props.id}`
  }

  componentWillReceiveProps (nextProps) {
    // Don't update input with schema in edit mode!
    if (!this.state.edit) {
      const { configurationSchema } = nextProps

      this.setState({
        configurationSchema,
        uiSchema: generateUiSchema(configurationSchema)
      })

      if (this.refs.pluginInput) {
        // TODO: Compare values!!!
        // `|| undefined` because old configs can be null.
        this.refs.pluginInput.value = nextProps.configuration || undefined
      }
    }
  }

  _updateExpanded = () => {
    this.setState({
      expanded: !this.state.expanded
    })
  }

  _setAutoload = (event) => {
    if (this._updateAutoload) {
      return
    }

    this._updateAutoload = true

    const method = event.target.checked ? enablePluginAutoload : disablePluginAutoload

    method(this.props.id)::lastly(() => {
      this._updateAutoload = false
    })
  }

  _updateLoad = () => {
    const { props } = this

    if (!props.loaded) {
      return loadPlugin(props.id)
    }
    if (props.unloadable !== false) {
      return unloadPlugin(props.id)
    }
  }

  _saveConfiguration = async () => {
    try {
      await configurePlugin(this.props.id, this.refs.pluginInput.value)

      this.setState({
        edit: false
      })
    } catch (_) { }
  }

  _deleteConfiguration = async () => {
    try {
      await purgePluginConfiguration(this.props.id)
      this.refs.pluginInput.value = undefined
    } catch (_) { }
  }

  _edit = () => {
    this.setState({
      edit: true
    })
  }

  _cancelEdit = () => {
    this.setState({
      edit: false
    })
  }

  _applyPredefinedConfiguration = () => {
    const configName = this.refs.selectPredefinedConfiguration.value
    this.refs.pluginInput.value = this.props.configurationPresets[configName]
  }

  _test = async () => {
    try {
      await testPlugin(this.props.id, this.refs.testInput.value)
    } catch (err) {
      await alert(
        'You have an error!',
        <div>
          <p>Code: {err.code}</p>
          <p>Message: {err.message}</p>
          {err.data && <pre>{JSON.stringify(err.data, null, 2)}</pre>}
        </div>
      )
      throw err
    }
  }

  render () {
    const {
{'fraction_non_alphanumeric': 0.12107904642409034, 'fraction_numerical': 0.0007841907151819323, 'mean_word_length': 2.737983587338804, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 31, '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': '4423817', 'n_tokens_mistral': 2014, 'n_tokens_neox': 1956, 'n_words': 427}