import React from 'react'
import Img from 'react-image'
import { Player } from 'video-react'
import Md from './Md.jsx'
import Spinner from './presentational/Spinner'
import NoSource from './presentational/NoSource'
// TODO: move render functions into presentational components
class SourceOverlay extends React.Component {
constructor () {
super()
this.state = { idx: 0 }
}
renderImage (path) {
return (
}
unloader={}
/>
)
}
renderVideo (path) {
return (
)
}
renderText (path) {
return (
}
unloader={() => this.renderError()}
/>
)
}
renderNoSupport (ext) {
return (
)
}
toMedia (path) {
let type
switch (true) {
case /\.(png|jpg)$/.test(path):
type = 'Image'; break
case /\.(mp4)$/.test(path):
type = 'Video'; break
case /\.(md)$/.test(path):
type = 'Text'; break
default:
type = 'Unknown'; break
}
return { type, path }
}
getTypeCounts (media) {
return media.reduce(
(acc, vl) => {
acc[vl.type] += 1
return acc
},
{ Image: 0, Video: 0, Text: 0 }
)
}
_renderPath (media) {
const { path, type } = media
switch (type) {
case 'Image':
return this.renderImage(path)
case 'Video':
return this.renderVideo(path)
case 'Text':
return this.renderText(path)
default:
return this.renderNoSupport(path.split('.')[1])
}
}
_renderCounts (counts) {
const strFor = type =>
counts[type] > 0
? `${counts[type]} ${type.toLowerCase()}${counts[type] > 1 ? 's' : ''}`
: ''
const img = strFor('Image')
const vid = strFor('Video')
const txt = strFor('Text')
return (
{img || ''}
{(img && vid) ? `, ${vid}` : (vid || '')}
{((img || vid) && txt) ? `, ${txt}` : (txt || '')}
)
}
_renderContent (media) {
const el = document.querySelector(`.source-media-gallery`)
const shiftW = el ? el.getBoundingClientRect().width : 0
return (
{media.map((m) => this._renderPath(m))}
)
}
onShiftGallery (shift) {
// no more left
if (this.state.idx === 0 && shift === -1) return
// no more right
if (this.state.idx === this.props.source.paths.length - 1 && shift === 1) return
this.setState({ idx: this.state.idx + shift })
}
_renderControls () {
const backArrow = this.state.idx !== 0 ? (
this.onShiftGallery(-1)}
>
) : null
const forwardArrow = this.state.idx < this.props.source.paths.length - 1 ? (
this.onShiftGallery(1)}
>
) : null
if (this.props.source.paths.length > 1) {
return (
{backArrow}
{forwardArrow}
)
}
return (
)
}
render () {
if (typeof (this.props.source) !== 'object') {
return this.renderError()
}
const { url, title, paths, date, type, desc } = this.props.source
const media = paths.map(this.toMedia)
return (
e.stopPropagation()}>
{this.props.source.title.substring(0, 200)}
{this._renderContent(media)}
{this._renderControls()}
{/*
{`${this.state.idx+1} / ${paths.length}`}
*/}
{title ?
{title}
: null}
{desc}
{type ?
Media type
: null}
{type ?
perm_media{type}
: null}
{date ?
Date
: null}
{date ?
today{date}
: null}
)
}
}
export default SourceOverlay