2021-10-25 14:56:04 +00:00
|
|
|
import { Fragment, h, render } from 'preact'
|
|
|
|
import { useEffect, useState } from 'preact/hooks'
|
2021-10-20 14:33:06 +00:00
|
|
|
import { ThemeProvider } from 'styled-components'
|
2021-10-11 13:50:24 +00:00
|
|
|
import { BrowserRouter, Route, Switch } from 'react-router-dom'
|
2021-10-25 14:56:04 +00:00
|
|
|
import { isWithinInterval, subHours, addHours } from 'date-fns'
|
|
|
|
import { zonedTimeToUtc, utcToZonedTime } from 'date-fns-tz'
|
2021-10-11 13:50:24 +00:00
|
|
|
|
|
|
|
import Main from './app'
|
|
|
|
import SeriesPage from './src/pages/SeriesPage'
|
2021-10-25 14:56:04 +00:00
|
|
|
import { useEventApi, usePeertubeApi } from './src/hooks/data'
|
2021-11-01 22:34:29 +00:00
|
|
|
import { useStreamStore, useTheme, useUiStore } from './src/store'
|
2021-10-11 13:50:24 +00:00
|
|
|
import { useTimeout } from './src/hooks/timerHooks'
|
|
|
|
import LoaderLayout from './src/pages/LoaderLayout'
|
2021-10-15 13:37:54 +00:00
|
|
|
import FourOhFour from './src/pages/404'
|
|
|
|
import Series from './src/pages/Series'
|
2021-10-22 13:44:28 +00:00
|
|
|
import Program from './src/pages/Program'
|
2021-10-25 14:56:04 +00:00
|
|
|
import StreamPreview from './src/components/StreamPreview'
|
2021-11-01 22:34:29 +00:00
|
|
|
import Video from './src/components/Video'
|
2022-09-07 12:37:49 +00:00
|
|
|
import Chat from './src/components/Chat'
|
2021-11-01 22:34:29 +00:00
|
|
|
// import { useWindowSize } from './src/hooks/dom'
|
2021-10-11 13:50:24 +00:00
|
|
|
|
|
|
|
const App = () => {
|
2022-09-07 12:37:49 +00:00
|
|
|
const { theme } = useTheme(store => store)
|
2021-11-01 22:34:29 +00:00
|
|
|
const { data, loading: eventsLoading, error } = useEventApi()
|
2021-10-11 13:50:24 +00:00
|
|
|
const [minLoadTimePassed, setMinTimeUp] = useState(false)
|
2022-09-07 12:37:49 +00:00
|
|
|
const { setCurrentStream, currentStream, streamIsLive } = useStreamStore(
|
|
|
|
store => store
|
|
|
|
)
|
2021-11-01 22:34:29 +00:00
|
|
|
const streamActive = useUiStore(store => store.streamActive)
|
2021-10-25 14:56:04 +00:00
|
|
|
usePeertubeApi(data.episodes)
|
2021-10-11 13:50:24 +00:00
|
|
|
|
|
|
|
useTimeout(() => {
|
|
|
|
setMinTimeUp(true)
|
|
|
|
}, 1500)
|
|
|
|
|
2021-10-25 14:56:04 +00:00
|
|
|
useEffect(() => {
|
|
|
|
if (Array.isArray(data.episodes)) {
|
|
|
|
data.episodes.forEach(stream => {
|
|
|
|
const utcStartDate = zonedTimeToUtc(
|
|
|
|
new Date(stream.beginsOn),
|
|
|
|
'Europe/Berlin'
|
|
|
|
)
|
2022-09-07 12:37:49 +00:00
|
|
|
const utcEndDate = zonedTimeToUtc(
|
|
|
|
new Date(stream.endsOn),
|
|
|
|
'Europe/Berlin'
|
|
|
|
)
|
2021-10-25 14:56:04 +00:00
|
|
|
const { timeZone } = Intl.DateTimeFormat().resolvedOptions()
|
|
|
|
|
2021-11-19 15:37:36 +00:00
|
|
|
const zonedStartDate = utcToZonedTime(utcStartDate, 'Europe/Berlin')
|
|
|
|
const zonedEndDate = utcToZonedTime(utcEndDate, 'Europe/Berlin')
|
2021-10-25 14:56:04 +00:00
|
|
|
if (
|
|
|
|
isWithinInterval(new Date(), {
|
2021-11-19 15:37:36 +00:00
|
|
|
start: subHours(zonedStartDate, 2),
|
2021-10-25 14:56:04 +00:00
|
|
|
end: addHours(zonedEndDate, 1),
|
|
|
|
})
|
|
|
|
) {
|
|
|
|
setCurrentStream(stream)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}, [eventsLoading])
|
|
|
|
|
2021-10-22 13:44:28 +00:00
|
|
|
// console.log({ episodes: data.episodes, series: data.series })
|
|
|
|
|
|
|
|
const seriesData = data.series ? Object.values(data.series) : []
|
2021-10-11 13:50:24 +00:00
|
|
|
|
2021-10-20 14:33:06 +00:00
|
|
|
return (
|
|
|
|
<ThemeProvider theme={theme}>
|
2021-11-01 22:34:29 +00:00
|
|
|
{!seriesData.length || eventsLoading || !minLoadTimePassed || error ? (
|
|
|
|
<LoaderLayout error={error} />
|
2021-10-20 14:33:06 +00:00
|
|
|
) : (
|
2021-10-25 14:56:04 +00:00
|
|
|
<Fragment>
|
|
|
|
<BrowserRouter>
|
|
|
|
<Switch>
|
2021-11-01 22:34:29 +00:00
|
|
|
<Route exact path="/" component={Program} />
|
2021-10-25 14:56:04 +00:00
|
|
|
<Route exact path="/series" component={Series} />
|
|
|
|
<Route exact path="/program" component={Program} />
|
2022-09-07 12:37:49 +00:00
|
|
|
{seriesData.length
|
|
|
|
? seriesData.map(series => (
|
|
|
|
<Route exact path={`/series/${series.slug}`}>
|
|
|
|
<SeriesPage data={series} />
|
|
|
|
</Route>
|
|
|
|
))
|
|
|
|
: null}
|
2021-10-25 14:56:04 +00:00
|
|
|
<Route path="*">
|
|
|
|
<FourOhFour />
|
|
|
|
</Route>
|
|
|
|
</Switch>
|
|
|
|
</BrowserRouter>
|
2022-09-07 12:37:49 +00:00
|
|
|
|
|
|
|
{streamActive ? (
|
|
|
|
<Video stream={currentStream} />
|
|
|
|
) : (
|
|
|
|
<StreamPreview stream={currentStream} isLive={streamIsLive} />
|
|
|
|
)}
|
2021-10-25 14:56:04 +00:00
|
|
|
</Fragment>
|
2021-10-20 14:33:06 +00:00
|
|
|
)}
|
2022-09-07 12:37:49 +00:00
|
|
|
</ThemeProvider>
|
|
|
|
)
|
2021-10-11 13:50:24 +00:00
|
|
|
}
|
|
|
|
|
2021-03-10 13:51:24 +00:00
|
|
|
const appEl = document.getElementById('app')
|
2021-03-05 12:17:51 +00:00
|
|
|
|
2021-03-10 13:51:24 +00:00
|
|
|
render(<App />, appEl)
|