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

184 lines
6.6 KiB
React
Raw Permalink Normal View History

2018-04-02 18:39:00 +02:00
// @flow
import * as MODALS from 'constants/modal_types';
2019-08-02 08:28:14 +02:00
import { Lbry, THUMBNAIL_STATUSES } from 'lbry-redux';
import { DOMAIN } from 'config';
2018-07-18 17:46:21 +02:00
import * as React from 'react';
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,
2019-08-29 17:19:37 +02:00
fileInfos: { [string]: FileListItem },
myClaimForUri: ?StreamClaim,
2018-04-02 18:39:00 +02:00
thumbnail: ?string,
formDisabled: boolean,
uploadThumbnailStatus: string,
2018-06-08 06:05:45 +02:00
thumbnailPath: ?string,
thumbnailError: ?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
};
class SelectThumbnail extends React.PureComponent<Props> {
2018-07-17 19:43:43 +02:00
constructor() {
super();
(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
updatePublishForm({
thumbnail: newThumbnail,
thumbnailError: newThumbnail.startsWith('data:image'),
});
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,
2019-08-29 17:19:37 +02:00
fileInfos,
myClaimForUri,
2018-04-02 18:39:00 +02:00
thumbnail,
formDisabled,
uploadThumbnailStatus: status,
openModal,
updatePublishForm,
2018-06-08 06:05:45 +02:00
thumbnailPath,
thumbnailError,
2018-06-08 06:05:45 +02:00
resetThumbnailStatus,
2018-04-02 18:39:00 +02:00
} = this.props;
2019-03-05 05:46:57 +01:00
const accept = '.png, .jpg, .jpeg, .gif';
2019-03-05 05:46:57 +01:00
2019-08-29 17:19:37 +02:00
const outpoint = myClaimForUri ? `${myClaimForUri.txid}:${myClaimForUri.nout}` : undefined;
const fileInfo = outpoint ? fileInfos[outpoint] : undefined;
const downloadPath = fileInfo ? fileInfo.download_path : undefined;
const actualFilePath = filePath || downloadPath;
let isSupportedVideo = false;
if (typeof actualFilePath === 'string') {
isSupportedVideo = Lbry.getMediaType(null, actualFilePath) === 'video';
} else if (actualFilePath && actualFilePath.type) {
isSupportedVideo = actualFilePath.type.split('/')[0] === 'video';
}
2019-05-21 23:18:11 +02:00
let thumbnailInputSrc;
2019-03-05 05:46:57 +01:00
if (!thumbnail) {
thumbnailInputSrc = ThumbnailMissingImage;
2019-03-05 05:46:57 +01:00
} else if (thumbnailError) {
thumbnailInputSrc = ThumbnailBrokenImage;
2019-03-05 05:46:57 +01:00
} else {
thumbnailInputSrc = thumbnail;
2019-03-05 05:46:57 +01:00
}
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 (
2019-07-21 23:31:22 +02:00
<div>
{status === THUMBNAIL_STATUSES.API_DOWN || status === THUMBNAIL_STATUSES.MANUAL ? (
2018-07-17 19:43:43 +02:00
<div className="column">
<div className="column__item thumbnail-preview" style={{ backgroundImage: `url(${thumbnailInputSrc})` }}>
<img
style={{ display: 'none' }}
src={thumbnailInputSrc}
alt={__('Thumbnail Preview')}
onError={() => updatePublishForm({ 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"
placeholder="https://images.fbi.gov/alien"
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 an enticing thumbnail')}
accept={accept}
onFileChosen={(file) => openModal(MODALS.CONFIRM_THUMBNAIL_UPLOAD, { file })}
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">
<div className="column__item thumbnail-preview" style={{ backgroundImage: `url(${thumbnail})` }}>
{(thumbnailError !== false) && __('This will be visible in a few minutes.')}
<img
style={{ display: 'none' }}
src={thumbnail}
alt={__('Thumbnail Preview')}
onError={() => updatePublishForm({ thumbnailError: true })}
onLoad={() => updatePublishForm({ thumbnailError: false })}
/>
</div>
2019-03-05 05:46:57 +01:00
<div className="column__item">
2020-07-23 19:11:53 +02:00
<p>{__('Upload complete.')}</p>
<div className="section__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 && (
2020-07-23 19:11:53 +02:00
<div className="section__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
/>
{isSupportedVideo && IS_WEB && (
// Disabled on desktop until this is resolved
// https://github.com/electron/electron/issues/20750#issuecomment-709505902
2019-05-21 23:18:11 +02:00
<Button
button="link"
label={__('Take a snapshot from your video')}
2019-08-29 17:19:37 +02:00
onClick={() => openModal(MODALS.AUTO_GENERATE_THUMBNAIL, { filePath: actualFilePath })}
2019-05-21 23:18:11 +02:00
/>
)}
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>}
2020-07-23 19:11:53 +02:00
{status !== THUMBNAIL_STATUSES.COMPLETE && (
<p className="help">
{status === THUMBNAIL_STATUSES.API_DOWN
? __('Enter a URL for your thumbnail.')
: __('Upload your thumbnail to %domain%. Recommended size is 16:9.', { domain: DOMAIN })}
</p>
)}
2018-04-02 18:39:00 +02:00
</div>
);
}
}
export default SelectThumbnail;