import React from 'react';
import PropTypes from 'prop-types';
import styled from 'styled-components';
import { Text } from '@bufferapp/components';
import ReactPlayer from 'react-player'
const Container = styled.div`
width: 14rem;
height: 14rem;
border-radius: 3px 0 0 3px;
border: 1px solid #DEDEDE;
border-width: 0 1px 0 0;
overflow: hidden;
`;
const Media = styled.div`
width: 100%;
height: 100%;
background: transparent;
background-image: url(${props => props.thumbnail});
background-repeat: no-repeat;
background-size: cover;
background-position: center;
`;
const Empty = styled.div`
width: 100%;
height: 100%;
background: #FDFDFD;
display: flex;
align-items: center;
justify-content: center;
box-shadow: inset -2px 0 8px rgba(0,0,0,0.05);
box-sizing: border-box;
`;
const TypeIcon = styled.div`
width: 32px;
height: 32px;
background: rgba(255, 255, 255, 0.75);
box-shadow: 0px 2px 4px rgba(0, 0, 0, 0.08);
position: absolute;
right: 10px;
bottom: 10px;
border-radius: 100%;
display: flex;
align-items: center;
justify-content: center;
`;
const MediaContainer = styled.div`
position: relative;
width: 100%;
height: 100%;
`;
const VideoIcon = () => (<svg width="16" height="12" viewBox="0 0 16 12" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M11.52 4.16V1.92819C11.52 0.865872 10.6567 0 9.59181 0L1.92819 0C0.865872 0 0 0.86328 0 1.92819V9.59181C0 10.6541 0.86328 11.52 1.92819 11.52H9.59181C10.6541 11.52 11.52 10.6567 11.52 9.59181V7.36L13.8916 9.33634C14.6993 10.0094 15.36 9.70055 15.36 8.64037V2.87963C15.36 1.81194 14.7026 1.50785 13.8916 2.18366L11.52 4.16Z" fill="black" />
</svg>);
const CarouselIcon = () => (<svg width="15" height="15" viewBox="0 0 15 15" fill="none" xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd" clip-rule="evenodd" d="M2 0C0.895431 0 0 0.895433 0 2V9.52C0 10.6246 0.895433 11.52 2 11.52H10.52C11.0723 11.52 11.52 11.0723 11.52 10.52V2C11.52 0.895431 10.6246 0 9.52 0H2ZM5.23512 14.7536C4.25568 14.7536 3.44067 14.0496 3.26859 13.12H12.1193C12.6716 13.12 13.1193 12.6723 13.1193 12.12V3.26673C14.05 3.43797 14.7551 4.25347 14.7551 5.23365V12.7536C14.7551 13.8582 13.8597 14.7536 12.7551 14.7536H5.23512Z" fill="black"/>
</svg>);
const Attachment = ({ type, media }) => {
function hasAttachment(type, media) {
return (['picture', 'photo', 'video', 'image', 'carousel_album', 'link'].includes(type) && media);
}
function getThumbnail() {
if (type === 'video') {
return (<MediaContainer>
{String(media.thumbnail).match(/\.mp4/) && <ReactPlayer
url={media.thumbnail}
width="200%"
height="200%"
mute
style={{
display: 'flex',
background: '#F5F5F5',
width: '200%',
height: '200%',
position: 'relative',
top: '-50%',
left: '-50%',
}}
/>}
{!String(media.thumbnail).match(/\.mp4/) &&
<Media thumbnail={`https://safeimage.buffer.com/224x/${media.thumbnail}`} />
}
<TypeIcon> <VideoIcon /> </TypeIcon>
</MediaContainer>);
}
if (type === 'carousel_album') {
return (<MediaContainer>
<Media thumbnail={`https://safeimage.buffer.com/224x/${media.thumbnail}`} />
<TypeIcon> <CarouselIcon /> </TypeIcon>
</MediaContainer>);
}
return <Media thumbnail={`https://safeimage.buffer.com/224x/${media.thumbnail}`} />;
}
return (
<Container>
{hasAttachment(type, media)
? getThumbnail()
: <Empty><Text color="lightSlate">No media</Text></Empty>
}
</Container>
)
};
Attachment.defaultProps = {
media: null,
};
Attachment.propTypes = {
type: PropTypes.string.isRequired,
media: PropTypes.shape({
thumbnail: PropTypes.String,
picture: PropTypes.String,
}),
};
export default Attachment;
|