<commit_before>import PouchDB from "pouchdb";
import React from "react";
import Input from "react-bootstrap/lib/Input";

import Config from "../../../controller/Config";

export default class Url extends React.Component
{
    constructor( props )
    {
        super( props );

        this.state = {
            url: undefined
          , db: undefined
        };
    }

    componentWillMount()
    {
    }

    componentWillUnmount()
    {
    }

    handleChange()
    {
        var url = this.refs.url.getValue();
        this.setState({
            url: url
          , db: { info: this.getInfo( url ) }
        });
    }

    getInfo( url )
    {
        var db = new PouchDB( url, { skipSetup: true} );
        return (db
            .info()
            .then( this.setInfo.bind( this ) )
            .catch( this.setError.bind( this ) )
        );
    }

    setInfo( info )
    {
        this.setState({
            db: { info, error: undefined }
        });
    }

    setError( error )
    {
        this.setState({
            db: { info: undefined, error }
        });
    }

    validate( db )
    {
        var bsStyle = "error", help;
        console.log( "db", db );
        if ( db )
        {
            var { info, error } = db || {};
            if ( info instanceof Promise )
            {
                bsStyle = "warning";
                help = "checking...";
            }
            else if ( info )
            {
                bsStyle = "success";
                help = this.renderInfo( info );
            }
            else if ( error )
            {
                bsStyle = "error";
                help = this.renderError( error );
            }
        }

        return { bsStyle, help };
    }

    /**
     * @param {Object} info
     * @param {string} info.db_name
     * @param {number} info.doc_count
     * @returns {string}
     */
    renderInfo( info )
    {
        return `name: ${ info.db_name } document count: ${ info.doc_count }`;
    }

    /**
     * @param {Object} error
     * @param {number} error.status
     * @returns {string}
     */
    renderError( error )
    {
        return `status: ${ error.status } message: ${ error.message }`;
    }

    render()
    {
        var validation = this.validate( this.state.db );

        return React.createElement(
            Input
          , {
                type: "text"
              , value: this.state.url
              , placeholder: "e.g. local-db-name or http://host:5984/remote-db-name"
              , label: "Url"
              , ref: "url"
              , bsStyle: validation.bsStyle
              , help: validation.help
              , hasFeedback: true
              , onChange: this.handleChange.bind( this )
            }
        );
    }
}
Url.propTypes = {
    config: React.PropTypes.instanceOf( Config ).isRequired
  , configPath: React.PropTypes.string.isRequired
};

Url.defaultProps = {
    configPath: "db.url"
};
<commit_msg> - only check for db if input is an url, to prevent PouchDB from generating lots of local DBs<commit_after>import PouchDB from "pouchdb";
import React from "react";
import Input from "react-bootstrap/lib/Input";

import Config from "../../../controller/Config";

export default class Url extends React.Component
{
    constructor( props )
    {
        super( props );

        this.state = {
            url: undefined
          , db: undefined
        };
    }

    componentWillMount()
    {
    }

    componentWillUnmount()
    {
    }

    handleChange()
    {
        var url = this.refs.url.getValue();
        this.setState({
            url: url
          , db: { info: this.getInfo( url ) }
        });
    }

    isUrl( url )
    {
        try {
            return !!(new URL( url ));
        }
        catch(e) {
            return false;
        }
    }

    getInfo( url )
    {
        if ( !this.isUrl( url ) )
        {
            return true;
        }

        var db = new PouchDB( url, { skipSetup: true} );

        return (db
            .info()
            .then( this.setInfo.bind( this ) )
            .catch( this.setError.bind( this ) )
        );
    }

    setInfo()
    {
        this.setState({
            db: { info: true, error: undefined }
        });
    }

    setError( error )
    {
        this.setState({
            db: { info: undefined, error }
        });
    }

    validate( db )
    {
        var bsStyle = "error", help;

        if ( db )
        {
            var { info, error } = db;

            if ( info === true )
            {
                bsStyle = "success";
            }
            else if ( error )
            {
                bsStyle = "error";
                help = this.renderError( error );
            }
        }

        return { bsStyle, help };
    }

    /**
     * @param {Object} error
     * @param {number} error.status
     * @returns {string}
     */
    renderError( error )
    {
        return `status: ${ error.status } message: ${ error.message }`;
    }

    render()
    {
        var validation = this.validate( this.state.db );

        return React.createElement(
            Input
          , {
                type: "text"
              , value: this.state.url
              , placeholder: "e.g. local-db-name or http://host:5984/remote-db-name"
              , label: "Url"
              , ref: "url"
              , bsStyle: validation.bsStyle
              , help: validation.help
              , hasFeedback: true
              , onChange: this.handleChange.bind( this )
            }
        );
    }
}
Url.propTypes = {
    config: React.PropTypes.instanceOf( Config ).isRequired
  , configPath: React.PropTypes.string.isRequired
};

Url.defaultProps = {
    configPath: "db.url"
};

{'fraction_non_alphanumeric': 0.12476107732406604, 'fraction_numerical': 0.0013900955690703736, 'mean_word_length': 1.2923138191955397, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '661555', 'n_tokens_mistral': 1705, 'n_tokens_neox': 1655, 'n_words': 384}