2020-03-02 00:21:43 +01:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
|
2020-03-18 23:31:02 +01:00
|
|
|
import { getExtension, getType } from '../../utils';
|
2020-03-16 13:37:10 +01:00
|
|
|
|
|
|
|
import BrokenFile from '../../icons/BrokenFile';
|
2020-03-02 00:21:43 +01:00
|
|
|
import FileIcon from '../FileIcon';
|
|
|
|
import Wrapper from './Wrapper';
|
|
|
|
import Image from './Image';
|
2020-03-26 12:15:00 +01:00
|
|
|
import Video from './Video';
|
2020-03-02 00:21:43 +01:00
|
|
|
|
2020-03-26 12:15:00 +01:00
|
|
|
const CardPreview = ({ hasError, url, previewUrl, type, withFileCaching }) => {
|
2020-03-16 13:37:10 +01:00
|
|
|
const isFile = getType(type) === 'file';
|
2020-03-24 22:57:41 +01:00
|
|
|
const isVideo = getType(type) === 'video';
|
2020-03-10 11:32:52 +01:00
|
|
|
|
2020-03-16 15:33:58 +01:00
|
|
|
if (hasError) {
|
|
|
|
return (
|
|
|
|
<Wrapper isFile>
|
|
|
|
<BrokenFile />
|
|
|
|
</Wrapper>
|
|
|
|
);
|
|
|
|
}
|
2020-03-10 11:32:52 +01:00
|
|
|
|
2020-03-24 22:57:41 +01:00
|
|
|
if (isFile) {
|
|
|
|
return (
|
|
|
|
<Wrapper isFile>
|
|
|
|
<FileIcon ext={getExtension(type)} />
|
|
|
|
</Wrapper>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-03-20 23:19:18 +01:00
|
|
|
return (
|
2020-03-24 22:57:41 +01:00
|
|
|
<Wrapper>
|
|
|
|
{isVideo ? (
|
2020-03-26 12:27:40 +01:00
|
|
|
<Video previewUrl={previewUrl} src={url} />
|
2020-03-24 22:57:41 +01:00
|
|
|
) : (
|
2020-03-26 12:27:40 +01:00
|
|
|
// Adding performance.now forces the browser no to cache the img
|
|
|
|
// https://stackoverflow.com/questions/126772/how-to-force-a-web-browser-not-to-cache-images
|
2020-03-24 15:45:01 +01:00
|
|
|
<Image src={`${url}${withFileCaching ? `?${performance.now()}` : ''}`} />
|
2020-03-24 22:57:41 +01:00
|
|
|
)}
|
2020-03-20 23:19:18 +01:00
|
|
|
</Wrapper>
|
|
|
|
);
|
2020-03-02 00:21:43 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
CardPreview.defaultProps = {
|
2020-03-10 11:32:52 +01:00
|
|
|
hasError: false,
|
2020-03-26 12:15:00 +01:00
|
|
|
previewUrl: null,
|
2020-03-02 00:21:43 +01:00
|
|
|
url: null,
|
2020-03-06 17:19:20 +01:00
|
|
|
type: '',
|
2020-03-24 15:45:01 +01:00
|
|
|
withFileCaching: true,
|
2020-03-02 00:21:43 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
CardPreview.propTypes = {
|
2020-03-10 11:32:52 +01:00
|
|
|
hasError: PropTypes.bool,
|
2020-03-26 12:15:00 +01:00
|
|
|
previewUrl: PropTypes.string,
|
2020-03-02 00:21:43 +01:00
|
|
|
url: PropTypes.string,
|
|
|
|
type: PropTypes.string,
|
2020-03-24 15:45:01 +01:00
|
|
|
withFileCaching: PropTypes.bool,
|
2020-03-02 00:21:43 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
export default CardPreview;
|