stream/app.js

73 lines
2.2 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-10-11 11:48:17 +00:00
import { isWithinInterval, subHours, addHours } from 'date-fns'
2021-10-11 13:50:24 +00:00
import { zonedTimeToUtc, utcToZonedTime } from 'date-fns-tz'
2021-03-05 12:17:51 +00:00
import Video from './src/components/Video'
import Info from './src/components/Info'
2021-10-11 11:48:17 +00:00
import { useEventCalendar, useEventApi } from './src/hooks/data'
2021-03-11 20:24:44 +00:00
import { useTimeout } from './src/hooks/timerHooks'
2021-10-11 13:50:24 +00:00
export default (props) => {
2021-10-11 17:15:09 +00:00
// console.log({ props })
2021-03-24 15:24:34 +00:00
const [currentVideo, setCurrentVideo] = useState(null)
2021-03-29 16:03:46 +00:00
const [streamIsLive, setStreamIsLive] = useState(false)
2021-03-24 15:24:34 +00:00
const [infoActive, setInfoActive] = useState(false)
2021-03-11 20:24:44 +00:00
const [minLoadTimePassed, setMinTimeUp] = useState(false)
2021-10-11 11:48:17 +00:00
const { data: calData, loading } = useEventCalendar()
const { data: eventsData, loading: eventsLoading } = useEventApi()
2021-03-11 20:24:44 +00:00
useTimeout(() => {
setMinTimeUp(true)
}, 1500)
useEffect(() => {
2021-10-11 11:48:17 +00:00
if (calData && calData.length) {
calData.forEach((stream, index) => {
2021-05-23 15:21:15 +00:00
const utcStartDate = zonedTimeToUtc(
new Date(stream.start),
'Europe/Berlin'
)
const utcEndDate = zonedTimeToUtc(new Date(stream.end), 'Europe/Berlin')
const { timeZone } = Intl.DateTimeFormat().resolvedOptions()
const zonedStartDate = utcToZonedTime(utcStartDate, timeZone)
const zonedEndDate = utcToZonedTime(utcEndDate, timeZone)
2021-03-24 15:24:34 +00:00
if (
isWithinInterval(new Date(), {
2021-05-23 15:21:15 +00:00
start: subHours(zonedStartDate, 1),
end: addHours(zonedEndDate, 1),
2021-03-24 15:24:34 +00:00
})
) {
setCurrentVideo(stream)
}
if (
isWithinInterval(new Date(), {
2021-05-23 15:21:15 +00:00
start: zonedStartDate,
end: zonedEndDate,
2021-03-24 15:24:34 +00:00
})
) {
2021-03-29 16:03:46 +00:00
setStreamIsLive(true)
2021-03-11 20:24:44 +00:00
}
})
}
2021-10-11 11:48:17 +00:00
}, [calData, eventsData, eventsLoading])
2021-03-05 12:17:51 +00:00
return (
<div>
2021-03-24 15:24:34 +00:00
{currentVideo && !infoActive && minLoadTimePassed ? (
2021-03-29 16:03:46 +00:00
<Video video={currentVideo} setInfoActive={setInfoActive} />
2021-03-11 20:24:44 +00:00
) : (
2021-03-24 15:24:34 +00:00
<Info
2021-10-11 11:48:17 +00:00
data={calData}
2021-10-11 13:50:24 +00:00
loading={false}
2021-03-24 15:24:34 +00:00
infoActive={infoActive}
currentVideo={currentVideo}
setInfoActive={setInfoActive}
/>
2021-03-11 20:24:44 +00:00
)}
2021-03-05 12:17:51 +00:00
</div>
)
}