lbry-desktop/src/ui/component/selectThumbnail/view.jsx

182 lines
5.9 KiB
React
Raw Normal View History

2018-04-02 18:39:00 +02:00
// @flow
import * as MODALS from 'constants/modal_types';
import { THUMBNAIL_STATUSES } from 'lbry-redux';
2018-07-18 17:46:21 +02:00
import * as React from 'react';
2019-05-21 23:18:11 +02:00
import getMediaType from 'util/get-media-type';
2018-07-18 17:46:21 +02:00
import { FormField } from 'component/common/form';
2018-04-02 18:39:00 +02:00
import FileSelector from 'component/common/file-selector';
2018-06-08 06:05:45 +02:00
import Button from 'component/button';
2019-03-05 05:46:57 +01:00
import ThumbnailMissingImage from './thumbnail-missing.png';
import ThumbnailBrokenImage from './thumbnail-broken.png';
2018-04-02 18:39:00 +02:00
type Props = {
2019-05-21 23:18:11 +02:00
filePath: ?string,
2018-04-02 18:39:00 +02:00
thumbnail: ?string,
formDisabled: boolean,
uploadThumbnailStatus: string,
2018-06-08 06:05:45 +02:00
thumbnailPath: ?string,
openModal: (id: string, {}) => void,
2018-04-02 18:39:00 +02:00
updatePublishForm: ({}) => void,
2018-06-08 06:05:45 +02:00
resetThumbnailStatus: () => void,
2018-04-02 18:39:00 +02:00
};
2018-07-17 19:43:43 +02:00
type State = {
2018-07-18 17:46:21 +02:00
thumbnailError: boolean,
};
2018-07-17 19:43:43 +02:00
2019-03-05 05:46:57 +01:00
const filters = [
{
name: __('Thumbnail Image'),
extensions: ['png', 'jpg', 'jpeg', 'gif'],
},
];
2018-07-17 19:43:43 +02:00
class SelectThumbnail extends React.PureComponent<Props, State> {
constructor() {
super();
2018-07-18 17:46:21 +02:00
2018-07-17 19:43:43 +02:00
this.state = {
2018-07-18 17:46:21 +02:00
thumbnailError: false,
2018-07-17 19:43:43 +02:00
};
2018-07-18 17:46:21 +02:00
2018-07-17 19:43:43 +02:00
(this: any).handleThumbnailChange = this.handleThumbnailChange.bind(this);
}
2018-07-18 17:46:21 +02:00
handleThumbnailChange(e: SyntheticInputEvent<*>) {
2018-07-17 19:43:43 +02:00
const { updatePublishForm } = this.props;
const newThumbnail = e.target.value.replace(' ', '');
2018-07-18 17:46:21 +02:00
2018-07-17 19:43:43 +02:00
updatePublishForm({ thumbnail: newThumbnail });
2019-03-05 05:46:57 +01:00
this.setState({ thumbnailError: false });
2018-07-17 19:43:43 +02:00
}
2018-07-18 17:46:21 +02:00
2018-04-02 18:39:00 +02:00
render() {
const {
2019-05-21 23:18:11 +02:00
filePath,
2018-04-02 18:39:00 +02:00
thumbnail,
formDisabled,
uploadThumbnailStatus: status,
openModal,
updatePublishForm,
2018-06-08 06:05:45 +02:00
thumbnailPath,
resetThumbnailStatus,
2018-04-02 18:39:00 +02:00
} = this.props;
2019-03-05 05:46:57 +01:00
const { thumbnailError } = this.state;
2019-05-21 23:18:11 +02:00
const isSupportedVideo = getMediaType(null, filePath) === 'video';
2019-03-05 05:46:57 +01:00
let thumbnailSrc;
if (!thumbnail) {
thumbnailSrc = ThumbnailMissingImage;
} else if (thumbnailError) {
thumbnailSrc = ThumbnailBrokenImage;
} else {
thumbnailSrc = thumbnail;
}
2018-07-18 17:46:21 +02:00
/*
Note:
We are using backgroundImage instead of an <img /> to zoom if the selected thumbnail isn't
the proper aspect ratio. This is to avoid blackbars on the side of images and inconsistent thumbnails
We still need to render the image to see if there is an error loading the url
*/
2018-04-02 18:39:00 +02:00
return (
2018-07-17 19:43:43 +02:00
<div className="card__content">
{status === THUMBNAIL_STATUSES.API_DOWN || status === THUMBNAIL_STATUSES.MANUAL ? (
2018-07-17 19:43:43 +02:00
<div className="column">
2019-05-07 23:38:29 +02:00
<div className="column__item thumbnail-preview" style={{ backgroundImage: `url(${thumbnailSrc})` }}>
<img
style={{ display: 'none' }}
src={thumbnailSrc}
alt={__('Thumbnail Preview')}
2019-03-05 05:46:57 +01:00
onError={e => {
this.setState({
thumbnailError: true,
});
}}
/>
</div>
2018-07-17 19:43:43 +02:00
<div className="column__item">
<FormField
type="text"
name="content_thumbnail"
2018-07-18 17:46:21 +02:00
label="URL"
2018-10-06 00:19:31 +02:00
placeholder="https://spee.ch/mylogo"
2018-07-17 19:43:43 +02:00
value={thumbnail}
disabled={formDisabled}
onChange={this.handleThumbnailChange}
2018-07-18 17:46:21 +02:00
/>
2018-07-17 19:43:43 +02:00
<div className="card__actions">
<Button
button="link"
label={__('Use thumbnail upload tool')}
2019-05-07 23:38:29 +02:00
onClick={() => updatePublishForm({ uploadThumbnailStatus: THUMBNAIL_STATUSES.READY })}
2018-07-17 19:43:43 +02:00
/>
</div>
</div>
</div>
2018-06-08 06:05:45 +02:00
) : (
2018-07-18 17:46:21 +02:00
<React.Fragment>
2018-07-17 19:43:43 +02:00
{status === THUMBNAIL_STATUSES.READY && (
2018-06-08 06:05:45 +02:00
<FileSelector
currentPath={thumbnailPath}
label={__('Thumbnail')}
placeholder={__('Choose a thumbnail')}
2018-10-13 17:49:47 +02:00
filters={filters}
onFileChosen={path => openModal(MODALS.CONFIRM_THUMBNAIL_UPLOAD, { path })}
2018-06-08 06:05:45 +02:00
/>
)}
2019-03-05 05:46:57 +01:00
{status === THUMBNAIL_STATUSES.COMPLETE && thumbnail && (
<div className="column column--space-between">
2019-05-07 23:38:29 +02:00
<div className="column__item thumbnail-preview" style={{ backgroundImage: `url(${thumbnail})` }} />
2019-03-05 05:46:57 +01:00
<div className="column__item">
<p>
2019-05-07 23:38:29 +02:00
Upload complete. <Button button="link" href={thumbnail} label={__('View it on spee.ch')} />.
2019-03-05 05:46:57 +01:00
</p>
<div className="card__actions">
2019-05-07 23:38:29 +02:00
<Button button="link" label={__('New thumbnail')} onClick={resetThumbnailStatus} />
2018-07-18 18:01:28 +02:00
</div>
2018-07-17 19:43:43 +02:00
</div>
2019-03-05 05:46:57 +01:00
</div>
)}
2018-07-18 17:46:21 +02:00
</React.Fragment>
2018-04-02 18:39:00 +02:00
)}
2018-07-18 17:46:21 +02:00
{status === THUMBNAIL_STATUSES.READY && (
<div className="card__actions">
2018-06-08 06:05:45 +02:00
<Button
button="link"
2019-05-21 23:18:11 +02:00
label={__('Enter a thumbnail URL')}
2019-05-07 23:38:29 +02:00
onClick={() => updatePublishForm({ uploadThumbnailStatus: THUMBNAIL_STATUSES.MANUAL })}
2018-06-08 06:05:45 +02:00
/>
2019-05-21 23:18:11 +02:00
{isSupportedVideo && (
<Button
button="link"
label={__('Take a snapshot from your video')}
onClick={() => openModal(MODALS.AUTO_GENERATE_THUMBNAIL, { filePath })}
/>
)}
2018-07-18 17:46:21 +02:00
</div>
)}
2018-04-02 18:39:00 +02:00
{status === THUMBNAIL_STATUSES.IN_PROGRESS && <p>{__('Uploading thumbnail')}...</p>}
<p className="help">
{(status === undefined && __('You should reselect your file to choose a thumbnail')) ||
(status === THUMBNAIL_STATUSES.API_DOWN ? (
__('Enter a URL for your thumbnail.')
) : (
<React.Fragment>
{__('Upload your thumbnail to')}{' '}
<Button button="link" label={__('spee.ch')} href="https://spee.ch/about" />.{' '}
{__('Recommended size: 800x450 (16:9)')}
</React.Fragment>
))}
</p>
2018-04-02 18:39:00 +02:00
</div>
);
}
}
export default SelectThumbnail;