<commit_before>import * as React from 'react';
import {IArticle, IAttachment, IAttachmentsWidgetProps, IAttachmentsWrapperProps} from 'superdesk-api';
import {isLocked, isLockedInCurrentSession} from 'core/get-superdesk-api-implementation';
import {appConfig} from 'appConfig';
import {notify} from 'core/notify/notify';
import {gettext, gettextCatalog} from 'core/utils';
import {filesize} from 'core/ui/ui';
import {CC} from 'core/ui/configurable-ui-components';
import {AttachmentsWidgetComponent} from './AttachmentsWidgetComponent';
import {withAttachments} from './AttachmentsWrapper';
import {dispatchCustomEvent} from 'core/notification/notification';

interface IProps extends IAttachmentsWrapperProps {
    updateItem(updates: Partial<IArticle>): void;
    readOnly: boolean;
    isWidget: boolean;
}

class AttachmentsWidgetWrapper extends React.PureComponent<IProps> {
    constructor(props: IProps) {
        super(props);

        this.addAttachments = this.addAttachments.bind(this);
        this.removeAttachment = this.removeAttachment.bind(this);
        this.updateAttachment = this.updateAttachment.bind(this);
        this.isUploadValid = this.isUploadValid.bind(this);
    }

    addAttachments(newAttachments: Array<IAttachment>) {
        const attachments = this.props.attachments.concat(newAttachments);

        if (this.props.updateItem != null) {
            this.props.updateItem({
                attachments: attachments.map((attachment) => ({attachment: attachment._id})),
            });
        }

        dispatchCustomEvent('attachmentsAdded', newAttachments);
    }

    removeAttachment(attachment: IAttachment) {
        const attachments = this.props.attachments.filter(
            (_attachment) => _attachment._id !== attachment._id,
        );

        if (this.props.updateItem != null) {
            this.props.updateItem({
                attachments: attachments.map((_attachment) => ({attachment: _attachment._id})),
            });
        }

        dispatchCustomEvent('attachmentRemoved', attachment);
    }

    updateAttachment(attachment: IAttachment) {
        dispatchCustomEvent('attachmentUpdated', attachment);
    }

    isUploadValid(files: Array<File>) {
        if (files.length === 0 || !isLocked(this.props.item)) {
            return false;
        } else if (files.length + this.props.attachments.length > appConfig.attachments_max_files) {
            notify.error(gettextCatalog.getPlural(
                appConfig.attachments_max_files,
                'Too many files selected. Only 1 file is allowed',
                'Too many files selected. Only {{count}} files are allowed',
                {count: appConfig.attachments_max_files},
            ));
            return false;
        }

        const filenames = files.filter((file) => file.size > appConfig.attachments_max_size)
            .map((file) => file.name);

        if (filenames.length > 0) {
            notify.error(gettext(
                'Sorry, but some files "{{filenames}}" are bigger than limit ({{limit}})',
                {
                    filenames: filenames.join(', '),
                    limit: filesize(appConfig.attachments_max_size),
                },
            ));
            return false;
        }

        return true;
    }

    render() {
        const Widget = CC.AuthoringAttachmentsWidget != null ?
            CC.AuthoringAttachmentsWidget :
            AttachmentsWidgetComponent;

        return (
            <Widget
                {...this.props}
                editable={!!this.props.item._editable}
                isLocked={isLocked(this.props.item)}
                isLockedByMe={isLockedInCurrentSession(this.props.item)}
                isUploadValid={this.isUploadValid}
                addAttachments={this.addAttachments}<commit_msg>fix(imports): Use sdApi.article.isLocked in AttachmentsWidget<commit_after>import * as React from 'react';
import {IArticle, IAttachment, IAttachmentsWidgetProps, IAttachmentsWrapperProps} from 'superdesk-api';
import {isLockedInCurrentSession} from 'core/get-superdesk-api-implementation';
import {sdApi} from 'api';
import {appConfig} from 'appConfig';
import {notify} from 'core/notify/notify';
import {gettext, gettextCatalog} from 'core/utils';
import {filesize} from 'core/ui/ui';
import {CC} from 'core/ui/configurable-ui-components';
import {AttachmentsWidgetComponent} from './AttachmentsWidgetComponent';
import {withAttachments} from './AttachmentsWrapper';
import {dispatchCustomEvent} from 'core/notification/notification';

interface IProps extends IAttachmentsWrapperProps {
    updateItem(updates: Partial<IArticle>): void;
    readOnly: boolean;
    isWidget: boolean;
}

class AttachmentsWidgetWrapper extends React.PureComponent<IProps> {
    constructor(props: IProps) {
        super(props);

        this.addAttachments = this.addAttachments.bind(this);
        this.removeAttachment = this.removeAttachment.bind(this);
        this.updateAttachment = this.updateAttachment.bind(this);
        this.isUploadValid = this.isUploadValid.bind(this);
    }

    addAttachments(newAttachments: Array<IAttachment>) {
        const attachments = this.props.attachments.concat(newAttachments);

        if (this.props.updateItem != null) {
            this.props.updateItem({
                attachments: attachments.map((attachment) => ({attachment: attachment._id})),
            });
        }

        dispatchCustomEvent('attachmentsAdded', newAttachments);
    }

    removeAttachment(attachment: IAttachment) {
        const attachments = this.props.attachments.filter(
            (_attachment) => _attachment._id !== attachment._id,
        );

        if (this.props.updateItem != null) {
            this.props.updateItem({
                attachments: attachments.map((_attachment) => ({attachment: _attachment._id})),
            });
        }

        dispatchCustomEvent('attachmentRemoved', attachment);
    }

    updateAttachment(attachment: IAttachment) {
        dispatchCustomEvent('attachmentUpdated', attachment);
    }

    isUploadValid(files: Array<File>) {
        if (files.length === 0 || !sdApi.article.isLocked(this.props.item)) {
            return false;
        } else if (files.length + this.props.attachments.length > appConfig.attachments_max_files) {
            notify.error(gettextCatalog.getPlural(
                appConfig.attachments_max_files,
                'Too many files selected. Only 1 file is allowed',
                'Too many files selected. Only {{count}} files are allowed',
                {count: appConfig.attachments_max_files},
            ));
            return false;
        }

        const filenames = files.filter((file) => file.size > appConfig.attachments_max_size)
            .map((file) => file.name);

        if (filenames.length > 0) {
            notify.error(gettext(
                'Sorry, but some files "{{filenames}}" are bigger than limit ({{limit}})',
                {
                    filenames: filenames.join(', '),
                    limit: filesize(appConfig.attachments_max_size),
                },
            ));
            return false;
        }

        return true;
    }

    render() {
        const Widget = CC.AuthoringAttachmentsWidget != null ?
            CC.AuthoringAttachmentsWidget :
            AttachmentsWidgetComponent;

        return (
            <Widget
                {...this.props}
                editable={!!this.props.item._editable}
                isLocked={sdApi.article.isLocked(this.props.item)}
                isLockedByMe={isLockedInCurrentSession(this.props.item)}
                isUploadValid={this.isUploadValid}
                addAttachments={this.addAttachments}
{'fraction_non_alphanumeric': 0.11268878275461469, 'fraction_numerical': 0.0007744933522653931, 'mean_word_length': 2.567219152854512, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 27, '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': '6537714', 'n_tokens_mistral': 2131, 'n_tokens_neox': 2091, 'n_words': 394}