<commit_before>// Constants
import {has, assert, maybeHas, translate as t, DEFAULT_TYPE_LABELS} from '../Helpers';

import {Category} from '../Models';

// Global variables
import {Actions, store, State} from '../store';

import {AmountWell, FilteredAmountWell} from './AmountWell';
import SearchComponent from './SearchOperationList';
import CategorySelectComponent from './CategorySelectComponent';
import OperationTypeSelectComponent from './OperationTypeSelectComponent';
import T from './Translated';

import {MaybeHandleSyncError} from '../errors';

// If the length of the short label (of an operation) is smaller than this
// threshold, the raw label of the operation will be displayed in lieu of the
// short label, in the operations list.
// TODO make this a parameter in settings
const SMALL_TITLE_THRESHOLD = 4;

// Components
function ComputeAttachmentLink(op) {
    let file = op.binary.fileName || 'file';
    return `operations/${op.id}/`+file;
}

class LabelComponent extends React.Component {
    constructor(props) {
        has(props, 'operation');
        super(props);
        this.state = {
            editMode: false
        };
    }

    buttonLabel() {
        assert(false, "buttonLabel() must be implemented by the subclasses!");
    }

    dom() {
        return this.refs.customlabel.getDOMNode();
    }

    switchToEditMode() {
        this.setState({ editMode: true }, () => {
            this.dom().focus();
            // Set the cursor at the end
            this.dom().selectionStart = (this.dom().value || '').length;
        });
    }
    switchToStaticMode() {
        this.setState({ editMode: false });
    }

    onBlur() {
        let customLabel = this.dom().value;
        if (customLabel && customLabel !== this.defaultValue()) {
            if (customLabel.trim().length) {
                Actions.SetCustomLabel(this.props.operation, customLabel);
                // Be optimistic
                this.props.operation.customLabel = customLabel;
            }
        } else if (this.props.operation.customLabel && this.props.operation.customLabel.length)
        {
            Actions.SetCustomLabel(this.props.operation, '');
            // Be optimistic
            this.props.operation.customLabel = null;
        }
        this.switchToStaticMode();
    }<commit_msg>Fix customLabel deletion (customLabel is not deleted on validation, if not modified);<commit_after>// Constants
import {has, assert, maybeHas, translate as t, DEFAULT_TYPE_LABELS} from '../Helpers';

import {Category} from '../Models';

// Global variables
import {Actions, store, State} from '../store';

import {AmountWell, FilteredAmountWell} from './AmountWell';
import SearchComponent from './SearchOperationList';
import CategorySelectComponent from './CategorySelectComponent';
import OperationTypeSelectComponent from './OperationTypeSelectComponent';
import T from './Translated';

import {MaybeHandleSyncError} from '../errors';

// If the length of the short label (of an operation) is smaller than this
// threshold, the raw label of the operation will be displayed in lieu of the
// short label, in the operations list.
// TODO make this a parameter in settings
const SMALL_TITLE_THRESHOLD = 4;

// Components
function ComputeAttachmentLink(op) {
    let file = op.binary.fileName || 'file';
    return `operations/${op.id}/`+file;
}

class LabelComponent extends React.Component {
    constructor(props) {
        has(props, 'operation');
        super(props);
        this.state = {
            editMode: false
        };
    }

    buttonLabel() {
        assert(false, "buttonLabel() must be implemented by the subclasses!");
    }

    dom() {
        return this.refs.customlabel.getDOMNode();
    }

    switchToEditMode() {
        this.setState({ editMode: true }, () => {
            this.dom().focus();
            // Set the cursor at the end
            this.dom().selectionStart = (this.dom().value || '').length;
        });
    }
    switchToStaticMode() {
        this.setState({ editMode: false });
    }

    onBlur() {
        let customLabel = this.dom().value;
        if (customLabel) {
            // If the new non empty customLabel value is different from the current one, save it.
            if (customLabel.trim() !== this.defaultValue() && customLabel.trim().length) {
                Actions.SetCustomLabel(this.props.operation, customLabel);
                // Be optimistic
                this.props.operation.customLabel = customLabel;
            }
        } else if (this.props.operation.customLabel && this.props.operation.customLabel.length) {
                // If the new customLabel value is empty and there was already one, unset it.
                Actions.SetCustomLabel(this.props.operation, '');
                // Be optimistic
                this.props.operation.customLabel = null;
            }
        this.switchToStaticMode();
    }
{'fraction_non_alphanumeric': 0.12009754115017272, 'fraction_numerical': 0.000406421459053038, 'mean_word_length': 2.55635838150289, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '4803854', 'n_tokens_mistral': 1299, 'n_tokens_neox': 1273, 'n_words': 378}