2021-01-19 01:05:19 +09:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import ImmutablePureComponent from 'react-immutable-pure-component';
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import ImmutablePropTypes from 'react-immutable-proptypes';
|
2022-04-17 09:04:23 +09:00
|
|
|
import { tenorSet, uploadCompose } from 'flavours/glitch/actions/compose';
|
2023-05-19 03:37:02 +09:00
|
|
|
import { IconButton } from 'flavours/glitch/components/icon_button';
|
2022-04-17 09:04:23 +09:00
|
|
|
import Tenor from 'react-tenor';
|
2021-01-20 01:51:44 +09:00
|
|
|
import { defineMessages, injectIntl } from 'react-intl';
|
|
|
|
|
|
|
|
const messages = defineMessages({
|
2022-04-17 09:04:23 +09:00
|
|
|
search: { id: 'tenor.search', defaultMessage: 'Search for GIFs' },
|
|
|
|
error: { id: 'tenor.error', defaultMessage: 'Oops! Something went wrong. Please, try again.' },
|
|
|
|
loading: { id: 'tenor.loading', defaultMessage: 'Loading...' },
|
|
|
|
nomatches: { id: 'tenor.nomatches', defaultMessage: 'No matches found.' },
|
2021-01-20 01:51:44 +09:00
|
|
|
close: { id: 'settings.close', defaultMessage: 'Close' },
|
|
|
|
});
|
2021-01-19 01:05:19 +09:00
|
|
|
|
|
|
|
// Utility for converting base64 image to binary for upload
|
|
|
|
// https://stackoverflow.com/questions/35940290/how-to-convert-base64-string-to-javascript-file-object-like-as-from-file-input-f
|
|
|
|
function dataURLtoFile(dataurl, filename) {
|
|
|
|
let arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
|
|
|
|
bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
|
|
|
|
while(n--){
|
|
|
|
u8arr[n] = bstr.charCodeAt(n);
|
|
|
|
}
|
|
|
|
return new File([u8arr], filename, { type: mime });
|
|
|
|
}
|
|
|
|
|
|
|
|
const mapStateToProps = state => ({
|
2022-04-17 09:04:23 +09:00
|
|
|
options: state.getIn(['compose', 'tenor']),
|
2021-01-19 01:05:19 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
const mapDispatchToProps = dispatch => ({
|
|
|
|
/** Set options in the redux store */
|
2022-04-17 09:04:23 +09:00
|
|
|
setOpt: (opts) => dispatch(tenorSet(opts)),
|
2021-01-19 01:05:19 +09:00
|
|
|
/** Submit GIF for upload */
|
2022-06-07 21:14:54 +09:00
|
|
|
submit: (file, alt) => dispatch(uploadCompose([file], alt)),
|
2021-01-19 01:05:19 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
class GIFModal extends ImmutablePureComponent {
|
|
|
|
|
|
|
|
static propTypes = {
|
2021-01-20 01:51:44 +09:00
|
|
|
intl: PropTypes.object.isRequired,
|
2021-01-19 01:05:19 +09:00
|
|
|
options: ImmutablePropTypes.map,
|
|
|
|
onClose: PropTypes.func.isRequired,
|
|
|
|
setOpt: PropTypes.func.isRequired,
|
|
|
|
submit: PropTypes.func.isRequired,
|
|
|
|
};
|
|
|
|
|
2022-04-17 09:04:23 +09:00
|
|
|
onDoneButton = (result) => {
|
|
|
|
const url = result.media[0].mp4.url;
|
2022-06-07 21:14:54 +09:00
|
|
|
const alt = result.content_description;
|
2021-01-19 01:05:19 +09:00
|
|
|
var modal = this;
|
2022-04-17 09:04:23 +09:00
|
|
|
// eslint-disable-next-line promise/catch-or-return
|
2021-01-19 01:05:19 +09:00
|
|
|
fetch(url).then(function(response) {
|
|
|
|
return response.blob();
|
|
|
|
}).then(function(blob) {
|
|
|
|
const reader = new FileReader();
|
2023-03-29 15:36:14 +09:00
|
|
|
reader.readAsDataURL(blob);
|
2021-01-19 01:05:19 +09:00
|
|
|
reader.onloadend = function() {
|
|
|
|
var dataUrl = reader.result;
|
2022-04-17 09:04:23 +09:00
|
|
|
const file = dataURLtoFile(dataUrl, 'tenor.mp4');
|
2022-06-07 21:14:54 +09:00
|
|
|
modal.props.submit(file, alt);
|
2021-01-19 01:05:19 +09:00
|
|
|
modal.props.onClose(); // close dialog
|
|
|
|
};
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
render () {
|
|
|
|
|
2021-01-20 01:51:44 +09:00
|
|
|
const { intl } = this.props;
|
|
|
|
|
2021-01-19 01:05:19 +09:00
|
|
|
return (
|
2022-04-17 09:04:23 +09:00
|
|
|
<div className='modal-root__modal tenor-modal'>
|
|
|
|
<div className='tenor-modal__container'>
|
2023-01-22 21:16:28 +09:00
|
|
|
<IconButton title={intl.formatMessage(messages.close)} icon='close' size='16' onClick={this.props.onClose} style={{ float: 'right' }} />
|
2022-04-17 10:05:39 +09:00
|
|
|
<Tenor
|
|
|
|
token='FJBKNQSVF2DD'
|
|
|
|
// eslint-disable-next-line react/jsx-no-bind
|
|
|
|
onSelect={result => this.onDoneButton(result)}
|
|
|
|
autoFocus='true'
|
|
|
|
searchPlaceholder={intl.formatMessage(messages.search)}
|
|
|
|
messageError={intl.formatMessage(messages.error)}
|
|
|
|
messageLoading={intl.formatMessage(messages.loading)}
|
|
|
|
messageNoMatches={intl.formatMessage(messages.nomatches)}
|
2022-04-25 19:23:40 +09:00
|
|
|
contentFilter='off'
|
2022-04-17 10:05:39 +09:00
|
|
|
/>
|
2022-04-17 09:04:23 +09:00
|
|
|
<br /><img src='/tenor.svg' alt='Tenor logo' />
|
2021-01-19 01:05:19 +09:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2023-03-29 15:36:14 +09:00
|
|
|
|
|
|
|
|
2023-05-03 12:43:11 +09:00
|
|
|
export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(GIFModal));
|