chuckya/app/javascript/flavours/glitch/features/ui/components/gif_modal.jsx

110 lines
3.7 KiB
React
Raw Normal View History

2021-01-19 01:05:19 +09:00
import PropTypes from 'prop-types';
2023-06-06 14:23:12 +09:00
import { defineMessages, injectIntl } from 'react-intl';
import ImmutablePropTypes from 'react-immutable-proptypes';
2021-01-19 01:05:19 +09:00
import ImmutablePureComponent from 'react-immutable-pure-component';
import { connect } from 'react-redux';
2023-06-06 14:23:12 +09:00
2024-01-15 08:22:38 +09:00
import { ReactComponent as CloseIcon } from '@material-symbols/svg-600/outlined/close.svg';
2023-06-06 14:23:12 +09:00
import Tenor from 'react-tenor';
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';
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.' },
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 => ({
2023-06-06 14:23:12 +09:00
/**
* Set options in the redux store
2023-12-19 10:55:34 +09:00
* @param {Object} opts
2023-06-06 14:23:12 +09:00
*/
2022-04-17 09:04:23 +09:00
setOpt: (opts) => dispatch(tenorSet(opts)),
2023-06-06 14:23:12 +09:00
/**
* Submit GIF for upload
2023-12-19 10:55:34 +09:00
* @param {File} file
* @param {string} alt
2023-06-06 14:23:12 +09:00
*/
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 = {
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 () {
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'>
2024-01-15 08:22:38 +09:00
<IconButton title={intl.formatMessage(messages.close)} icon='close' iconComponent={CloseIcon} 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));