stream/app.js

74 lines
2.0 KiB
JavaScript
Raw Normal View History

2021-03-05 12:17:51 +00:00
import { h } from 'preact'
2021-03-11 20:24:44 +00:00
import { useState, useEffect } from 'preact/hooks'
2021-03-24 15:24:34 +00:00
import { isWithinInterval, subHours } from 'date-fns'
import { addHours } from 'date-fns/esm'
2021-03-05 12:17:51 +00:00
import Video from './src/components/Video'
import config from './src/data/config'
import Info from './src/components/Info'
2021-03-24 15:24:34 +00:00
import { useEventStream } from './src/hooks/data'
2021-03-11 20:24:44 +00:00
import { useTimeout } from './src/hooks/timerHooks'
2021-03-05 12:17:51 +00:00
export default () => {
const [isPlaying, setIsPlaying] = useState(false)
2021-03-24 15:24:34 +00:00
const [currentVideo, setCurrentVideo] = useState(null)
const [streamIsLive, setStreamLive] = useState(false)
const [infoActive, setInfoActive] = useState(false)
2021-03-11 20:24:44 +00:00
const [minLoadTimePassed, setMinTimeUp] = useState(false)
2021-03-24 15:24:34 +00:00
const { data, loading } = useEventStream()
2021-03-11 20:24:44 +00:00
useTimeout(() => {
setMinTimeUp(true)
}, 1500)
useEffect(() => {
if (data && data.length) {
2021-03-24 15:24:34 +00:00
data.forEach((stream, index) => {
// if (stream.title === 'A Wider Screen') {
if (index === 0) {
setCurrentVideo(stream)
}
if (
isWithinInterval(new Date(), {
start: subHours(stream.start, 1),
end: addHours(stream.end, 1),
})
) {
setCurrentVideo(stream)
}
if (
isWithinInterval(new Date(), {
start: stream.start,
end: stream.end,
})
) {
setStreamLive(`${config.peertube_root}${stream.embedPath}`)
2021-03-11 20:24:44 +00:00
}
})
}
}, [data])
2021-03-05 12:17:51 +00:00
return (
<div>
2021-03-24 15:24:34 +00:00
{currentVideo && !infoActive && minLoadTimePassed ? (
<Video
playing={isPlaying}
setPlaying={setIsPlaying}
2021-03-24 15:24:34 +00:00
src={`${config.peertube_root}${currentVideo.embedPath}`}
title={currentVideo.title}
org={currentVideo.channel.displayName}
setInfoActive={setInfoActive}
/>
2021-03-11 20:24:44 +00:00
) : (
2021-03-24 15:24:34 +00:00
<Info
data={data}
loading={loading || !minLoadTimePassed}
infoActive={infoActive}
currentVideo={currentVideo}
setInfoActive={setInfoActive}
/>
2021-03-11 20:24:44 +00:00
)}
2021-03-05 12:17:51 +00:00
</div>
)
}