# All files / composer/components MediaAttachment.jsx

0% Statements 0/25
0% Branches 0/36
0% Functions 0/10
0% Lines 0/25

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191

     /**
     * Component that displays media suggestions and attachments
     */

    import React from 'react';
    import PropTypes from 'prop-types';
    import { FileUploadFormatsConfigs, MediaTypes, UploadTypes } from '../AppConstants';
    import ComposerActionCreators from '../action-creators/ComposerActionCreators';
    import MediaAttachmentThumbnail from '../components/MediaAttachmentThumbnail';
    import MediaAttachmentEditor from '../components/MediaAttachmentEditor';
    import UploadZone from '../components/UploadZone';
    import CircularUploadIndicator from '../components/progress-indicators/CircularUploadIndicator';
    import Dropdown, { DropdownTrigger, DropdownContent } from '../components/Dropdown';
    import styles from './css/MediaAttachment.css';

    class MediaAttachment extends React.Component {
      static propTypes = {
        draft: PropTypes.object.isRequired,
        draftId: PropTypes.string.isRequired,
        images: PropTypes.array.isRequired,
        video: PropTypes.object,
        gif: PropTypes.object,
        tempImage: PropTypes.string,
        maxAttachableImagesCount: PropTypes.number.isRequired,
        filesUploadProgress: PropTypes.instanceOf(Map),
        service: PropTypes.object,
        visibleNotifications: PropTypes.array.isRequired,
        className: PropTypes.string,
        usesImageFirstLayout: PropTypes.bool,
        composerPosition: PropTypes.object,
      };

      static defaultProps = {
        composerPosition: null,
      };

      hasImagesAttached = () => this.props.images.length > 0;
      hasVideoAttached = () => this.props.video !== null;
      hasGifAttached = () => this.props.gif !== null;

      collapseAttachedMediaEditor = () => {
        ComposerActionCreators.updateDraftAttachedMediaEditingPayload(this.props.draftId, null);
      }

      expandAttachedMediaEditor = () => {
        ComposerActionCreators.updateDraftAttachedMediaEditingPayload(this.props.draftId, this.props.video);
      }

      onVideoEditButtonClick = () => {
        const isAlreadyEditingMedia = this.props.draft.attachedMediaEditingPayload !== null;
        const payload = isAlreadyEditingMedia ? null : this.props.video;
        ComposerActionCreators.updateDraftAttachedMediaEditingPayload(this.props.draftId, payload);
      };

      render() {
        const {
          images, video, gif, tempImage, draftId, maxAttachableImagesCount, filesUploadProgress,
          service, className, usesImageFirstLayout, composerPosition, draft,
        } = this.props;

        const shouldDisplayUploadNewButton = (
          maxAttachableImagesCount > images.length &&
          video === null &&
          gif === null
        );

        const areUploadsInProgress = filesUploadProgress.size > 0;
        const totalUploadsProgress = areUploadsInProgress &&
          Array.from(filesUploadProgress.values()).reduce((a, b) => a + b) / filesUploadProgress.size;

        const uploadNewButtonTooltipCopy = 'Upload image or video';

        const uploadNewButtonUIClassName = [
          areUploadsInProgress ? styles.uploadNewButtonUIIsUploading :
          tempImage ? styles.uploadNewButtonUIWithTempImage : styles.uploadNewButtonUI,
          usesImageFirstLayout ? styles.imageFirstUploadButtonUI : '',
          'bi bi-add-media',
        ].join(' ');

        const uploadFormatsConfig = new Map(FileUploadFormatsConfigs.MEDIA); // Clone config

        service.unavailableMediaAttachmentTypes.forEach((mediaType) => {
          uploadFormatsConfig.delete(mediaType);
        });

        const thumbnailClassName = usesImageFirstLayout ? styles.imageFirstThumbnail : styles.thumbnail;

        const mediaAttachmentClassNames = [
          styles.mediaAttachment,
          className,
        ].join(' ');

        const uploadZoneClassNames = {
          uploadZone: styles.uploadZone,
          uploadZoneActive: [styles.uploadButtonUIActiveDrop, 'bi bi-add-media'].join(' '),
          uploadZoneDisabled: styles.uploadButtonUIDisabled,
        };

        const editButtonClass = `${styles.editButton} bi bi-edit`;
        const showTwitterImageDescription = draftId === 'twitter';

        return (
          <div className={mediaAttachmentClassNames}>
            {this.hasImagesAttached() &&
              images.map((image) => (
                <MediaAttachmentThumbnail
                  draftId={draftId}
                  className={thumbnailClassName}
                  key={image.url}
                  media={image}
                  showTwitterImageDescription={showTwitterImageDescription}
                  composerPosition={composerPosition}
                />
              ))}

            {this.hasVideoAttached() && draft.service.canEditVideoAttachment && (
              <Dropdown
                isDropdownExpanded={draft.attachedMediaEditingPayload !== null}
                onHide={this.collapseAttachedMediaEditor}
                onShow={this.expandAttachedMediaEditor}
                className={styles.editButtonContainer}
              >
                <DropdownTrigger
                  className={editButtonClass}
                  data-tip="Edit video details"
                  aria-label="Click to edit video details"
                />
                <DropdownContent className={styles.editDropdownContent}>
                  {draft.attachedMediaEditingPayload !== null &&
                    <MediaAttachmentEditor draft={draft} onSubmit={this.collapseAttachedMediaEditor} />}
                </DropdownContent>
              </Dropdown>
            )}

            {this.hasVideoAttached() &&
              <MediaAttachmentThumbnail
                draftId={draftId}
                className={thumbnailClassName}
                key={video.thumbnail}
                media={video}
                showTwitterImageDescription={showTwitterImageDescription}
                composerPosition={composerPosition}
                canEditVideoAttachment={draft.service.canEditVideoAttachment}
              />}

            {this.hasGifAttached() &&
              <MediaAttachmentThumbnail
                draftId={draftId}
                className={thumbnailClassName}
                key={gif.url}
                media={gif}
                showTwitterImageDescription={showTwitterImageDescription}
                composerPosition={composerPosition}
              />}

            {shouldDisplayUploadNewButton &&
              <div
                className={uploadNewButtonUIClassName}
                data-tip={uploadNewButtonTooltipCopy}
                data-tip-disable={areUploadsInProgress}
              >
                {tempImage && !areUploadsInProgress &&
                  <div className={styles.tempImageContainer}>
                    <img alt="" src={tempImage} className={styles.tempImage} />
                  </div>}

                <UploadZone
                  classNames={uploadZoneClassNames}
                  draftId={draftId}
                  uploadFormatsConfig={uploadFormatsConfig}
                  service={this.props.service}
                  visibleNotifications={this.props.visibleNotifications}
                  uploadType={UploadTypes.MEDIA}
                  multiple={maxAttachableImagesCount > 1}
                  disabled={areUploadsInProgress}
                />

                {areUploadsInProgress &&
                  <CircularUploadIndicator size={54} progress={totalUploadsProgress} showText />}

                {areUploadsInProgress && filesUploadProgress.size > 1 &&
                  <span className={styles.activeUploadsCount}>{filesUploadProgress.size}</span>}
              </div>}
          </div>

        );
      }
    }

    export default MediaAttachment;