2020-03-27 13:59:38 -07:00
|
|
|
import React from 'react';
|
|
|
|
import IconButton from '../../../components/icon_button';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { defineMessages, injectIntl } from 'react-intl';
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import ImmutablePureComponent from 'react-immutable-pure-component';
|
|
|
|
import ImmutablePropTypes from 'react-immutable-proptypes';
|
|
|
|
|
|
|
|
const messages = defineMessages({
|
2020-06-28 18:51:10 -07:00
|
|
|
upload: { id: 'upload_button.label', defaultMessage: 'Add media attachment' },
|
2020-03-27 13:59:38 -07:00
|
|
|
});
|
|
|
|
|
2021-10-31 14:14:09 -07:00
|
|
|
const onlyImages = types => {
|
|
|
|
return Boolean(types && types.every(type => type.startsWith('image/')));
|
|
|
|
};
|
|
|
|
|
2020-03-27 13:59:38 -07:00
|
|
|
const makeMapStateToProps = () => {
|
|
|
|
const mapStateToProps = state => ({
|
2021-10-31 14:14:09 -07:00
|
|
|
attachmentTypes: state.getIn(['instance', 'configuration', 'media_attachments', 'supported_mime_types']),
|
2020-03-27 13:59:38 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
return mapStateToProps;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default @connect(makeMapStateToProps)
|
|
|
|
@injectIntl
|
|
|
|
class UploadButton extends ImmutablePureComponent {
|
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
disabled: PropTypes.bool,
|
|
|
|
unavailable: PropTypes.bool,
|
|
|
|
onSelectFile: PropTypes.func.isRequired,
|
|
|
|
style: PropTypes.object,
|
|
|
|
resetFileKey: PropTypes.number,
|
2021-10-31 14:14:09 -07:00
|
|
|
attachmentTypes: ImmutablePropTypes.listOf(PropTypes.string),
|
2020-03-27 13:59:38 -07:00
|
|
|
intl: PropTypes.object.isRequired,
|
|
|
|
};
|
|
|
|
|
|
|
|
handleChange = (e) => {
|
|
|
|
if (e.target.files.length > 0) {
|
|
|
|
this.props.onSelectFile(e.target.files);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleClick = () => {
|
|
|
|
this.fileElement.click();
|
|
|
|
}
|
|
|
|
|
|
|
|
setRef = (c) => {
|
|
|
|
this.fileElement = c;
|
|
|
|
}
|
|
|
|
|
2020-04-14 14:47:35 -07:00
|
|
|
render() {
|
2021-10-31 14:14:09 -07:00
|
|
|
const { intl, resetFileKey, attachmentTypes, unavailable, disabled } = this.props;
|
2020-03-27 13:59:38 -07:00
|
|
|
|
|
|
|
if (unavailable) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-10-31 14:14:09 -07:00
|
|
|
const src = onlyImages(attachmentTypes)
|
|
|
|
? require('@tabler/icons/icons/photo.svg')
|
|
|
|
: require('@tabler/icons/icons/paperclip.svg');
|
|
|
|
|
2020-03-27 13:59:38 -07:00
|
|
|
return (
|
|
|
|
<div className='compose-form__upload-button'>
|
2021-09-22 12:38:48 -07:00
|
|
|
<IconButton
|
|
|
|
className='compose-form__upload-button-icon'
|
2021-10-31 14:14:09 -07:00
|
|
|
src={src}
|
2021-09-22 12:38:48 -07:00
|
|
|
title={intl.formatMessage(messages.upload)}
|
|
|
|
disabled={disabled}
|
|
|
|
onClick={this.handleClick}
|
|
|
|
/>
|
2020-03-27 13:59:38 -07:00
|
|
|
<label>
|
|
|
|
<span style={{ display: 'none' }}>{intl.formatMessage(messages.upload)}</span>
|
|
|
|
<input
|
|
|
|
key={resetFileKey}
|
|
|
|
ref={this.setRef}
|
|
|
|
type='file'
|
|
|
|
multiple
|
2021-10-31 14:14:09 -07:00
|
|
|
accept={attachmentTypes && attachmentTypes.toArray().join(',')}
|
2020-03-27 13:59:38 -07:00
|
|
|
onChange={this.handleChange}
|
|
|
|
disabled={disabled}
|
|
|
|
style={{ display: 'none' }}
|
|
|
|
/>
|
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|