about summary refs log tree commit diff
path: root/app/javascript/glitch/components/compose/attach_options/index.js
blob: 4340972f01e5d73abcc4ac5acc081514ab1833b7 (plain) (blame)
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
//  Package imports  //
import React from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { injectIntl, defineMessages } from 'react-intl';

//  Our imports  //
import ComposeDropdown from '../dropdown/index';
import { uploadCompose } from '../../../../mastodon/actions/compose';
import ImmutablePropTypes from 'react-immutable-proptypes';
import ImmutablePureComponent from 'react-immutable-pure-component';
import { openModal } from '../../../../mastodon/actions/modal';

//  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

const messages = defineMessages({
  upload :
    { id: 'compose.attach.upload', defaultMessage: 'Upload a file' },
  doodle :
    { id: 'compose.attach.doodle', defaultMessage: 'Draw something' },
  attach :
    { id: 'compose.attach', defaultMessage: 'Attach...' },
});

const mapStateToProps = state => ({
  // This horrible expression is copied from vanilla upload_button_container
  disabled: state.getIn(['compose', 'is_uploading']) || (state.getIn(['compose', 'media_attachments']).size > 3 || state.getIn(['compose', 'media_attachments']).some(m => m.get('type') === 'video')),
  resetFileKey: state.getIn(['compose', 'resetFileKey']),
  acceptContentTypes: state.getIn(['media_attachments', 'accept_content_types']),
});

const mapDispatchToProps = dispatch => ({
  onSelectFile (files) {
    dispatch(uploadCompose(files));
  },
  onOpenDoodle () {
    dispatch(openModal('DOODLE', { noEsc: true }));
  },
});

@injectIntl
@connect(mapStateToProps, mapDispatchToProps)
export default class ComposeAttachOptions extends ImmutablePureComponent {

  static propTypes = {
    intl     : PropTypes.object.isRequired,
    resetFileKey: PropTypes.number,
    acceptContentTypes: ImmutablePropTypes.listOf(PropTypes.string).isRequired,
    disabled: PropTypes.bool,
    onSelectFile: PropTypes.func.isRequired,
    onOpenDoodle: PropTypes.func.isRequired,
  };

  handleItemClick = bt => {
    if (bt === 'upload') {
      this.fileElement.click();
    }

    if (bt === 'doodle') {
      this.props.onOpenDoodle();
    }

    this.dropdown.setState({ open: false });
  };

  handleFileChange = (e) => {
    if (e.target.files.length > 0) {
      this.props.onSelectFile(e.target.files);
    }
  }

  setFileRef = (c) => {
    this.fileElement = c;
  }

  setDropdownRef = (c) => {
    this.dropdown = c;
  }

  render () {
    const { intl, resetFileKey, disabled, acceptContentTypes } = this.props;

    const options = [
      { icon: 'cloud-upload', text: messages.upload, name: 'upload' },
      { icon: 'paint-brush', text: messages.doodle, name: 'doodle' },
    ];

    const optionElems = options.map((item) => {
      const hdl = () => this.handleItemClick(item.name);
      return (
        <div
          role='button'
          tabIndex='0'
          key={item.name}
          onClick={hdl}
          className='privacy-dropdown__option'
        >
          <div className='privacy-dropdown__option__icon'>
            <i className={`fa fa-fw fa-${item.icon}`} />
          </div>

          <div className='privacy-dropdown__option__content'>
            <strong>{intl.formatMessage(item.text)}</strong>
          </div>
        </div>
      );
    });

    return (
      <div>
        <ComposeDropdown
          title={intl.formatMessage(messages.attach)}
          icon='paperclip'
          disabled={disabled}
          ref={this.setDropdownRef}
        >
          {optionElems}
        </ComposeDropdown>
        <input
          key={resetFileKey}
          ref={this.setFileRef}
          type='file'
          multiple={false}
          accept={acceptContentTypes.toArray().join(',')}
          onChange={this.handleFileChange}
          disabled={disabled}
          style={{ display: 'none' }}
        />
      </div>
    );
  }

}